Return to Snippet

Revision: 13845
at May 7, 2009 15:42 by jimfred

Initial Code
static void MyExec( LPCSTR cmdArg, LPCSTR cwdArg )
   STARTUPINFO         si = {sizeof(STARTUPINFO)};

   BOOL bOk = CreateProcess(
      NULL,   // No module name (use command line)
      (LPSTR)cmdArg, // Command line
      NULL,   // Process handle not inheritable
      NULL,   // Thread handle not inheritable
      FALSE,  // Set handle inheritance to FALSE
      0,      // No creation flags
      NULL,   // Use parent's environment block
      (LPSTR)cwdArg,   // Use parent's starting directory 
      &si,    // Pointer to STARTUPINFO structure
      &pi     // Pointer to PROCESS_INFORMATION structure

   // Wait until child process exits.
   WaitForSingleObject( pi.hProcess, INFINITE );

   // Close process and thread handles. 
   CloseHandle( pi.hProcess );
   CloseHandle( pi.hThread );

   if ( bOk ) { return; }

   CString s;
   s.Format( "Unable to execute '%s'", cmdArg );
   MessageBox( NULL, s, APP_NAME, 0 );

Initial URL

Initial Description
I occasionally need a WinExec function the synchronously executes a command. cwdArg may be null. 

This example, upon error, pops-up a dialog and exits the app. This is useful for small installation utilities., with or without MFC.

Initial Title
MyExec, calls CreateProcess, waits for completion. WinBase

Initial Tags

Initial Language