Link to home
Start Free TrialLog in
Avatar of PhilAI
PhilAIFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Execute another application from a .NET Windows Service

I am running a Windows Service I created in C# on Vista Premium. When I execute the service from the IDE the application (bittorrent.exe) loads successfully. However, when I actually install the application and run the service under my Administrator account, the application executes, but does not appear. It appears in the Task Manager, but with no interface. In fact, the application is not doing anything - I know this because the CPU activity is zero, and there is no internet activity (for the files that should be downloading/uploading within the bittorrent application).

Any ideas anyone? Am I missing a privilege in order to execute another application. Perhaps there is something else I can do?
// Build the structure used to execute an application
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\BitTorrent\bittorrent.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.RedirectStandardOutput = false;
startInfo.UseShellExecute = true;
// Execute the application
Process process = Process.Start(startInfo);

Open in new window

Avatar of PhilAI
PhilAI
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

OK, I have done some more research and discovered that in Vista services run in a different session from my user, so that is why it works in the IDE and not from the service.

I found this article to assist me:
http://blogs.msdn.com/sripod/archive/2006/11/21/session-0-isolation-in-vista-and-application-compatibility.aspx
Avatar of PhilAI

ASKER

But what I need help with now is how to convert the following steps into C#:
1) Call OpenProcess with PROCESS_ALL_ACCESS and open a user process. If the call is successful, it returns a handle to the process.
2) Use the handle obtained above to make a call to OpenProcessToken and specify TOKEN_ASSIGN_PRIMARY and TOKEN_DUPLICATE as the desired access. If the call is successful, it returns a handle to the process token.
3) Use the token handle obtained in step 2 above, and call DuplicateTokenEx function and specify TOKEN_ASSIGN_PRIMARY and TOKEN_ALL_ACCESS as the access rights. Also specify token type as TokenPrimary. If the call succeeds, it returns you a handle to a token which can be used as a primary token.
4) Call CreateProcessAsUser function to create an application in the user's session.

My main question on this is: "What is a 'user process' in step 1?"
Avatar of kaufmed
A user process is what you attempting to run--a regular application. There are user processes and kernel processes in a typical operating system. User processes run under restricted access to the system's resources (i.e. you have to go through the OS to gain access to the hard disk). Kernel processes have unrestricted access to the machine.

What you are dealing with in the blog you posted a link to are API calls--which can be tedious and confusing. I would ask if you are sure you need to go that route or can you possibly use some other method of interacting with the service (i.e. if you are expecting output from the service, write to a text file rather than try to display to screen).
Avatar of PhilAI

ASKER

I have been using API calls in VB6 for a over ten years so I am familiar with them - just not these ones. I must admit, they are tedious though! OK, what I want to achieve is the following...

My wife uses her laptop a lot when I am not at home so I have provided her a website that she can access that will give her the option to stop downloads on my computer (so that my computer is not eating all the bandwidth). I have the website stopping the downloads, but I now want to be able to restart them, and eventually interact with the third party application that is doing the downloading.

The current target I am trying to achieve is the user will check a box that will execute the download application. I thought I would have to create a service to be able to have the permissions to run the application as I was unaware of different sessions in Windows. I would ideally like to use a service as I simply need a background application to take in commands to execute applications in the user's current session.

If there is a better technique than using a Windows service, then please make some suggestions... Perhaps using an application that runs on start up and awaits commands via TCP within the user's session? However, I would really like to use a service, but since this is for my computer only, it is not that important.
ASKER CERTIFIED SOLUTION
Avatar of PhilAI
PhilAI
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial