0

to launch an exe, I have wrote the CreateProcess of this thread How do I open an .exe from another C++ .exe?

this works but if my first program end the cmd will close and i want to keep my launched program running

my code looks like that

void Client::StartServer(){
    //init process structures 
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // get working directory
    char tmp[512];
    GetModuleFileName(NULL, tmp, 512);
    stringstream sstmp;
    string stmp;

    sstmp << tmp;
    stmp = sstmp.str();
    
    //remove name.exe from path
    while(stmp.back() != '\\'){
        stmp.pop_back();
    }
    
    //launch
    sstmp.str("");
    sstmp << stmp << "Sim_Server.exe";
    cout << "start : " << sstmp.str() << " end "<< endl;
    LPCTSTR path =  sstmp.str().c_str(); 

    if( !CreateProcess(path,NULL,NULL,NULL,false,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi) ){
        throw runtime_error("unable to launch server");
    }
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Your OS will close a program after it finishes. So if this is a console program and it finishes expect the cmd.exe window to go away. – drescherjm Mar 22 '22 at 12:39
  • i have the same on python with subprocess.Popen(exepath, cretionflag.DETACHED_PROCESS, shell=True) and the second exe is not closed when th mother program ends –  Mar 22 '22 at 12:43
  • If you wish to wait on your child process, then maybe you should wait on your child process. [`WaitForSingleObject`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject) is one way to do that. – IInspectable Mar 22 '22 at 12:49
  • this mean the mother terminal will not return if the child program is not ended ? on the python example , the terminal return even if the second exe is not closed –  Mar 22 '22 at 12:55
  • Have a look at [Creating Processes](https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes). Also according to [How Processes are Terminated](https://learn.microsoft.com/en-us/windows/win32/procthread/terminating-a-process#how-processes-are-terminated), Child process exists after Parent process terminated is a expected behavior. – YangXiaoPo-MSFT Mar 23 '22 at 02:02
  • finally i use system("start my.exe") even if not recomended –  Mar 23 '22 at 12:53

0 Answers0