Start and impersonate a console app from windows service

A

Agnes

Hi,

I posted a question earlier asking about starting a process as a different
user from windows service. I got some replies and read about this a bit
more on the web but still couldn't get it working.

The application I want to start is a console app and it also opens a win
form which contains an axWebBrowser. There is no user interaction with the
form.

I've tried creating a Process and set the username, password and domain in
the Process's StartInfo:

Process p = new Process();
p.StartInfo.FileName = "somePathToThe.EXE";
p.StartInfo.Arguments = "some arguments";
p.StartInfo.UserName = "theUser@Domain";
p.StartInfo.Password = passwordInSecureString;
p.StartInfo.UseShellExecute = false;
p.Start();

I've also tried Pinvoke the CreateProcessWithLogonW Windows API call. I
read about having to set the default desktop for console application so I
did this: startupInfo.lpDesktop = @"winsta0\default";

These 2 methods both did not work. I got this message as soon as the
process starts: The application failed to initialize properly (0xc0000142).
Click on OK to terminate the application.

If I start this application without the impersonate, it works perfectly. I
won't see the console and win form showing up but I see the expected result
when the app ends.
Only when I add in the impersonate, I get the error message every time when
the windows service tries to start my application.

Also I have a console app which does the same thing as my win service. If I
try the impersonation in the console mode, it works. Only when it runs from
win services, it gives the error.

Is there other way to do this? Or am I missing something in my code??

Thank you!!

Agnes
 
M

Massimo

Also I have a console app which does the same thing as my win service. If
I try the impersonation in the console mode, it works. Only when it runs
from win services, it gives the error.

What happens if you run the service using a System or Administrator account?


Massimo
 
A

Agnes

When running service as Local System Account, I got "access is denied" error
or Win32 error code 5 which is the same error "Access is denied".

When running as admin or normal user account, it shows the "application
failed to initialize properly" error. :(
 
M

Massimo

When running service as Local System Account, I got "access is denied"
error or Win32 error code 5 which is the same error "Access is denied".

When running as admin or normal user account, it shows the "application
failed to initialize properly" error. :(

Have you tried running it as the same user with which you're logged on?
Also, did you set the option to allow the service to interact with the
desktop?


Massimo
 
A

Agnes

I have just tried changing the LogonFlag of the CreateProcessWithLogonW
method from LOGON_WITH_PROFILE to LOGON_NETCREDENTIALS_ONLY.

It started the new process properly but then I realised the process was not
running as the new user, it's using the user credential for network only.

So windows service has no problem starting a new process. But if the
process has to be started as a different user, it gives that error.

Is there some permission thing that I need to setup before creating the
process?? Has anyone tried this before?

Thanks in advance!!!
 
A

Agnes

Have you tried running it as the same user with which you're logged on?
Also, did you set the option to allow the service to interact with the
desktop?

yup, I tried running as myself and I am also the admin on the machine but
it's still not working.

I see that I can only set that option if I use the Local System account to
run the service. From the service property window, I don't seen an option
to enter a user logon and select the allow service to interact with desktop.
Is there other way to set this up??

Thank you very much for your help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top