我正在使用wxWebConnect测试应用程序进行实验,并在“http://nerdlife.net/building-ac-xpcom-component-in-windows/”中加入 xpcom 教程
我根据需要调整 MyComponent 类以与 testapp.exe (而不是单独的 dll)一起编译,并且在 MyApp::OnInit 上我有以下几行:
ns_smartptr<nsIComponentRegistrar> comp_reg;
res = NS_GetComponentRegistrar(&comp_reg.p);
if (NS_FAILED(res))
return false;
ns_smartptr<nsIFactory> prompt_factory;
CreateMyComponentFactory(&prompt_factory.p);
nsCID prompt_cid = MYCOMPONENT_CID;
res = comp_reg->RegisterFactory(prompt_cid,
"MyComponent",
"@mozilla.org/mycomp;1",
prompt_factory);
这些行是从 GeckoEngine::Init() 复制的,使用相同的机制来注册 PromptService 等。代码编译良好,并且 testapp.exe 按预期运行。
我将javascript测试如下:
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const cid = "@mozilla.org/mycomp;1";
obj = Components.classes[cid].createInstance();
alert(typeof obj);
// bind the instance we just created to our interface
alert(Components.interfaces.nsIMyComponent);
obj = obj.QueryInterface(Components.interfaces.nsIMyComponent);
} catch (err) {
alert(err);
return;
}
并得到以下异常:无法转换 JavaScript 参数 arg 0 [nsISupport.QueryInterface]
第一个警报说“对象”,所以该行
Components.classes[cid].createInstance()
正在返回创建的实例。
第二个警报显示“未定义”,因此 XULRunner 无法识别接口 nsIMyComponent。如何在 wxWebConnect 环境中动态注册 nsIMyComponent 接口?
谢谢