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: