SetLastError function
Namespace: Win32
Sets the last-error code for the calling thread.
Syntax
VOID SetLastError(
DWORD dwErrCode
);
Parameters
dwErrCode
- The error code to be set for the calling thread. Use the
GetLastError
function to retrieve the error code later.
Return value
This function does not return a value.
Remarks
- The error code set by
SetLastError
is thread‑local; each thread maintains its own last‑error value. - Use
GetLastError
to retrieve the error code that was set. - Most Windows API functions set the last‑error value on failure, but it should not be used for normal program flow.
Example
#include <windows.h>
#include <stdio.h>
int main(void)
{
// Force an error
SetLastError(ERROR_FILE_NOT_FOUND);
DWORD err = GetLastError();
printf("Last error: %lu\\n", err);
return 0;
}