为什么 FormatMessage() 无法找到 WinINet 错误的消息? [英] Why is FormatMessage() failing to find a message for WinINet errors?
问题描述
我运行它是为了测试 FormatMessage一个>:
I'm running this to test 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.
However, when it returns lpMsgBuf
contains NULL... I was expecting something like ERROR_INTERNET_DISCONNECTED.
有什么不对吗?谢谢.
推荐答案
这是一个 WinINet 错误,因此与其关联的消息位于 WinINet.dll 中.你只需要告诉 FormatMessage() 以便它检索正确的消息:
That's a WinINet error, and so the message associated with it lives in WinINet.dll. You just need to tell FormatMessage() about this in order for it to retrieve the correct message:
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 文档的部分.
This is officially documented on MSDN under the "Handling Errors" section of the WinINet documentation.
请注意,如果您想将此例程用于可能或可能不是来自 WinINet 的错误,您可以重新添加 FORMAT_MESSAGE_FROM_SYSTEM
标志:在该标志中如果在 wininet.dll 中没有发现错误,FormatMessage()
将回退到系统消息表.但是,不要删除 FORMAT_MESSAGE_IGNORE_INSERTS 标志.
Note that you can add the FORMAT_MESSAGE_FROM_SYSTEM
flag back in if you want to use this routine for errors that may or may not have come from WinINet: with that flag in place, FormatMessage()
will fall back on the system message table if the error isn't found in wininet.dll. However, do not remove the FORMAT_MESSAGE_IGNORE_INSERTS flag.
这篇关于为什么 FormatMessage() 无法找到 WinINet 错误的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!