Retrieves the last error code set by a thread.
DWORD GetLastError(void);
This function takes no parameters.
The return value is the last error code set by a thread. The error code is a system-defined value that can be used to identify the specific error that occurred.
The error code is stored on a per-thread basis. If multiple threads call functions that can set the last error code, each thread maintains its own private error code. The error code is not reset when a thread terminates.
Call the GetLastError function immediately after a function returns failure (e.g., it returns FALSE
or NULL
) to retrieve the error code. The error code is cleared by most system functions, whether they succeed or fail. Therefore, you should call GetLastError immediately after a function returns failure to get the most accurate error information.
The value returned by GetLastError is meaningful only when the return value of the function you called indicates that an error occurred. For example, if the return value of a function is TRUE
or a non-NULL
pointer, that return value indicates success, and the error code returned by GetLastError is undefined.
To get a formatted message string for the error code, use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM
flag.
The following are some common error codes that may be returned by GetLastError:
Error Code | Symbolic Name | Description |
---|---|---|
0 |
ERROR_SUCCESS |
The operation completed successfully. |
1 |
ERROR_INVALID_FUNCTION |
Incorrect function. |
2 |
ERROR_FILE_NOT_FOUND |
The system cannot find the file specified. |
3 |
ERROR_PATH_NOT_FOUND |
The system cannot find the path specified. |
5 |
ERROR_ACCESS_DENIED |
Access is denied. |
6 |
ERROR_INVALID_HANDLE |
The handle is invalid. |
87 |
ERROR_INVALID_PARAMETER |
The parameter is incorrect. |
For a complete list of error codes, refer to the System Error Codes documentation.