0

In my application I have one service which is launching one exe.

In service code I am launching exe with ShellExecuteEx command as :

BOOL bLaunched = false;



       SHELLEXECUTEINFO ex;
       memset(&ex, 0, sizeof(ex));
       ex.cbSize       = sizeof(ex);
       ex.fMask        = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
       ex.hwnd         = ::GetDesktopWindow();
       ex.lpVerb       = _T("open");
       ex.lpParameters = params;
       ex.lpDirectory  = path;
       ex.nShow        = SW_SHOWNORMAL;
       ex.lpFile       = appName;

       bLaunched = ShellExecuteEx(&ex);
       return bLaunched;

After ShellExecuteEx call I can see that exe in task manager and when I try to attach that exe to VS debugger and break it ,it show me message "the process appears to be deadlocked(or is not running any user-mode code).All threads have been stopped"

VS debugger shows green arrow pointed to first line in winMain funciton.

This only happening when I try to launch that exe from a service. When I try to launch that exe from sample application with same set of params it works fine .

Any suggestion why its not working in case of service.

Updates: I figured it out.I was calling MessageBox in the exe.I removed MessageBox and it executed fine. It looks like calling windows related functions caused that issue. Actually service was running in Local System account and exe launched from that service .So can we not call window functions from Local system account.

One more issue I am facing now.In the exe it is not able to open registry to read values.I am trying to open HKCU but it failed.

anand
  • 11,071
  • 28
  • 101
  • 159
  • 1
    As stated in [the MSDN reference](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154%28v=vs.85%29.aspx), you have initialized COM? And you don't hold a [loader lock](http://msdn.microsoft.com/en-us/library/dd744765%28VS.85%29.aspx)? – Some programmer dude Jan 31 '14 at 11:31
  • I am not sure that could be the reason because I just tried using CreateProcess and I can see my exe in taskmanager but its is again in deadlocked state – anand Jan 31 '14 at 11:40
  • 1
    Passing the desktop window is a really bad idea, pass NULL. Watch out for the Image File Execution Options registry key if you tinkered with it before. – Hans Passant Jan 31 '14 at 15:18
  • Odd. What do you suppose `GetDesktopWindow()` should return, when called from a service? I cannot think of a scenario where this would be legal and actually make sense. Likewise, calling `ShellExecuteEx` from a service is fairly non-sensical. – IInspectable Jan 31 '14 at 17:39
  • yes figured out that,cannt call GetDesktopWindow from services running in system account – anand Feb 17 '14 at 05:09

1 Answers1

1

You are attempting to launch an executable that interacts with the desktop. Services run in session 0 which is a non-interactive session. You'll need to make sure that any processes that you start in that session do not interact with the desktop.

As an aside, you should never ever pass the desktop window as an owner window: http://blogs.msdn.com/b/oldnewthing/archive/2004/02/24/79212.aspx.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Any idea why that exe is not able to open registry . I am expecting service which is running in system account has high level privileges and exe launched by service should also be able to do all stuff. – anand Jan 31 '14 at 15:58
  • The process will be able to open the registry, but what value do you expect to find in `HKCU` for a process running as LocalSystem? I guess you actually want the registry for the logged on user. But that's hard to locate from a serivce running as LocalSystem. Btw, why did you choose to use LocalSystem? You are not supposed to do that. – David Heffernan Jan 31 '14 at 16:01