Base API Reference
This section provides documentation for fundamental Windows API functions and structures that are essential for most Windows applications. These APIs cover core functionalities such as memory management, process and thread management, handles, and error handling.
Core Concepts
Understanding these concepts is crucial for effective use of the Windows API:
- Handles: Opaque objects that identify resources like windows, files, or processes.
- Data Types: Standardized data types for consistent development across the platform.
- Error Handling: Mechanisms for detecting and responding to API errors, primarily through return values and
GetLastError. - Memory Management: Functions for allocating, deallocating, and managing memory.
Key Functions
Explore essential functions for application development:
HeapAlloc
Allocates a block of memory from a specified heap.
LPVOID HeapAlloc(
HANDLE hHeap,
DWORD dwFlags,
SIZE_T dwBytes
);
Parameters
| Parameter | Description |
|---|---|
hHeap |
A handle to the heap from which the memory will be allocated. This handle is returned by the HeapCreate function. |
dwFlags |
The memory allocation attributes. This parameter can be zero or one of the following values:
|
dwBytes |
The number of bytes to allocate. |
Return Value
If the function succeeds, the return value is a pointer to the allocated memory block. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
HeapFree
Frees a block of memory previously allocated from a specified heap by the HeapAlloc or HeapReAlloc function.
BOOL HeapFree(
HANDLE hHeap,
DWORD dwFlags,
LPVOID lpMem
);
Parameters
| Parameter | Description |
|---|---|
hHeap |
A handle to the heap that contains the memory block to be freed. |
dwFlags |
Reserved; must be zero. |
lpMem |
A pointer to the memory block to be freed. This pointer was returned by a previous call to HeapAlloc or HeapReAlloc. |
Return Value
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
GetLastError
Retrieves the last error value set by a failed function call.
DWORD GetLastError(void);
Return Value
The return value is the last error code set by the calling thread. A return value of 0 is not a valid error code.
SetLastError
Sets the last-error code for the calling thread.
VOID SetLastError(
DWORD dwErrCode
);
Parameters
| Parameter | Description |
|---|---|
dwErrCode |
The error code to set. |
Remarks
Use this function to set the last-error code for a thread before returning from a function that fails. Most system services set the last-error code when they fail.
GetCurrentProcess
Retrieves a pseudo handle for the current process.
HANDLE GetCurrentProcess(void);
Return Value
The return value is a pseudo handle to the current process. This handle is unique to the calling process and has the maximum possible access rights over the process. This pseudo handle can be used only by the current process; it cannot be inherited by child processes or used in functions that require an open handle to a process.
GetCurrentThread
Retrieves a pseudo handle for the current thread.
HANDLE GetCurrentThread(void);
Return Value
The return value is a pseudo handle for the current thread. This handle is unique to the calling thread and has the maximum possible access rights over the thread. This pseudo handle can be used only by the current thread; it cannot be inherited by child threads or used in functions that require an open handle to a thread.
CreateProcess
Creates a new process and its primary thread in the calling process's address space.
BOOL CreateProcess(
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
STARTUPINFOA *lpStartupInfo,
PROCESS_INFORMATION *lpProcessInformation
);
Remarks
CreateProcess provides access to the command-line string used to create the process, environment variables, and the working directory. It also provides more control over priority class and optimizations.
ExitProcess
Terminates the calling process and all of its threads.
VOID ExitProcess(
UINT uExitCode
);
Parameters
| Parameter | Description |
|---|---|
uExitCode |
The exit code for the process. Use STILL_ACTIVE (defined as 255) if the exit code is not yet determined. |
Remarks
The system does not close the process handle of the calling process until the last thread of the process has terminated. Therefore, you can call ExitProcess from any thread in the process. The exit code can be used to retrieve process status information using the GetExitCodeProcess function.