locked
Calling CreateProcessAsUser() from XP NT service RRS feed

  • Question

  • I wrote a NT service 'S' which will launch an instance of 'P'(an application) in each session logged in. I do this in the following way:

    * enumerate all processes running in the system

    * check the session id of each process: OpenProcess, OpenProcessToken, GetTokenInformation(..., TokenSessionId, ...)

    * if a new session found, CreateProcessAsUser with the token opened just now

    It works well when ONLY ONE session is logged in. If a secondary session is logged in, CreateProcessAsUser will be failed with error code 87 when attempting to launch 'P'.

    The snippet of my code:

     

    static void Spawn(const std::basic_string<TCHAR>& sAppName, __inout SessionInfo& info, TCHAR* lpCommandLine = NULL)
    {
    	STARTUPINFO si = {0};
    	si.cb = sizeof(si);
    	si.lpDesktop = _T("winsta0\\default");
    	memset(&info.pi, 0, sizeof(PROCESS_INFORMATION));
    
    	if (!::CreateProcessAsUser(info.hToken,
    							 sAppName.c_str(),
    							 lpCommandLine,
    							 NULL,
    							 NULL,
    							 TRUE,
    							 NORMAL_PRIORITY_CLASS,
    							 NULL,
    							 NULL,
    							 &si,
    							 &info.pi))
    	{
    		write_log("CreateProcessAsUser Error: %d, token: 0x%08X, app name: %ws, command line: %ws\r\n",
    			::GetLastError(), info.hToken, sAppName.c_str(), lpCommandLine);
    		return;
    	}
    
    	::CloseHandle(info.pi.hThread);
    	info.pi.hThread = NULL;
    }
    

     

    87 = ERROR_INVALID_PARAMETER

    How to solve this?

    Thanks for reading my poor English...

    • Edited by Kenneth_Wang Thursday, April 14, 2011 5:17 AM merge a following post
    Thursday, April 14, 2011 3:30 AM

Answers

  • I have found the answer...

    It turns out to be:

    Terminal Services: You cannot inherit handles across sessions. Additionally, if this parameter is TRUE, you must create the process in the same session as the caller.


    • Marked as answer by Kenneth_Wang Friday, April 15, 2011 9:10 AM
    • Edited by Kenneth_Wang Friday, April 15, 2011 9:10 AM mistyping
    Friday, April 15, 2011 9:10 AM