Search code examples
c++firefoxfirefox-addonxpcomgecko

Why do I get error C2440 when I try to compile XPCOM code?


I need to create a Firefox addon that calls C++ code. I did some research and found this tutorial to get me started: http://briankrausz.com/building-a-c-xpcom-component-in-windows/

I followed all the steps but got stuck in the part where I need to compile the code using VS. I'm using VS 2005.

To be more specific, I have a project that contains 2 header files (IMyComponent.h & MyComponent.h) as well 2 cpp files (MyComponent.cpp & MyComponentModule.cpp).

Each cpp file is configured to compile with no CLR support and without precompiled headers.

However, when I attempt to compile, I get 3 C2440 error messages that correspond to the following lines of code:

//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)

In the correspond header file I have the following code:

//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_

All the other code was generated using the xpidl.exe tool included with the Gecko SDK.

Can someone please give some hints?

PS: Here is the 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;
 };

Solution

  • The first error is because you are trying to pass in a char * instead of the required type. This page: https://developer.mozilla.org/en/Adding_XPCOM_components_to_Mozilla_build_system

    at least mentions how to deal with the CID param:

    { "Friendly class description for people to read",
     YOUR_CLASS_CID,
     YOUR_CLASS_CONTRACTID,
     nsYourConcreteClassNameConstructor }
    

    and

    /* 2d96b3d0-c051-11d1-a827-0040959a28c9 */
    #define NS_WINDOW_CID \
    { 0x2d96b3d0, 0xc051, 0x11d1, \
    {0xa8, 0x27, 0x00, 0x40, 0x95, 0x9a, 0x28, 0xc9}}