How to raise an exception from C ++ that will trigger script exception handlers

I found bits and pieces of this answer on the Internet, but not a crystal clear solution.

Here is what I am trying to do. 1) Create a simple ATL object. 2) Add a method to this object that returns BOOL, not HRESULT. The subscriber wants to return true / false values. 3) Throw an exception for jscript or vbscript caller that will provide e.description and e.number data.

RE 2) I found that I can use STDMETHODIMP_ (BOOL) together with [local] to allow BOOL to be returned RE 3) I found that I could pass IErrorInfo via SetErrorInfo () to populate the Error object

My dilemma is that I cannot understand how a C ++ architect throws an exception across the ABI border that will not cause the caller to fail.

+4
source share
2 answers

When you write code for a script client, you should use a subset of COM called Automation. What dictates that:

  • all interfaces must be obtained from IDispatch
  • a class must implement only one source interface
  • all methods must return HRESULT; only STDMETHODIMP is valid
  • argument types must be limited to the subset permitted by Automation.

In particular, this means that BOOL is not allowed, it must be VARIANT_BOOL. You declare a method that returns a boolean value by writing it to the IDL:

[id(42)] HRESULT Foo([out,retval] VARIANT_BOOL* retval); 

Assign VARIANT_TRUE or VARIANT_FALSE to * retval in code. The scripting language uses natural syntax, for example var = Foo() .

You throw an exception in the scripting client, returning an HRESULT failure.

+4
source

You can force the client to handle the "exception" on

  • setting IErrorInfo as you said
  • returns no HRESULT hr! = S_OK

So, you need IDispatch / Interop compatible interfaces that require HRESULT return types (AFAIR).

IDL allows a lot more, but "dynamic" clients, such as script hosts (VBS, JScript, VBA and others), do not consume them initially, so interoperability will not be optimal.

+1
source

Source: https://habr.com/ru/post/1436438/


All Articles