Process Functions

API Reference | Process Management

This section provides detailed information on functions used for managing processes in the Windows operating system.

CreateProcess

BOOL CreateProcess( LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation );

Creates a new process and its primary thread. The new process runs in the same address space of the calling process.

Parameters

  • lpApplicationName: The name of the module to be executed.
  • lpCommandLine: The command line string to be executed.
  • lpProcessAttributes: Security attributes for the new process.
  • lpThreadAttributes: Security attributes for the new thread.
  • bInheritHandles: Specifies whether the new process inherits handles.
  • dwCreationFlags: Flags that control the priority class and the way the process is created.
  • lpEnvironment: A block of environment variables for the new process.
  • lpCurrentDirectory: The fully qualified path of the current directory for the new process.
  • lpStartupInfo: A pointer to a STARTUPINFO structure that specifies how to display the main window for the new process.
  • lpProcessInformation: A pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Remarks

Use CloseHandle to close the handles specified in lpProcessInformation when they are no longer needed.

TerminateProcess

BOOL TerminateProcess( HANDLE hProcess, UINT uExitCode );

Terminates the specified process and all of its threads.

Parameters

  • hProcess: A handle to the process to be terminated.
  • uExitCode: The exit code for the process.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Remarks

TerminateProcess is a forceful way to end a process. It does not allow the process to run any special code to shut down, such as cleanup handlers or shutdown notification handlers.

GetCurrentProcess

HANDLE GetCurrentProcess(void);

Returns a pseudo-handle for the current process. A pseudo-handle is a special constant that is equivalent to the actual handle of the process.

Parameters

This function takes no parameters.

Return Value

The return value is a pseudo-handle to the current process.

Remarks

You can use the pseudo-handle returned by GetCurrentProcess in any function that requires a handle to the process, such as the access-control functions. The pseudo-handle is valid only within the context of the current process. It cannot be duplicated or inherited by another process.

GetProcessId

DWORD GetProcessId( HANDLE Process );

Retrieves the process identifier (PID) for the specified process.

Parameters

  • Process: A handle to the process.

Return Value

If the function succeeds, the return value is the process identifier of the specified process. If the function fails, the return value is zero.

OpenProcess

HANDLE OpenProcess( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId );

Retrieves a handle to an access token associated with the specified process.

Parameters

  • dwDesiredAccess: The access to the process object.
  • bInheritHandle: If this parameter is TRUE, the handle inherits the same security attributes that the calling process inherited when the process was created.
  • dwProcessId: The identifier of the process to open.

Return Value

If the function succeeds, the return value is an open handle to the specified process. If the function fails, the return value is NULL.

Remarks

Do not forget to close the handle with CloseHandle when it is no longer needed.

EnumProcesses

BOOL EnumProcesses( DWORD* lpidProcess, DWORD cb, DWORD* lpcbNeeded );

Retrieves the process identifiers for each process running on the local computer.

Parameters

  • lpidProcess: A pointer to an array that receives a list of process identifiers.
  • cb: The size of the lpidProcess array, in bytes.
  • lpcbNeeded: A pointer to a variable that receives the number of bytes required to store all process identifiers on the system.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

See Also