1

How do I launch an executable with supplied arguments if I'm running as network service? I've tried using ShellExecuteEx from within my program (which runs as Network Service):

try
{

    DWORD dwErr;
    TCHAR bufProgName[1000] = _T("");
    TCHAR bufParameters[1000] = _T("");

    ::lstrcat(bufProgName,_T("C:\\NotMyFault\\x86\\NotMyFault.exe"));
    ::lstrcat(bufParameters,_T(" -help"));

    SHELLEXECUTEINFO ShExecInfo;
    ZeroMemory(&ShExecInfo,sizeof(ShExecInfo));
    ShExecInfo.cbSize=sizeof(ShExecInfo);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = bufProgName;        
    ShExecInfo.lpParameters = bufParameters;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL; 
    int res = ShellExecuteEx(&ShExecInfo);
    if(!res)
    {
        dwErr = GetLastError();
        return -1;
    }

}
catch(...)
{
    std::cout<<"ShellFail: " << dwError;
    return -1;

}

But all this does is launch NotMyFault.exe running as user Network Service (I see this from Task Manager), but it doesn't bring up help. Same thing for other arguments (" -crash").

I know this is hastily written code, but I need to see if a process running as Network Service (like mine) can do this successfully. When I run it as another user (by double clicking the executable) it seems to work just fine.

3
  • What are you expecting to happen? Of course the new process runs under the login of the creating process. Commented Mar 8, 2013 at 0:18
  • 2
    Have you tried using CreateProcess? Commented Mar 8, 2013 at 1:21
  • No luck with CreateProcess. I'll try CreateProcessWithLogonW. Commented Mar 8, 2013 at 22:01

1 Answer 1

4

Your service runs in session 0. Your interactive desktop is in a different session. So you will not see anything when your service starts a process. Because it starts in the same session as the service, in session 0.

If you want the service to start a process on the interactive desktop, that possible, albeit difficult: Launching an interactive process from Windows Service in Windows Vista and later.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. What if I don't need the interaction though? For example "Notmyfault.exe -crash" is not interactive when run from the command line (either using the system() or ShellExecuteEx() calls or running it from cmd directly. My process gets called - I see it using TaskMgr - it's just not running it with the parameter (-crash or -help, -help is admittedly interactive, but -crash shouldn't be). Or am I misunderstanding what "interactive desktop" means?
How do you detect whether or not the other process receives the parameter?
I used procmon to see how it was started and looks like it was given the complete command, but I'm not sure if it's hung or just didn't receive the parameter. Should I try CreateProcessWithLogonW?
You need to follow the instructions in the linked article

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.