Process Management API Reference
Key Functions
| Function | Header | Description |
CreateProcess | Windows.h | Creates a new process and its primary thread. |
OpenProcess | Windows.h | Opens an existing process object. |
TerminateProcess | Windows.h | Ends the specified process. |
GetCurrentProcess | Windows.h | Retrieves a pseudo handle for the current process. |
GetProcessId | Processthreadsapi.h | Returns the process identifier of a process handle. |
EnumProcesses | Psapi.h | Enumerates all process identifiers currently running. |
GetProcessTimes | Windows.h | Retrieves timing information for a process. |
Example: Creating a Process
#include <windows.h>
#include <stdio.h>
int main(void) {
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(si);
// Command line to execute
wchar_t cmdLine[] = L"notepad.exe";
// Create the new process
if (!CreateProcessW(
NULL, // Application name
cmdLine, // Command line
NULL, // Process security attributes
NULL, // Primary thread security attributes
FALSE, // Inherit handles
0, // Creation flags
NULL, // Environment
NULL, // Current directory
&si, // STARTUPINFO pointer
&pi)) { // PROCESS_INFORMATION pointer
wprintf(L"CreateProcess failed (error %lu)\\n", GetLastError());
return 1;
}
wprintf(L"Process ID: %lu\\n", pi.dwProcessId);
// Wait for the created process to exit
WaitForSingleObject(pi.hProcess, INFINITE);
// Cleanup
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
Related Topics