Can't seem to run a program from a service. Any ideas why?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Can't seem to run a program from a service. Any ideas why?

  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    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.

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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?

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by ahmd View Post
    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.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by Skizmo View Post
    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.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by Skizmo View Post
    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?

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by ahmd View Post
    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.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Can't seem to run a program from a service. Any ideas why?

    Thanks, Arjay.
    Quote Originally Posted by Arjay View Post
    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

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by ahmd View Post
    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
    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).

  10. #10
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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...

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    Re: Can't seem to run a program from a service. Any ideas why?

    Quote Originally Posted by ahmd View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center