【问题标题】:Why is FormatMessage() failing to find a message for WinINet errors?为什么 FormatMessage() 找不到 WinINet 错误消息?
【发布时间】:2011-01-10 16:40:44
【问题描述】:

我正在运行它来测试FormatMessage

LPVOID lpMsgBuf;
errCode=12163;

FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM ,
    0,
    errCode,
    0,
    (LPTSTR) &lpMsgBuf,
    0, NULL );

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

看起来有什么问题吗?谢谢。

【问题讨论】:

    标签: c windows winapi wininet formatmessage


    【解决方案1】:

    这是一个 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 );
    

    这在 MSDN 上正式记录在 WinINet 文档的 "Handling Errors" section 下。

    请注意,如果您想将此例程用于可能或可能不是来自 WinINet 的错误,则可以重新添加 FORMAT_MESSAGE_FROM_SYSTEM 标志:使用该标志,FormatMessage()如果在 wininet.dll 中未找到错误,将退回到系统消息表。但是,do not remove the FORMAT_MESSAGE_IGNORE_INSERTS flag

    【讨论】:

    • 与 WinINet 的合作教会了我很多我以前并不特别想知道的事情。 :-(
    猜你喜欢
    • 2016-10-03
    • 2016-09-11
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多