Process Management APIs
This section details the Windows API functions and structures used for managing processes, including creation, termination, and querying information.
Creating Processes
Functions for creating new processes and threads.
CreateProcess
Creates a new process and its primary thread. The new process runs in the same address space as the calling process.
BOOL CreateProcess(
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
Parameters:
| Parameter | Type | Description |
|---|---|---|
lpApplicationName |
LPCSTR | The name of the module to be executed. |
lpCommandLine |
LPSTR | The command line string for the process. |
lpProcessAttributes |
LPSECURITY_ATTRIBUTES | Security attributes for the new process object. |
lpThreadAttributes |
LPSECURITY_ATTRIBUTES | Security attributes for the new thread object. |
bInheritHandles |
BOOL | If TRUE, the child process inherits copies of the handles in the calling process. |
dwCreationFlags |
DWORD | Flags that control the priority class and behavior of the new process. |
lpEnvironment |
LPVOID | A block of memory containing a new environment for the child process. |
lpCurrentDirectory |
LPCSTR | The full path of the current directory for the process. |
lpStartupInfo |
LPSTARTUPINFOA | Startup information for the new process. |
lpProcessInformation |
LPPROCESS_INFORMATION | Receives information about the new process and its primary thread. |
CreateProcessAsUser
Creates a new process and its primary thread. The new process runs in the security context of the user specified by the lpUsername parameter.
BOOL CreateProcessAsUser(
PSID lpSid,
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
CreateThread
Creates a new thread within the address space of the calling process.
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
Terminating Processes
Functions for ending processes and threads.
TerminateProcess
Terminates the specified process and any threads that it created.
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);
ExitProcess
This function is used by a process to terminate itself.
VOID ExitProcess(
UINT uExitCode
);
Querying Process Information
Functions to retrieve details about running processes.
GetProcessId
Retrieves the process identifier of the specified process.
DWORD GetProcessId(
HANDLE ProcessHandle
);
GetProcessTimes
Retrieves timing information for the specified process.
BOOL GetProcessTimes(
HANDLE hProcess,
LPFILETIME lpCreationTime,
LPFILETIME lpExitTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime
);
EnumProcesses
Enumerates the process identifiers for all processes currently running on the local computer.
BOOL EnumProcesses(
LPDWORD lpidProcess,
DWORD cb,
LPDWORD lpcbNeeded
);
Process Structures
Key data structures used with process management APIs.
PROCESS_INFORMATION
Contains information about a newly created process and its primary thread. This structure is used by the CreateProcess function.
typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
STARTUPINFO
Specifies the window station, standard handles, and appearance of the main window for a process at creation time.
typedef struct _STARTUPINFO {
DWORD cb;
LPSTR lpReserved;
LPSTR lpDesktop;
LPSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFO, *LPSTARTUPINFO;