This is a step-by-step tutorial on creating, building and registering an XPCOM component on Linux and MS Windows.
The complete source code for this tutorial can be downloaded from here.
guidgen utility.
uuidgen utility.
IMyComponent.idl
#include "nsISupports.idl"
[scriptable, uuid(_YOUR_INTERFACE_GUID_)]
interface IMyComponent : nsISupports
{
long Add(in long a, in long b);
};
xpcom/idl directory found under your Gecko SDK main directory.
xpidl -m header -I_DIR_ IMyComponent.idl will create the IMyComponent.h header file.
xpidl -m typelib -I_DIR_ IMyComponent.idl will create the IMyComponent.xpt typelib file.
IMyComponent.h contains templates for building your component header and implementation files. You can copy and paste the templates to create these files, changing only your component name.
MyComponent.h.
#ifndef _MY_COMPONENT_H_ ....).
#include "IMyComponent.h" to include the interface definition.
#define MY_COMPONENT_CONTRACTID "@mydomain.com/XPCOMSample/MyComponent;1"
#define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample"
#define MY_COMPONENT_CID _YOUR_COMPONENT_GUID_
IMyComponent.h (starting with /* Header file */) into the MyComponent.h file.
_MYCLASS_ with the name of your component.
MyComponent.cpp.
#include "MyComponent.h" to include your component definitions.
IMyComponent.h (starting with /* Implementation file */) into the MyComponent.cpp file.
_MYCLASS_ with the name of your component.
MyComponentModule.cpp.
#include "nsIGenericFactory.h" to include Mozilla GenericFactory definitions.
#include "MyComponent.h" to include your component definitions.
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent) to define the constructor for your component.
static nsModuleComponentInfo components[] =
{
{
MY_COMPONENT_CLASSNAME,
MY_COMPONENT_CID,
MY_COMPONENT_CONTRACTID,
MyComponentConstructor,
}
};
to define the class name, contract ID and GUID of your component.
NS_IMPL_NSGETMODULE("MyComponentsModule", components) to export the above information to Mozilla.
Makefile for Linux, MyComponent.mak for Windows
GECKO_SDK_PATH variable to point to your Gecko SDK directory.
Project > Settings...
Settings For: All Configurations in the upper left corner.
C/C++ tab and choose Preprocessor in the Category drop-down list.
Link tab and choose Input in the Category drop-down list.
cmd on Windows; tcsh, bash, xterm etc. on Linux).
make command. MyComponent.so file is created.
nmake /f MyComponent.mak command. Debug\MyComponent.dll is created.
nmake /f "MyComponent.mak" CFG="MyComponent - Win32 Release" command for a release build. Release\MyComponent.dll is created.
Build > Set Active Configuration...
Build > Build MyComponent.dll
MyComponent.so/MyComponent.dll and IMyComponent.xpt file to the Mozilla components directory.
C:\Program Files\Mozilla Firefox\components.
~/firefox/components (or ~/mozilla/components).
regxpcom command supplied with the Gecko SDK to register the new component.
regxpcom -x _COMPONENTS_DIR_ where _COMPONENTS_DIR_ is the components directory.
xpti.dat and compreg.dat files from your Mozilla profile directory..autoreg in the main Mozilla/Firefox installation directory.
~/.mozilla/firefox/default.??? on Linux.
C:\Documents and Settings\USERNAME\Application Data\Mozilla\Firefox\Profiles\default.??? on Windows.
MyComponentTest.html file provided with the sample and click the "Go" button.