一个很好的调试函数

可以根据错误代码给出错的原因,在调试程序中可一不用查找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 SettingParameter Type
FORMAT_MESSAGE_FROM_HMODULElpSource is an hModule of the module that contains the message table to search.
FORMAT_MESSAGE_FROM_STRINGlpSource 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)))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值