
Is it a good idea to create HRESULT for my COM or not? If yes, How?
Look up HRESULT in MSDN. Here's an excerpt:
a.. "Return an HRESULT value for all methods in all component interfaces.
COM+ uses HRESULTs to report on any errors in making function calls or
interface method calls. An HRESULT indicates whether a method succeeded or
failed and identifies the facility associated with the error, such as RPC,
WIN32, or ITF for interface-specific errors. Also, system APIs provide a
lookup from an HRESULT to a string that describes the error condition.
Using methods that return HRESULTs are fundamental to well-written
components and are essential to the debugging process. Microsoft? Visual
Basic? automatically defines each method with an HRESULT as a return. In
Microsoft Visual C++?, you must explicitly return an HRESULT.
For additional information on HRESULTs, see Structure of COM Error Codes in
the COM documentation in the Platform SDK."
You should use MAKE_HRESULT like this:
const HRESULT ERROR_NUMBER =
MAKE_HRESULT (SEVERITY_ERROR, FACILITY_ITF, YourCustomErrCodeHere);
You define your error codes in a certain range (so as not to conflict with
existing Windows codes.) I can't remember what the range is.
You can also use the ErrorInfo object to convey additional information about
the error. From the MSDN:
"ErrorInfo objects are often called COM exceptions because they allow an
object to pass (or throw) rich error information to its caller, even across
apartment boundaries. The value of this generic error object is that it
supplements an HRESULT, extending the type of error information that you can
return to a caller. Each ErrorInfo object returns a contextual description,
the source of the error, and the interface identifier of the method that
originated the error. You can also include pointers to an entry in a help
file."
Quote:
> Hello,
> I have checked my book and it says that it is a bad idea to create new
> HRESULT and should use the standard HRESULT.
> But I have specific error information and want to notify client. If I
don't
> use the HRESULT. I have to add a new parameter to every interface's member
> function. Then the client should check HRESULT first, then this parameter
> secondly.
> How can I create my own HRESULT? For which scope I can use?
> --
> kind regards/chenyu