可以根据错误代码给出错的原因,在调试程序中可一不用查找errocode而获得对错误代码的解释,
是一个很实用的函数。
控制台版:调用实例 :PRINTDEBUG(GetLastError())
PRINTDEBUG(WSAGetLastError());
#define PRINTDEBUG(a) PrintError(#a,__FILE__,__LINE__,GetLastError())
__inline int PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
{
LPSTR lpBuffer;
char errbuf[256];
DWORD numread;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errnum,
LANG_NEUTRAL,
(LPTSTR)&lpBuffer,
0,
NULL );
LocalFree(lpBuffer)
wsprintf(errbuf, "/nThe following call failed at line %d in %s:/n/n"
" %s/n/nReason: %s/n", lineno, filename, linedesc, lpBuffer);
//写屏的操作
WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, strlen(errbuf), &numread, FALSE );
return errnum;
}
MFC版: 调用实例:PRINTDEBUG(FALSE);
#define PRINTDEBUG(a) PrintError(#a,__FILE__,__LINE__,GetLastError())
int PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
{
LPSTR lpBuffer;
char errbuf[256];
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errnum,
LANG_NEUTRAL,
(LPTSTR)&lpBuffer,
0,
NULL );
LocalFree(lpBuffer);
wsprintf(errbuf, "/nThe following call failed at line %d in %s:/n/n"
" %s/n/nReason: %s/n", lineno, filename, linedesc, lpBuffer);
AfxMessageBox(errbuf);
return errnum;
}
另:
FormatMessage的解释:
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
Specifies that the lpBuffer parameter is a pointer to a PVOID pointer, and that the nSize parameter specifies the minimum number of bytes (ANSI version) or characters (Unicode version) to allocate for an output message buffer. The function allocates a buffer large enough to hold the formatted message, and places a pointer to the allocated buffer at the address specified by lpBuffer. The caller should use the LocalFree function to free the buffer when it is no longer needed. |
FORMAT_MESSAGE_FROM_SYSTEM | |
Specifies that the function should search the system message-table resource(s) for the requested message. If this flag is specified with FORMAT_MESSAGE_FROM_HMODULE, the function searches the system message table if the message is not found in the module specified by lpSource. Cannot be used with FORMAT_MESSAGE_FROM_STRING. If this flag is specified, an application can pass the result of the GetLastError function to retrieve the message text for a system-defined error. |
dwFlags Setting | Parameter Type |
---|---|
FORMAT_MESSAGE_FROM_HMODULE | lpSource is an hModule of the module that contains the message table to search. |
FORMAT_MESSAGE_FROM_STRING | lpSource is an LPTSTR that points to unformatted message text. It will be scanned for inserts and formatted accordingly. |
If neither of these flags is set in dwFlags, then lpSource is ignored.
//其他小结::
//给出一个结构体成员的偏移地址
// Calculate the byte offset of a field in a structure of type type.
//
#define FIELD_OFFSET(type, field) ((LONG)(INT_PTR)&(((type *)0)->field))
//由结构体成员的地址给出结构体的地址
// Calculate the address of the base of the structure given its type, and an
// address of a field within the structure.
//
#define CONTAINING_RECORD(address, type, field) ((type *)( /
(PCHAR)(address) - /
(UINT_PTR)(&((type *)0)->field)))