脚本调用ActiveX控件若干问题

来源:互联网 发布:ubuntu 16.04 高分屏 编辑:程序博客网 时间:2024/05/21 00:49

1. 让脚本识别你的com对象的Method和Property。

1.1 有时我们在脚本里(javascript & vbscript)调用com组件的方法或属性时,系统会提示Object doesn't support method or property.我们知道要使com组件和browser client通信,com的接口一定要是继承自IDispatch接口,因为它实现了automation的功能,在idl文件里有一个接口ITest定义为:

 

[

 object,
 uuid(ACA3A8D9-7350-45E6-978A-4E44F103C024),
 dual,
 nonextensible,
 helpstring("ITest Interface"),
 pointer_default(unique)

]

interface ITest : IDispatch{

 [id(1), helpstring("method function1")] HRESULT function1([in] BSTR bstrParam);

};

而且我们要让接口是个dual接口,dual的含义是:that is, the flexibility of both vtable and late binding, thus making the class available to scripting languages as well as C++(MSDN).

1.2 如果你有个custom的接口,叫ITest2,并且你的com对象也继承了它,但是在脚本里却不能访问它的method或property,此时你只要吧ITest2的method或property的声明加到ITes1里就可以,因为ITest1是dual接口且是default,scripting clients are only able to access the [default] IDispatch interface,它的method和property能被brower访问。比如ITest2的定义如下:

[

 object,
 uuid(8C0D0B02-6C65-44DF-A54A-7D2E29262F93),
 helpstring("ITest Interface"),
]

interface ITest2 : IUnkown{

 [id(1), helpstring("method function2")] HRESULT function2([in] BSTR bstrParam);

};

然后ITest就是:

interface ITest : IDispatch{

 [id(1), helpstring("method function1")] HRESULT function1([in] BSTR bstrParam);

 [id(2), helpstring("method function2")] HRESULT function2([in] BSTR bstrParam);

};

 

 coclass MyCOMObject
 {
  [default] interface ITest;
  interface ITest2;
 };

这样在脚本里就可以访问接口ITest2的method了。