Created
January 6, 2015 15:35
-
-
Save duguying/6c8ea7b3d8a89f8adcb7 to your computer and use it in GitHub Desktop.
windows获取最后一次错误
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static CHAR * // return error message | |
getLastErrorText( // converts "Lasr Error" code into text | |
CHAR *pBuf, // message buffer | |
ULONG bufSize) // buffer size | |
{ | |
DWORD retSize; | |
LPTSTR pTemp=NULL; | |
if (bufSize < 16) { | |
if (bufSize > 0) { | |
pBuf[0]='\0'; | |
} | |
return(pBuf); | |
} | |
retSize=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| | |
FORMAT_MESSAGE_FROM_SYSTEM| | |
FORMAT_MESSAGE_ARGUMENT_ARRAY, | |
NULL, | |
GetLastError(), | |
LANG_NEUTRAL, | |
(LPTSTR)&pTemp, | |
0, | |
NULL ); | |
if (!retSize || pTemp == NULL) { | |
pBuf[0]='\0'; | |
} | |
else { | |
pTemp[strlen(pTemp)-2]='\0'; //remove cr and newline character | |
sprintf(pBuf,"%0.*s (0x%x)",bufSize-16,pTemp,GetLastError()); | |
LocalFree((HLOCAL)pTemp); | |
} | |
return(pBuf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment