VB-like VC ++ Development (2)-com exception and safecall

Source: Internet
Author: User

Correctly handling COM exceptions in COM programming is the basis for enhancing program robustness. The safecall concept exists in Delphi. Although this concept is not mentioned in VB, it is still in use. In VC, manual support is required. So what is safecall and what is the relationship with COM exceptions?

Safecall is actually a method of calling COM components between providers and callers. Safecall is first a stdcall, that is, all interface methods exposed by the COM component adopt the stdcall call Convention consistent with the Windows API. Second, safecall also requires that all interface methods return the hresult error code, which does not cause a com exception. after calling the interface method, the caller must check the hresult and handle the error accordingly. That is to say, safecall ensures that all com exceptions are encapsulated in the method, and the caller does not have to handle exceptions.

For example, iprovider provides a method of Echo. We can see the differences between different languages and different paradigms.

 

VB

'Provision' VB encapsulates the function echo (smessage as string) as string echo = smessageend function 'caller' VB, which encapsulates the processing of hresult error codes, if an error occurs, the com exception is thrown. On Error resume nextmsgbox echo ("Hello world! ")
 

 

VC ++-General Method

// Provider // you need to manually encapsulate exceptions using try catch to ensure that all errors return hresult echo (BSTR bstrmessage, BSTR * pbstrvalue) {try {* pbstrvalue = :: sysallocstring (L "Hello world! ");} Catch (_ com_error & E) {Return e. error ();} return s_ OK;} // caller // common method _ bstr_t bstrvalue; hresult hR = pprovider-> echo (_ bstr_t (L "Hello world! "), Bstrvalue. getaddress (); If (failed (HR) {// two processing methods are available here. // The first method returns the error return HR directly; // the second method, returns an exception, just like the VB method. _ com_util: checkerror (HR);} // if the provider component is imported through # import, you can call try {_ bstr_t bstrvalue = pprovider-> echo (L "Hello world! ");} Catch (_ com_error & E) {return E. Error ();}

 

VB-like VC ++

// Provider // encapsulate hresult echo (BSTR bstrmessage, BSTR * pbstrvalue) {__ safecall_begin using a custom macro; * pbstrvalue =: sysallocstring (L "Hello world! ") ;__ Safecall_end;} // caller // import through # import // if the method is called internally, you do not have to detect com exceptions; // because the methods exposed externally encapsulate the com exception through safecall _ bstr_t bstrvalue = pprovider-> echo (L "Hello world! ");

 

Custom macros

#define __SAFECALL_BEGIN / try{/  #define __SAFECALL_END /  / }/ catch(_com_error& e){/  return e.Error();/ }/ catch(...){/  return E_FAIL;/ }/ return S_OK

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.