当我尝试编译XPCOM代码时,为什么会出现错误C2440?
我需要创建一个调用C++代码的Firefox插件。我做了一些研究,发现这个教程让我开始:http://briankrausz.com/building-a-c-xpcom-component-in-windows/当我尝试编译XPCOM代码时,为什么会出现错误C2440?
我遵循所有的步骤,但卡住了我需要使用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;
};
第一个错误是因为你试图在char *
,而不是所需要的类型来传递。本页面:https://developer.mozilla.org/en/Adding_XPCOM_components_to_Mozilla_build_system
至少提到如何应对CID PARAM:
{ "Friendly class description for people to read",
YOUR_CLASS_CID,
YOUR_CLASS_CONTRACTID,
nsYourConcreteClassNameConstructor }
和
/* 2d96b3d0-c051-11d1-a827-0040959a28c9 */
#define NS_WINDOW_CID \
{ 0x2d96b3d0, 0xc051, 0x11d1, \
{0xa8, 0x27, 0x00, 0x40, 0x95, 0x9a, 0x28, 0xc9}}
我将MY_COMPONENT_CID设置为像上面那样格式化的值,现在编译代码。谢谢 – 2012-08-01 22:10:52
你能不能也发布'nsModuleComponentInfo'的声明? – 2012-08-01 21:33:49
您可能需要某种方式来投射数据类型。对不起,我没有更多的帮助,我不知道有关Firefox技术,但这是一个错误代码,我敢肯定,你可以看到,如果自我解释。它不能将一种数据类型转换为另一种数据类型。一个char [x]'实际上是一个1字节的数组(在大多数系统中)'int's。所以它可能是因为它没有能力。 [This](http://msdn.microsoft.com/en-us/library/sy5tsf8z(v = vs.80).aspx)总是有帮助的! – ChiefTwoPencils 2012-08-01 21:34:36
当你声明'components'时,你看起来好像缺少了'MY_COMPONENT_CLASSNAME'之前的结构开始部分的一些数据。实际上在Contract ID之前有3个成员变量。 – 2012-08-01 21:41:54