作者在 2008-11-26 14:36:47 发布以下内容
Creating a C++ XPCOM component
About
This is a step-by-step tutorial on creating, building and registering an XPCOM component on Linux and MS Windows.
Download
The complete source code for this tutorial can be downloaded from here.
Creating the component
- Download the Gecko SDK for your platform.
- You can find it at http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.7/.
- You can choose a different Mozilla release if you like.
- Extract the SDK to a local directory.
- Create a GUID for your main interface.
- On Windows you can use the
guidgenutility. - On Linux you can use the
uuidgenutility.
- On Windows you can use the
- Create the interface definition file -
IMyComponent.idl- Use the following template, add methods and attributes to the interface.
- Fill in the GUID you have generated.
#include "nsISupports.idl" [scriptable, uuid(_YOUR_INTERFACE_GUID_)] interface IMyComponent : nsISupports { long Add(in long a, in long b); }; - Generate the interface header and typelib files out of the interface definition file.
- Use the xpidl utility that comes with Gecko SDK.
- Substitute the "_DIR_" in the following commands with the full path to the
xpcom/idldirectory found under your Gecko SDK main directory. xpidl -m header -I_DIR_ IMyComponent.idlwill create theIMyComponent.hheader file.xpidl -m typelib -I_DIR_ IMyComponent.idlwill create theIMyComponent.xpttypelib file.
- The interface header file
IMyComponent.hcontains 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. - Create your component header file -
MyComponent.h.- Start by inserting double inclusion protection code (
#ifndef _MY_COMPONENT_H_ ....). - Add
#include "IMyComponent.h"to include the interface definition. - Create a GUID for your component.
- Add the following lines, which define your component name, contract ID, and GUID:
#define MY_COMPONENT_CONTRACTID "@mydomain.com/XPCOMSample/MyComponent;1" #define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample" #define MY_COMPONENT_CID _YOUR_COMPONENT_GUID_ - Copy the header template from
IMyComponent.h(starting with/* Header file */) into theMyComponent.hfile. - Replace all the instances of
_MYCLASS_with the name of your component.
- Start by inserting double inclusion protection code (
- Create your component implementation file -
MyComponent.cpp.- Add
#include "MyComponent.h"to include your component definitions. - Copy the implementation template from
IMyComponent.h(starting with/* Implementation file */) into theMyComponent.cppfile. - Replace all the instances of
_MYCLASS_with the name of your component. - Add method implementation code.
- Add
- Create your module definitions file -
MyComponentModule.cpp.- Add
#include "nsIGenericFactory.h"to include Mozilla GenericFactory definitions. - Add
#include "MyComponent.h"to include your component definitions. - Add
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)to define the constructor for your component. - Add
to define the class name, contract ID and GUID of your component.static nsModuleComponentInfo components[] = { { MY_COMPONENT_CLASSNAME, MY_COMPONENT_CID, MY_COMPONENT_CONTRACTID, MyComponentConstructor, } }; - Add
NS_IMPL_NSGETMODULE("MyComponentsModule", components)to export the above information to Mozilla.
- Add
- Create the makefiles and/or projects.
- You can use the templates provided with this sample to create your custom makefile.
- Alternatively, you can create a Visual C++ project with similar settings (on Windows).
Building the sample
- Download the sample code from here.
- Extract the sample to a local directory.
- Edit the makefile.
- The makefile is located in the src directory of the sample.
- It is named
Makefilefor Linux,MyComponent.makfor Windows - Edit the makefile, changing the
GECKO_SDK_PATHvariable to point to your Gecko SDK directory. - Added: When using the included Visual Studio project:
- Open the project settings:
Project > Settings... - Choose
Settings For: All Configurationsin the upper left corner. - Open the
C/C++tab and choosePreprocessorin theCategorydrop-down list. - Make sure that "Additional include directories" points to your Gecko SDK.
- Open the
Linktab and chooseInputin theCategorydrop-down list. - Make sure that the "Additional library path" points to your Gecko SDK.
- Open the project settings:
- Build the sample.
- Open a command shell (
cmdon Windows;tcsh,bash,xtermetc. on Linux). - Navigate to the sample src directory.
- On Linux issue a
makecommand.MyComponent.sofile is created. - On Windows issue a
nmake /f MyComponent.makcommand.Debug\MyComponent.dllis created. - Alternatively, on Windows, issue a
nmake /f "MyComponent.mak" CFG="MyComponent - Win32 Release"command for a release build.Release\MyComponent.dllis created. - Added: When using the included Visual Studio project:
- Select the wanted configuration (Debug/Release) in
Build > Set Active Configuration... - Choose
Build > Build MyComponent.dll
- Select the wanted configuration (Debug/Release) in
- Open a command shell (
- Register the new component with Mozilla.
- Copy
MyComponent.so/MyComponent.dllandIMyComponent.xptfile to the Mozilla components directory. - On Windows this directory is typically located at:
C:\Program Files\Mozilla Firefox\components. - On Linux this directory is typically located at
~/firefox/components(or ~/mozilla/components). - Run the
regxpcomcommand supplied with the Gecko SDK to register the new component. - You might need to provide an additional parameter:
regxpcom -x _COMPONENTS_DIR_where _COMPONENTS_DIR_ is the components directory. - Delete the
xpti.datandcompreg.datfiles from your Mozilla profile directory.
These files will be automatically rebuilt by Mozilla the next time it is restarted.
Added: Alternatively you can "touch" (either create or update) a file called.autoregin the main Mozilla/Firefox installation directory. - The profile directory is typically located at:
~/.mozilla/firefox/default.???on Linux.C:\Documents and Settings\USERNAME\Application Data\Mozilla\Firefox\Profiles\default.???on Windows.
- Copy
- Test the new component.
- Restart Mozilla.
- Open the
MyComponentTest.htmlfile provided with the sample and click the "Go" button. - If everything is OK you should see a dialog saying "3 + 4 = 7".
Links and Resources
- IBM developerWorks - XPCOM Overview [part 1] [part 2] [part 3] [part 4] [part 5]
- Creating XPCOM Components by Doug Turner.
- Creating Applications With Mozilla [chapter 8 - XPCOM]
Revision History
- June 3, 2005
- Modified the Windows makefile (MyComponent.mak) to work with the latest Gecko SDK. The old version of the sample can be found here.
- Added Visual Studio 6 project file to the sample.
- Added a tip about using the ".autoreg" file for registering new components.


