dk123
dk123

Reputation: 19690

C++ Start another program and exit

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?

Upvotes: 0

Views: 4019

Answers (2)

dk123
dk123

Reputation: 19690

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;
}

Upvotes: 3

Drew Dormann
Drew Dormann

Reputation: 63775

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
             )

Upvotes: 5

Related Questions