Active X控件自定义事件无法在Javascript中激发

Active X control custom events fail to fire in Javascript

本文关键字:Javascript 控件 自定义 事件 Active      更新时间:2023-09-26

我已经使用IDispatch接口编写了一个自定义的活动X控件,我想与javascript进行通信。我已经成功地使javascript->COM路径工作;我可以在我的活动x对象上调用一个javascript函数,并在我的dll中接收相应的INVOKE调用。

为了在javascript端接收事件,我遵循本文中的建议:http://jeffcode.blogspot.com/2008/02/how-to-create-activex-control-that.html

当我加载测试页面时,我会得到一个对FindConnectionPoint的调用,然后是一个对Advise的调用,正如我所期望的那样。当我在Advise提供的接口上调用Invoke时,我会收到一条成功状态消息,但在js端什么都没发生!

这是我用来测试事件处理的javascript代码:

function FooActiveX::ReceiveMessage(msg)
{
    alert(msg);
}

有趣的是,如果我删除了它,我就不会再调用FindConnectionPoint或Advise了,所以它正在做一些事情。

任何关于如何调试这个问题或尝试的事情的建议都将非常有用。非常感谢。

我的idl接口定义文件如下:

[
uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb7"),
version(1.0),
]
library FooControlLib
{
interface IFooControl;
dispinterface DFooControlEvents;
importlib("stdole2.tlb");
[
    uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb8"),
    hidden
]
dispinterface DFooControlEvents
{
    properties:
    methods:
        [id(DISPID_RECEIVEMESSAGE)]  void ReceiveMessage( [in] BSTR msg );
}
[
    odl,
    dual,
    uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb9"),
    oleautomation
]
interface IFooControl : IDispatch
{
    [id(DISPID_SENDMESSAGE)] HRESULT SendMessage( [in] BSTR msg);
}
[
    uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fc0")
]
coclass FooControl
{
    [default] interface IFooControl;
    [source, default] dispinterface DFooControlEvents;
}
}

EDIT:问题似乎与ReceiveMessage方法中的参数有关。如果我删除"msg"参数,警报框将正确显示。

发现我的问题。方法的参数作为VARIANTARG结构的数组传递给Invoke。我设置了值,但没有设置该结构的vt成员,它标识了参数的类型。我不知道invoke为什么返回OK状态。