Making native COM methods work as first class citizens in jscript

105 Views Asked by At

My application is hosting jscript (IActiveScript, IE9). It exports an interface (dual, IDispatch) to it (see below). I can call it from jscript:

host.my_method(42);

but passing it to another function (or assigning to a variable) does not work:

function foo(f) { f(42); };
foo(host.my_method); // error 0x800a138f - Unable to get property 'my_method'

Question: how do I make my native function look like property?

Interface:

[
    object,
    dual,
    uuid(whatever),
    pointer_default(unique)
]
__interface IMyInterface
{
    [id(1), helpstring("My method")]
    HRESULT my_method([in] VARIANT * value);
};

Here is an implementation of that interface:

[
    coclass,
    event_source(com),
    threading(apartment),
    uuid(guid),
    noncreatable,
    aggregatable(never),
    default(IMyInterface)
]
class MyClass :
    public CComObjectRootEx<....>,
    public CComCoClass<MyClass>,
    public IDispatchImpl<IMyInterface>,
    public IProvideClassInfo2Impl<....>
{
    ...
    STDMETHOD(my_method)(/*[in]*/ VARIANT * value) override;
};
1

There are 1 best solutions below

1
On

foo(host.my_method) doesn't work correctly in pure JavaScript either; my_method wouldn't have access to host via this. The right way to do this in JavaScript is with a capture:

foo( function (x) { return host.my_method(x); } );