Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

c++ - Creating your own 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?

EDIT

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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

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

#include "errors.rc"

in my main .rc file for the project.

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
.

with lots of error numbers defined.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...