建立一個程序並檢查其退出程式碼
此示例啟動記事本,等待它關閉,然後獲取其退出程式碼。
#include <Windows.h>
int main()
{
STARTUPINFOW si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
// Create the child process
BOOL success = CreateProcessW(
L"C:\\Windows\\system32\\notepad.exe", // Path to executable
NULL, // Command line arguments
NULL, // Process attributes
NULL, // Thread attributes
FALSE, // Inherit handles
0, // Creation flags
NULL, // Environment
NULL, // Working directory
&si, // Startup info
&pi); // Process information
if (success)
{
// Wait for the process to exit
WaitForSingleObject(pi.hProcess, INFINITE);
// Process has exited - check its exit code
DWORD exitCode;
GetExitCodeProcess(pi.hProcess, &exitCode);
// At this point exitCode is set to the process' exit code
// Handles must be closed when they are no longer needed
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
參考文獻(MSDN):