DWORD GetLastError( )函数是用来返回由上一个函数调用设置的线程32位错误代码,WinError.h头文件里包含了Microsoft定义的错误代码,如下所示:
#define ERROR_SUCCESS 0L
#define NO_ERROR 0L // dderror
#define SEC_E_OK ((HRESULT)0x00000000L)
//
// MessageId: ERROR_INVALID_FUNCTION
//
// MessageText:
//
// Incorrect function.
//
#define ERROR_INVALID_FUNCTION 1L // dderror
//
// MessageId: ERROR_FILE_NOT_FOUND
//
// MessageText:
//
// The system cannot find the file specified.
//
#define ERROR_FILE_NOT_FOUND 2L
//
// MessageId: ERROR_PATH_NOT_FOUND
//
// MessageText:
//
// The system cannot find the path specified.
//
#define ERROR_PATH_NOT_FOUND 3L
//
// MessageId: ERROR_TOO_MANY_OPEN_FILES
//
// MessageText:
//
// The system cannot open the file.
//
#define ERROR_TOO_MANY_OPEN_FILES 4L
//
// MessageId: ERROR_ACCESS_DENIED
//
// MessageText:
//
// Access is denied.
//
#define ERROR_ACCESS_DENIED 5L
//
// MessageId: ERROR_INVALID_HANDLE
//
// MessageText:
//
// The handle is invalid.
//
#define ERROR_INVALID_HANDLE 6L
//
// MessageId: ERROR_ARENA_TRASHED
//
// MessageText:
//
// The storage control blocks were destroyed.
//
#define ERROR_ARENA_TRASHED 7L
//
// MessageId: ERROR_NOT_ENOUGH_MEMORY
//
// MessageText:
//
// Not enough storage is available to process this command.
//
#define ERROR_NOT_ENOUGH_MEMORY 8L // dderror
//
// MessageId: ERROR_INVALID_BLOCK
//
// MessageText:
//
// The storage control block address is invalid.
//
#define ERROR_INVALID_BLOCK 9L
//
// MessageId: ERROR_BAD_ENVIRONMENT
//
// MessageText:
//
// The environment is incorrect.
//
#define ERROR_BAD_ENVIRONMENT 10L
在这里,我写了一个小程序,先创建一个文件,以打开的模式创建,如果不存在该文件,会返回错误代码,代码以及输出结果如下所示;
HANDLE hFile = CreateFile(TEXT("d:\\wang"),0,0,NULL,OPEN_EXISTING,0,NULL ) ;
DWORD dwRet = GetLastError();
_tprintf(TEXT("dwRet = %u\n"), dwRet );
CloseHandle( hFile );
dwRet =2 查找对应的错误原因,可知是ERROR_FILE_NOT_FOUND。可通过FormatMessage对错误代码进行格式化,使错误简洁明了,代码如下:
HANDLE hFile = CreateFile(TEXT("d:\\wang"),0,0,NULL,OPEN_EXISTING,0,NULL ) ;
DWORD dwRet = GetLastError();
_tprintf(TEXT("dwRet = %u\n"), dwRet );
LPVOID lpDisplayBuf;
LPVOID lpMsgBuf ;
DWORD systemLocal = MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT );
BOOL bOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,dwRet,systemLocal,(LPTSTR)&lpMsgBuf,0,NULL );
if ( !bOk )
{
HMODULE hmDll = LoadLibraryEx( TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES );
if( hmDll != NULL )
{
bOk = FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_ALLOCATE_BUFFER,hmDll,dwRet,systemLocal,(LPTSTR)&lpMsgBuf,0,NULL );
FreeLibrary( hmDll );
}
}
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf),
TEXT("failed with error %d: %s"),
dwRet, lpMsgBuf);
if( bOk && ( lpMsgBuf != NULL ) )
{
_tprintf(TEXT("%s\n"), (LPCTSTR)lpDisplayBuf );
LocalFree( lpMsgBuf );
LocalFree( lpDisplayBuf );
}
else
{
_tprintf( TEXT(" No text found for this error number\n"));
}
CloseHandle( hFile );
lpDisplayBuf就是显示的错误信息。