为什么FormatMessage()无法找到WinINet错误的消息?

T.T*_*.T. 15 c windows winapi wininet formatmessage

我正在运行它来测试FormatMessage:

LPVOID lpMsgBuf;
errCode=12163;

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM ,
    0,
    errCode,
    0,
    (LPTSTR) &lpMsgBuf,
    0, NULL );
Run Code Online (Sandbox Code Playgroud)

但是,当它返回lpMsgBuf包含NULL时......我期待像ERROR_INTERNET_DISCONNECTED这样的东西.

什么看错了?谢谢.

Sho*_*og9 26

这是一个WinINet错误,因此与它相关的消息存在于WinINet.dll中.您只需告诉FormatMessage(),以便它检索正确的消息:

FormatMessage( 
   // flags:
   FORMAT_MESSAGE_ALLOCATE_BUFFER  // allocate buffer (free with LocalFree())
   | FORMAT_MESSAGE_IGNORE_INSERTS // don't process inserts
   | FORMAT_MESSAGE_FROM_HMODULE,  // retrieve message from specified DLL
   // module to retrieve message text from
   GetModuleHandle(_T("wininet.dll")),
   // error code to look up
   errCode,
   // default language
   0, 
   // address of location to hold pointer to allocated buffer
   (LPTSTR)&lpMsgBuf, 
   // no minimum size
   0, 
   // no arguments
   NULL );
Run Code Online (Sandbox Code Playgroud)

这在WinDNet文档的"处理错误"部分的MSDN上正式记录.

请注意,FORMAT_MESSAGE_FROM_SYSTEM如果要将此例程用于可能会或可能不会来自WinINet的错误,您可以重新添加该标志:FormatMessage()如果在该位置没有找到错误,那么该标志就会回退到系统消息表中wininet.dll文件.但是,千万不能删除FORMAT_MESSAGE_IGNORE_INSERTS标志.

  • 与WinINet合作教会了我很多我不想特别知道的事情.:-( (4认同)