/ Published in: C++
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.
non-dot.net, with or without MFC.
This example, upon error, pops-up a dialog and exits the app. This is useful for small installation utilities.
non-dot.net, with or without MFC.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
static void MyExec( LPCSTR cmdArg, LPCSTR cwdArg ) { STARTUPINFO si = {sizeof(STARTUPINFO)}; PROCESS_INFORMATION pi = {0}; 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 ); exit(-1); }