0

I'm trying to create a program where it starts another .exe and itself just closes after the other program has started.

I currently have the following code:

#include <cstdlib>

int main( )
{
    std::system( "checkpoint.exe" );
}

I can get checkpoint.exe to start, but the starter program itself doesn't close until checkpoint.exe closes. How would I get around this?

dk123
  • 18,684
  • 20
  • 70
  • 77
  • 3
    you need to start a separate process which won't block your program execution, see this [question](http://stackoverflow.com/questions/1067789/how-to-create-a-process-in-c-on-windows) – sled Jan 25 '15 at 04:22
  • Some systems maintain a process hierarchy. Each process (except for the first) is a child of anther process. You are trying to destroy that kind of hierarchy. Some systems have the ability to create detached processes. It is likely then that you will have to use an operating system specific call to create such a process. – user3344003 Jan 25 '15 at 04:25
  • 1
    @Axalo: seems like he's using windows though, so no fork :/ – sled Jan 25 '15 at 04:30

2 Answers2

3

Since you appear to be using Windows, you can use CreateProcess

LPSTARTUPINFO lpStartupInfo;
LPPROCESS_INFORMATION lpProcessInfo;

memset(&lpStartupInfo, 0, sizeof(lpStartupInfo));
memset(&lpProcessInfo, 0, sizeof(lpProcessInfo));

CreateProcess("checkpoint.exe"
              NULL, NULL, NULL,
              NULL, NULL, NULL, NULL,
              lpStartupInfo,
              lpProcessInfo
             )
Teivaz
  • 5,462
  • 4
  • 37
  • 75
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 2
    MSDN also has some good [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863%28v=vs.85%29.aspx) about the various process creation flags :) – sled Jan 25 '15 at 04:36
3

The previous answer has some bugs. You have to initialize STARTUPINFO and PROCESS_INFORMATION structs, not LPSTARTUPINFO or LPPROCESS_INFORMATION. The latter are just pointers to the structs.

Here's a working solution:

#include <cstdlib>
#include <Windows.h>
    
int main( )
{    
    STARTUPINFO StartupInfo;
    PROCESS_INFORMATION ProcessInfo;
    
    ZeroMemory( &StartupInfo, sizeof( StartupInfo ) );
    StartupInfo.cb = sizeof( lpStartupInfo );
    ZeroMemory( &ProcessInfo, sizeof( ProcessInfo ) );
    
    CreateProcess( "Program.exe",
                   NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL,
                   &StartupInfo,
                   &ProcessInfo
                   );
   
    return 0;
}
jyl
  • 401
  • 3
  • 12
dk123
  • 18,684
  • 20
  • 70
  • 77