Platform SDK: Debugging and Error Handling

Retrieving the Last-Error Code

When many system functions fail, they set the last-error code (check the documentation for the function). If your application needs more details about an error, it can retrieve the last-error code.

The following example shows an error-handling function.

void error(LPSTR lpszFunction) 
{ 
    CHAR szBuf[80]; 
    DWORD dw = GetLastError(); 
 
    sprintf(szBuf, "%s failed: GetLastError returned %u\n", 
        lpszFunction, dw); 
 
    MessageBox(NULL, szBuf, "Error", MB_OK); 
    ExitProcess(dw); 
}