0

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;
};
sms
  • 1,014
  • 10
  • 20

1 Answers1

0

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); } );
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • I've tried `foo(window.alert)` in IE - it worked without wrapping. Here `window` is an object (just like my `host`). Btw, `window.alert.toString()` works, while `host.my_method.toString()` does not. I mean, how do they do that? – sms Jun 26 '15 at 12:45