COM development picks up
5. Customized error code? HRESULT? abnormal?
Error handling in COM can have a variety of options, such as the [OUT, RETVAL] parameter using the method returns a custom error code; or returns a standard and custom HRESULT value; throwing an exception is also a choice. Which method is used to be determined according to the actual situation.
Returning Custom Error Code is a traditional method derived from C language structured design, which gives up the error handling mechanism of C and COM, using a self-built method. There are less information on the code to write a lot of Switch-case statements to capture the error code. Its disadvantage is that the outer layer code wants to get the innermost error, the intermediate layer must return to the inclination code of the inner layer, such a layer of "Cc" to the outermost layer. The negative effect is that the flexibility is very poor.
Exception can solve this problem. But in the COM specification, a COM component cannot make any exception to this COM, because you don't know if the client language calling this COM supports an abnormal mechanism, it can capture (CATCH) to this exception. It is ruled that it is dead and people live. If the client is missing is also C , and you don't need to write the "specification" COM, and as long as it is practical, then from the COM, the client captures, why not? However, this "gameplay" is best to use, cross-process, across the process, across the process in the process, can not be captured normally, and there is no promise in the COM specification, and you don't use COM recommended methods.
Then what is a recommended method of com, the answer is HRESULT. We can return a HRESULT value to represent something wrong. In many cases, we are too lazy to check the predefined HRESULT values, more reluctant to have defined HRESULT, so almost all methods returns to S_OK or E_FAIL. In fact, 32 long HRESULTs have left us enough space to define their own error codes. You can define the HRESULT value in the IDL file so that when the client #import COM, the definition is automatically added to the * .tlh file, no additional .h file declaration:
CPP_QUOTE ("// Custom Errors")
CPP_QUOTE ("# define e_idnotfound 0x8000f001")
CPP_QUOTE ("# define e_idexist 0x8000f002")
CPP_QUOTE ("# define e_idrequired 0x8000f003")
The client processing HRESULT value is divided into two cases. If you write the client with the VC, use the #import precompiled instruction to generate a COM's smart package, then this package will automatically turn the wrong return code into an exception thrown. As shown in the following is the package code generated by #import:
Inline _variant_t icontext :: getContextValue (_BSTR_T PropName) {
Variant_result;
Variantinit (& _RESULT);
HRESULT_HR = GET_CONTextValue (propName, & _Result); // Call the true method
IF (failed (_hr)) _com_issue_errorex (_hr, this, __UUIDOF (this)); // If you return a Error value, _com_issue_errorex throws an exception
Return_variant_t (_Result, False);
}
Such package code allows us to enjoy specific languages
(C )
Provided convenience, can be developed
COM