Can't seem to run a program from a service. Any ideas why?
I made a small service program that is starting as SYSTEM. In it I need to open a file. I'm using the following line that works perfectly fine in a regular Win32 app:
Code:
ShellExecute(NULL, _T(""), _T("calc.exe"), _T(""), _T(""), SW_SHOW);
but when I run it from my service the return code is greater than 32 (meaning success), and I can see calc.exe in the task manager but there's nothing on the screen.
What am I not doing right?
Re: Can't seem to run a program from a service. Any ideas why?
The service runs under a different desktop. The program is starting and is running on a desktop it just isn't the same desktop as a logged on user.
Re: Can't seem to run a program from a service. Any ideas why?
Thanks, Arjay. It makes sense. But, is it possinle to run that file in a current desktop from my service?
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
ahmd
Thanks, Arjay. It makes sense. But, is it possinle to run that file in a current desktop from my service?
Yes. Read the documentation on services and you will see that there is a option you can use to make service visible.
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
Skizmo
Yes. Read the documentation on services and you will see that there is a option you can use to make service visible.
That no longer works or is recommended on Vista and above.
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
Skizmo
Yes. Read the documentation on services and you will see that there is a option you can use to make service visible.
I don't really need an interactive service, I'm just trying to start a "regular" (non-service) process from my service and I want it to show on the desktop for user input.
Guys, I found this sample on MSDN:
http://msdn.microsoft.com/en-us/libr...08(VS.85).aspx
It's kinda big and bulky (like anything else from Microsoft), so before I begin trying to tailor it, do you think is it what I need in my case?
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
ahmd
It's kinda big and bulky (like anything else from Microsoft), so before I begin trying to tailor it, do you think is it what I need in my case?
It's what you need. Not sure about the big and bulky comment though.
Re: Can't seem to run a program from a service. Any ideas why?
Thanks, Arjay.
Quote:
Originally Posted by
Arjay
Not sure about the big and bulky comment though.
More than 500 lines of code just to run a program + additional hundred in the comments to clean up the result, come on, man :D
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
ahmd
Thanks, Arjay.
More than 500 lines of code just to run a program + additional hundred in the comments to clean up the result, come on, man :D
Well considering you are in a different desktop and have to find the correct desktop for the user, is it really that bad?
Let me get this straight, are you complaining because there are lots of comments?
You know of course that you aren't suppose to just cut & paste the msdn snippets into your source code, right? (because nothing says "amateur" more than running across source code that is an obvious cut and paste job).
Re: Can't seem to run a program from a service. Any ideas why?
Well, I just killed 4 hours trying to make this sample work. For some stupid reason the CreateProcessAsUser was throwing an exception from within and I couldn't figure out why. It took me several times to reread their documentation to see that this API may modify the lpCommandLine parameter in a unicode build and it cannot be const like they put it in their dumb sample. There must have been a bunch of "professionals" at Microsoft who wrote it. Good point Arjay...
Re: Can't seem to run a program from a service. Any ideas why?
Quote:
Originally Posted by
ahmd
Well, I just killed 4 hours trying to make this sample work. For some stupid reason the CreateProcessAsUser was throwing an exception from within and I couldn't figure out why. It took me several times to reread their documentation to see that this API may modify the lpCommandLine parameter in a unicode build and it cannot be const like they put it in their dumb sample. There must have been a bunch of "professionals" at Microsoft who wrote it. Good point Arjay...
This is part of the learning curve. No sample is perfect, but there's no need to blame Microsoft.
Btw, you can be part of the solution by pointing out the error in the sample documentation. You'll notice that at the bottom of the documentation there is a place where you can send feedback to Microsoft. You just might get it fixed for the next guy.