Purpose of this Article

I have written this tutorial for programmers who are just starting out in XPCOM. The article briefly covers writing your own XPCOM objects and how to reuse existing XPCOM components using VC8.0. This article does not cover XPCOM basics or concepts.

Project Settings and Development Setup was a big problem for me. Also, registering XPCOM is another troublesome issue because xpti.dat and compreg.dat must be deleted from the profile directory.

Introduction

XPCOM is a cross platform component object model, similar to Microsoft COM. It has multiple language bindings, letting XPCOM components be used and implemented in JavaScript, Java, and Python, in addition to C++. Interfaces in XPCOM are defined in a dialect of IDL called XPIDL.

For me, understanding XPCOM has been no less than an odyssey. I believe that every programmer who wishes to understand the basic principles behind XPCOM must write at least one simple XPCOM object using plain C++. In this article, I present the guidelines for creating simple XPCOM objects from first principles. The components should be usable by both VC++/ JavaScript clients.

As an exercise, we will attempt to design an XPCOM component that will implement a hypothetical super-fast addition algorithm. The component must take in two parameters of long data type, and return to the user another long parameter that will be an outcome of our addition algorithm.

How to write C++ XPCOM components

Step 1: Development Setup

Use the right XULRunner SDK for your XULRunner release, I use xulrunner-1.9.2.

Add "/Zc:wchar_t-" in the additional option of C++ Command Line to support wchar_t

Use a custom build step for the XPCOM IDL file (exclude from build by MIDL)

Step 3: Create an XPCOM Component

An XPCOM component is made up of three parts:

Component interface described using IDL. The interface defines the methods, including arguments and return types, of the component.

Component implementation using C++. The implementation is where the methods actually do the work.

Component factory module, also in C++. The factory is in charge of creating instances of the implementations.

Build your component.

Step 4: Register the XPCOM Component

Copy your XPT and DLL files to the Firefox components directory.

Normally, if this was installed as part of an extension, it would automatically search this directory and find these files. But now, we have to force a refresh. Delete the "xpti.dat" and "compreg.dat" files from your profile directory. Add "<yourxpcom>.dll" at the end of the "components.list" file.

Firefox will regenerate them on next restart, or we can use regxpcom.exe like this:

You mean to run regxpcom on customer machine. I think regxpcom is needed when you don't have an addon that the xpcom component is part of. Normally you write xpcom to be part of some addon, so as I said, I just put them in the component directory of my addon with the xpt file, and delete the xpti.dat and compreg.dat.
I guess regxpcom is used in development environment, because the path of the xpcom dll might be your debug folder, rather than the addon component folder. But I just copy the dll to component folder and manually restart the firefox.
Also I am not sure whether regxpcom will work with the Gecko 2 SDK.

Oh. no.. My article is for xpcom programmers, not for xpcom plugin users. For xpcom plugin users, that is customer, there must be installation file which will delete xpti.dat and compreg.dat, modify components.list and then run regxpcom command.

I had written code using this:
http://www.iosart.com/firefox/xpcom/[^]

This article is bad for novice too. I had to copy some libs to build successfully from xulrunner-sdk-1.9.2.8 (nspr4.lib, plc4.lib, plds4.lib). But test page yields "TypeError: Components.classes[cid] is undefined".

Isn't Open Source wonderful? All the information you need is out there, all for free. You just need to spend 20 hours to find them, and your life has just been reduced by 40 days due to frustration. By the end of those 20 hours, you can't even remember what you initially set out to do.

I don't know about xulrunner_sdk, but the article was written long back, and the gecko-sdk that I have contains all the necessary files like libIDL-0.6.

And for the humor part, I myself trying for the last 48 hours to get XPCOM work on Firefox 4b2, which has changed the XPCOM registration altogether, and I am happy that Firefox is open source, extracting information from ... is even harder, and writing a plugin for IE is more harder at least for me.

When opened the sample_xpcom.sln it was converted to new format. There was only one warning during conversion - "MSB8012: $(TargetPath) ('C:\xpcom\sample_xpcom\..\xulrunner-sdk\sdk\bin\sample_xpcom.dll') does not match the Linker's OutputFile property value '..\xulrunner-sdk\sdk\bin\sample_xpcom.dll' ('C:\xpcom\xulrunner-sdk\sdk\bin\sample_xpcom.dll') in project configuration 'Debug|Win32'. This may cause your project to build incorrectly. To correct this, please make sure that $(TargetPath) property value matches the value specified in %(Link.OutputFile)."

By the way there only Debug profile compiles.

Unfortunaly register procedure does not work.

1. I copied ISample.xpt and sample_xpcom.dll into C:\Program Files\Mozilla Firefox\components

2. I deleted compreg.dat and xpti.dat in my Firefox profile.
3. I ran Firefox and it created new compreg.dat and xpti.dat.

# Normally, if this was installed as part of an extension, it would automatically search this directory and find these files. But now we have to force a refresh. Delete xpti.dat. and compreg.dat from your profile directory (FF will regenerate them on next restart)

# when deleting files compreg.dat and xpti.dat modify components.list
(same location where you put DLL and XPT files) and add at the end of
file “sample_xpcom.dll”. (this is new for me, read this comment and article for details[^])

There is no step about components.list in your article. regxpcom does not give effect. So you should add "When deleting files compreg.dat and xpti.dat modify components.list (same location where you put DLL and XPT files) and add at the end of file “sample_xpcom.dll”."

I have used the XPCOM in my sample applications,
i have to use the sample to interact with mozilla applications profile data.
Is there any way to read the mozilla firefox's profile data to handle the deletion of cookies of a particular profile (because mozilla firefox supports multiple profile).