Processes and Threads

This section provides comprehensive documentation for the Windows API functions related to process and thread management.

Process Creation

Creating new processes is a fundamental operation in Windows. You can launch new applications or instances of the current application using functions like CreateProcess.

CreateProcess

Signature:

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
);

This function creates a new process and its primary thread. It allows for extensive configuration of the new process, including command-line arguments, environment variables, and security attributes.

ShellExecute

Signature:

HINSTANCE ShellExecute(
  HWND   hwnd,
  LPCTSTR op,
  LPCTSTR file,
  LPCTSTR params,
  LPCTSTR dir,
  INT    showCmd
);

A higher-level API for executing programs or opening documents. It can handle file associations and verb execution.

Process Information

Once a process is running, you might need to retrieve information about it, such as its identifier, priority, or state.

GetCurrentProcessId

Signature:

DWORD GetCurrentProcessId(void);

Returns the process identifier of the calling process.

GetProcessId

Signature:

DWORD GetProcessId(
  HANDLE Process
);

Retrieves the process identifier for the specified process handle.

GetExitCodeProcess

Signature:

BOOL GetExitCodeProcess(
  HANDLE hProcess,
  LPDWORD lpExitCode
);

Retrieves the exit code of the specified process.

Process Termination

Processes can be terminated explicitly or by exiting their main thread.

TerminateProcess

Signature:

BOOL TerminateProcess(
  HANDLE hProcess,
  UINT   uExitCode
);

This function terminates the specified process and any threads that are running in it. It is generally recommended to allow processes to exit gracefully when possible.

Thread Creation

Threads allow for concurrent execution within a single process. You can create new threads using CreateThread.

CreateThread

Signature:

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES   lpThreadAttributes,
  SIZE_T                  dwStackSize,
  LPTHREAD_START_ROUTINE  lpStartAddress,
  LPVOID                  lpParameter,
  DWORD                   dwCreationFlags,
  LPDWORD                 lpThreadId
);

Creates a new thread to execute within the virtual address space of the calling process.

Thread Information

Retrieve information about threads, such as their current state or priority.

GetCurrentThreadId

Signature:

DWORD GetCurrentThreadId(void);

Returns the thread identifier of the calling thread.

GetThreadPriority

Signature:

int GetThreadPriority(
  HANDLE hThread
);

Retrieves the priority of the specified thread.

Thread Termination

Threads can exit by returning from their start routine or by explicit termination.

ExitThread

Signature:

VOID ExitThread(
  DWORD dwExitCode
);

Terminates the calling thread and provides an exit code.

Job Objects

Job objects allow you to group a set of processes and manage them as a unit. You can apply limits on CPU time, memory usage, and more.

CreateJobObject

Signature:

HANDLE CreateJobObject(
  LPSECURITY_ATTRIBUTES lpJobAttributes,
  LPCTSTR               lpName
);

Creates, opens, or truncates a job object.

← Kernel Memory Management →