Creating independent process!

2020-04-17 04:15发布

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

2条回答
何必那么认真
2楼-- · 2020-04-17 04:33

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.

查看更多
Fickle 薄情
3楼-- · 2020-04-17 04:45

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.

查看更多
登录 后发表回答