【发布时间】:2012-08-01 21:10:16
【问题描述】:
我需要创建一个调用 C++ 代码的 Firefox 插件。我做了一些研究,发现这个教程让我开始:http://briankrausz.com/building-a-c-xpcom-component-in-windows/
我遵循了所有步骤,但在需要使用 VS 编译代码的部分卡住了。我正在使用 VS 2005。
更具体地说,我有一个项目,其中包含 2 个头文件(IMyComponent.h 和 MyComponent.h)以及 2 个 cpp 文件(MyComponent.cpp 和 MyComponentModule.cpp)。
每个 cpp 文件都配置为在不支持 CLR 且不带预编译头的情况下进行编译。
但是,当我尝试编译时,我收到 3 条 C2440 错误消息,它们对应于以下代码行:
//MyComponentModule.cpp
#include "nsIGenericFactory.h"
#include "MyComponent.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)
static nsModuleComponentInfo components[] =
{
{
MY_COMPONENT_CLASSNAME,
MY_COMPONENT_CID, //'initializing': cannot convert from const char[37] to PRUint32
MY_COMPONENT_CONTRACTID, //'initializing': cannot convert from const char[39] to PRUint16
MyComponentConstructor, //'initializing': cannot convert from nsresult (__stdcall *)(nsISupports *, const nsIID &, void **) to PRUint16
}
};
NS_IMPL_NSGETMODULE("MyComponentsModule", components)
在相应的头文件中我有以下代码:
//MyComponent.h
#ifndef _MY_COMPONENT_H_
#define _MY_COMPONENT_H_
#include "IMyComponent.h"
#define MY_COMPONENT_CONTRACTID "@example.com/XPCOMSample/MyComponent;1"
#define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample"
#define MY_COMPONENT_CID "4c6f45e0-6366-4c69-Ab68-bb3c75cdada3"
class MyComponent : public IMyComponent
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMYCOMPONENT
MyComponent();
private:
~MyComponent();
protected:
/* additional members */
};
#endif //_MY_COMPONENT_H_
所有其他代码都是使用 Gecko SDK 附带的 xpidl.exe 工具生成的。
有人可以给点提示吗?
PS:这里是 nsModuleComponentInfo:
struct nsModuleComponentInfo {
const char* mDescription;
nsCID mCID;
const char* mContractID;
NSConstructorProcPtr mConstructor;
NSRegisterSelfProcPtr mRegisterSelfProc;
NSUnregisterSelfProcPtr mUnregisterSelfProc;
NSFactoryDestructorProcPtr mFactoryDestructor;
NSGetInterfacesProcPtr mGetInterfacesProc;
NSGetLanguageHelperProcPtr mGetLanguageHelperProc;
nsIClassInfo ** mClassInfoGlobal;
PRUint32 mFlags;
};
【问题讨论】:
-
能不能把
nsModuleComponentInfo的声明也发一下? -
您可能需要某种方式来转换数据类型。抱歉,我对 firefox 技术一无所知,因此对我没有多大帮助,但这是一个错误代码,我相信您可以看到它是否可以自我解释。它不能将一种数据类型转换为另一种数据类型。
char[x]实际上是一个 1 字节数组(在大多数系统上)ints。所以可能是它超出了它的能力。?? This 也总是很有帮助! -
当您声明
components时,您似乎缺少了一些应该位于结构开头MY_COMPONENT_CLASSNAME之前的数据。实际上在 Contract ID 之前有 3 个成员变量。 -
克里斯,请看原帖。我已添加您要求的代码。
-
请注意:如果可能,您最好使用js-ctypes(将您自己的库添加到扩展并调用其导出的函数)。每个 Firefox 版本需要每 6 周重新编译一次 XPCOM 组件。
标签: c++ firefox firefox-addon xpcom gecko