CreateProcess示例

// Start the child process 
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

TCHAR szCommandLine[] = TEXT("notepad.exe");

if(!CreateProcess(NULL,           // No module name (use command line)
                  szCommandLine,  // 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
                  NULL,           // Use parent's starting directory 
                  &si,            // Pointer to STARTUPINFO structure
                  &pi)            // Pointer to PROCESS_INFORMATION structure
) 
{
    AfxMessageBoxFormatted(TEXT("Create process failed: %d"), GetLastError());
    return;
}

// Wait until child process exits
DWORD dwResult = WaitForSingleObject(pi.hProcess, INFINITE);
switch (dwResult)
{
case WAIT_FAILED:
    AfxMessageBoxFormatted(TEXT("Bad call to function WaitForSingleObject, maybe for invalid handle."));
    break;
case WAIT_OBJECT_0:
    AfxMessageBoxFormatted(TEXT("Child process exits."));
    break;
default:
    break;
}

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

你可能感兴趣的:(C++,c,Win32)