创建自己的 HRESULT?

2022-01-14 00:00:00 com c++ hresult

我已经有一个使用大量 COM 和 HRESULTS 的项目.无论如何,我想知道是否可以定义自己的 HRESULT,并且能够将 FormatMessage() 用于我们自己的 HRESULT?

I already have a project that uses a lot of COM, and HRESULTS. Anyways I was wondering if it's possible to define your own HRESULT, AND be able to use the FormatMessage() for our own HRESULT?

我翻遍了,找不到任何东西.有什么想法吗?

I dug around and can't find anything. Any ideas?

编辑

基本上我想定义一组我自己的 HRESULT,而不是仅仅返回 E_FAIL.或其他通用的之一.像 E_FAIL 就可以了.但假设我想指出,例如地理处理子系统崩溃或文件是无效的光栅图像.该应用程序已经在整个过程中使用了 COM.

Basically I want to define a set of my own HRESULTs instead of just returning E_FAIL. Or one of the other generic ones. Like E_FAIL is fine. But let's say I want to point out that for example the Geoprocessing subsystem crashed or the file is an invalid Raster Image. The application already uses COM throughout it.

推荐答案

当然可以.通常,您会创建一个 .mc 文件并包含在你的项目中.指示 mc 编译器 构建它 - 这个创建一个头文件和一个 .rc 文件.HRESULTS 在头文件中定义.您在项目中包含 .rc 文件,以供资源编译器编译 - 这会将消息定义放入您的最终模块中.然后您可以使用普通的 FormatMessage 函数使用 HRESULTS 格式化消息并生成错误信息和其他内容.

Yes of course. Typically you create a .mc file and include that in your project. Instruct the mc compiler to build it - this creates a header file and a .rc file. The HRESULTS are defined in the header file. You include the .rc file in your project as normal for the resource compiler to compile - this puts the message definitions into your final module. Then you can use the normal FormatMessage functions to format the messages using the HRESULTS and generate error info and the other stuff.

我将此作为我的 .mc 文件之一的命令行:

I have this as the command line for one of my .mc files:

mc   -h "../include" -r "../include" "..includeerrors.mc"

这会在包含目录中创建 errors.rc 和 errors.h.然后我做了:

This creates errors.rc and errors.h in the include directory. Then I did:

#include "errors.rc"

在我的项目主 .rc 文件中.

in my main .rc file for the project.

.mc 文件看起来有点像这样:

The .mc file looks a bit like this:

LanguageNames=(English=0x409:MSG00409)

MessageId=0x1
SymbolicName=SOME_CATEGORY
Language=English
Some Category
.

MessageID=
Severity=Error
SymbolicName=ERROR_INVALID_PROP_INDEX

Language=English
Invalid property index %1
.

定义了很多错误号.

相关文章