有没有办法使用 win API 获取 HRESULT 值的字符串表示形式?
win API 中是否有函数可用于提取 HRESULT 值的字符串表示形式?
问题是 MSDN 中并未记录所有返回值,例如 ExecuteInDefaultAppDomain() 函数是没有记录返回“0x80070002 - 系统找不到指定的文件。”,但是,它确实如此!因此我想知道是否有一个可以在常见情况下使用的函数。
Is there a function in win API which can be used to extract the string representation of HRESULT value?
The problem is that not all return values are documented in MSDN, for example ExecuteInDefaultAppDomain() function is not documented to return "0x80070002 - The system cannot find the file specified.", however, it does! Therefore, I was wondering whether there is a function to be used in common case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用_com_error:
如果您不想使用
_com_error
无论出于何种原因,您仍然可以查看其源代码,看看它是如何完成的。不要忘记包含标头
comdef.h
You can use _com_error:
If you don't want to use
_com_error
for whatever reason, you can still take a look at its source, and see how it's done.Don't forget to include the header
comdef.h
从 c++11 开始,此功能内置于标准库中:
Since c++11, this functionality is built into the standard library:
为此,Windows API 为 FormatMessage 。以下链接解释了如何执行此操作: 检索错误消息。
对于 Win32 消息(HRESULT 以 0x8007 开头的消息,即 FACILITY_WIN32),您需要删除 hi 命令字。例如在0x80070002中,您需要使用0x0002调用FormatMessage。
然而,它并不总是适用于任何类型的消息。而对于一些特定的消息(特定于某种技术、某个供应商等),你需要加载相应的资源DLL,这并不总是一件容易的事,因为你需要找到这个DLL。
The Windows API for this is FormatMessage. Here is a link that explains how to do it: Retrieving Error Messages.
For Win32 messages (messages with an HRESULT that begins with 0x8007, which is FACILITY_WIN32), you need to remove the hi order word. For example in the 0x80070002, you need to call FormatMessage with 0x0002.
However, it does not always work for any type of message. And for some specific messages (specific to a technology, a vendor, etc.), you need to load the corresponding resource DLL, which is not always an easy task, because you need to find this DLL.
这是使用 FormatMessage() 的示例
Here's a sample using FormatMessage()