一个很好的调试函数

可以根据错误代码给出错的原因,在调试程序中可一不用查找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.

lpSource
Specifies the location of the message definition. The type of this parameter depends upon the settings in the dwFlags parameter.
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.

dwMessageId
Specifies the 32-bit message identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.
lpBuffer
Pointer to a buffer for the formatted (and null-terminated) message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc function, and places the pointer to the buffer at the address specified in lpBuffer.
nSize
If the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the maximum number of bytes (ANSI version) or characters (Unicode version) that can be stored in the output buffer. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of bytes or characters to allocate for an output buffer.

//其他小结::

//给出一个结构体成员的偏移地址
// 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)))

你可能感兴趣的:(一个很好的调试函数)