SetLastError

  VOID SetLastError(dwErrCode)    
  DWORD dwErrCode;    

The SetLastError function sets an error code and error string for the thread that calls it.

Parameters

dwErrCode

Specifies the error code to store in per thread storage for the current thread.

Comments

Each thread stores just one error code and error string. The code and string stored for a thread represent the most recent error.

Win32 functions that return indications of failure call SetLastError when they fail. They do not call it when they succeed. Thus, if three Win32 function calls are made, and the first one fails and the second two succeed, the error code and string stored by the first one are still available after the second two succeed.

SetLastError is intended primarily for DLLs, not application code.

Applications can retrieve the values saved by this function using GetLastError. The use of GetLastError is optional, as an application need only call if it is interested in knowing the specific reason for a function failure.

The last error code value is kept in thread local storage so that multiple threads do not overwrite each other's values.

See Also

GetLastError