为什么在尝试编译XPCOM代码时会出现错误C2440



我需要创建一个调用C++代码的Firefox插件。我做了一些研究,找到了这个教程让我开始:http://briankrausz.com/building-a-c-xpcom-component-in-windows/

我遵循了所有的步骤,但在需要使用VS编译代码的部分遇到了困难。我使用的是VS2005。

更具体地说,我有一个项目,它包含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参数:

{ "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}}

相关内容

最新更新