本文介绍了为什么的FormatMessage()未找到错误的WinINet的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行这个测试:

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.

请注意,你可以回来,如果你想使用这个例程可能会或可能的的有来自错误添加 FORMAT_MESSAGE_FROM_SYSTEM 标志的WinINet:在地方的标志,的FormatMessage()将回落在系统消息表如果wininet.dll文件未找到错误。但是,。

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的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:42