2

I am trying to create a process from a service in C++. This new process is creating as a child process. I want to create an independent process and not a child process...

I am using CreateProcess function for the same. Since the new process i create is a child process when i try to kill process tree at the service level it is killing the child process too... I dont want this to happen. I want the new process created to run independent of the service.

Please advice on the same.. Thanks..

Code

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);    // Start the child process.
ZeroMemory( &pi, sizeof(pi) );
si.dwFlags = STARTF_USESHOWWINDOW;

if(bRunOnWinLogonDesktop)
{
    if(csDesktopName.empty())
        si.lpDesktop = _T("winsta0\\default");
    else
        _tcscpy(si.lpDesktop, csDesktopName.c_str());
}

if(bHide)
    si.wShowWindow = SW_HIDE;     /* maybe even SW_HIDE */
else
    si.wShowWindow = SW_SHOW;     /* maybe even SW_HIDE */



TCHAR szCmdLine[512];
_tcscpy(szCmdLine, csCmdLine.c_str());

if( !CreateProcess( NULL,
                szCmdLine,
               NULL,
               NULL,
               FALSE,
               CREATE_NEW_PROCESS_GROUP,
               NULL,        
               NULL,        
               &si,        
               &pi ) ) 
David Given
  • 13,277
  • 9
  • 76
  • 123
Neha
  • 21
  • 1
  • 3
  • Will `CREATE_NEW_PROCESS_GROUP` flag help? – sharptooth Mar 03 '11 at 06:17
  • duplicate? http://stackoverflow.com/questions/3136154/how-to-create-a-process-that-is-not-a-child-of-its-creating-process – young Mar 03 '11 at 06:19
  • 1
    Why are you killing the process tree in the first place? – Rob Kennedy Mar 03 '11 at 06:32
  • @sharptooth :I tried CREATE_NEW_PROCESS_GROUP but still it is created as a child of the application that i am running.. – Neha Mar 03 '11 at 07:16
  • There is a requirement of the application thatr when process tree is killed from task manager, the child process should not die – Neha Mar 03 '11 at 07:17
  • @Young : This is the solution i got from the link you suggeted.. You can try that process A create process C, which create process B and then process C will be immediatly ended (terminated). In a process B there are exist only information about the direct parent process (process Id of C which is not more running) and not about the process A. So "if A's process tree is killed" the process B will probably stay running ... HOW DO I DO THIS USING CREATEPROCESS – Neha Mar 03 '11 at 07:18
  • I can create Process A and from Process A i create Process C but now how do i create process B which will be child to C and not A? I need to do this using createProcess? – Neha Mar 03 '11 at 09:31
  • I think it worth mentioning, that you need to consider the security of such code in a service. Creating processes in session0 or the default desktop by running arbitrary command lines from an elevated service requires great care. – David Thomas Jul 05 '16 at 14:53

2 Answers2

4

After closing thread and process handlers of the child process, it's still a child in the Process Explorer, but ending parent process doesn't cause termination of the child one.

CreateProcess( NULL,
            szCmdLine,
           NULL,
           NULL,
           FALSE,
           CREATE_NEW_PROCESS_GROUP,
           NULL,        
           NULL,        
           &si,        
           &pi ); 
if(pi.hThread)
    CloseHandle(pi.hTread);
if(pi.hProcess)
    CloseHandle(pi.hProcess);

I've found this decision in sources of cmd.exe of the ReactOS, in the procedure of executing 'start' command.

Renat
  • 7,718
  • 2
  • 20
  • 34
3

Create an intermediate process (use create_new_process_group), which then creates the real process.

Service
  -> Intermediate Process
     -> Real Process

The Intermediate process should exit as soon as it's launched the real process.

Erik
  • 88,732
  • 13
  • 198
  • 189