Click here to Skip to main content
14,027,303 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 26 Jul 2010
Licenced CPOL

Convert Microsoft COM to XPCOM

Rate this:
Please Sign up or sign in to vote.
Step by step XPCOM creation from Microsoft COM

Purpose of the Article

I have written this tutorial for programmers who are just starting out in XPCOM but have good knowledge in Microsoft COM. The article briefly covers writing your own XPCOM object converting MsCOM using VC8.0.This article does not cover XPCOM Basics or concepts.

Project Settings or 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 profile directory.

I have also written an article for XPCOM beginners A Simple XPCOM Tutorial.


XPCOM is a cross platform component object model, similar to Microsoft COM. It has multiple language bindings, letting the 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 object converting MsCOM. The components should be usable by both VC++/ JavaScript clients.

As an exercise, we will attempt to design a 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 Create XPCOM from Microsoft COM

Step 1: Development Setup

  • Use the right xulrunner SDK for your XULRunner release, I use xulrunner-1.9.2
  • Use a Microsoft compiler, I use Visual C++2005

Here is what my folder structure looks like:

    - xulrunner-sdk
    - sample_xpcom  (xpcom creation)

Step 2: Create a VC++ Project

  • Start an empty Win32 project in Visual Studio, selecting the "Dynamic Linked Library (DLL)" option.
  • Make the following tweaks:
    • Add “..\xulrunner-sdk\include” to Additional Include Directories
    • Add “..\xulrunner-sdk\lib” to Additional Library Directories
    • Add “nspr4.lib xpcom.lib xpcomglue_s.lib” to Additional Dependencies
    • Add “XP_WIN;XP_WIN32? to Preprocessor Definitions
    • Turnoff precompiled headers (just to keep it simple)

Step 3: Create the IDL File

  • XPCOM uses “include” to import IDL files.
  • Define interface attributes.
  • All XPCOM interfaces inherit the nsISupports interface. So, inherit your interface from “nsISupports”.
  • Defining library is not required.
  • Use a custom build step for the XPCOM IDL file (exclude from build by MIDL)

Step 4: Compile the IDL File

  • MsCOM is compiled with MIDL.exe. But XPCOM is compiled with XPIDL.exe. For MsCOM

    {path_to_ Vs_Tools }\bin\Midl.exe /I /Oicf {your_idl_file}
  • For XPCOM, compile twice with xpidl to create header and typelibrary.
    {path_to_ xulrunner-sdk }\bin\xpidl.exe 
    	-m header -I..\ xulrunner-sdk \idl {your_idl_file} 
    {path_to_ xulrunner-sdk }\bin\xpidl.exe 
    	-m typelib -I..\ xulrunner-sdk \idl {your_idl_file}

    A type-library (*.XPT) and a C++ header file (*.H) will be created.

Step 5: Create the Implementation Files (header and CPP)

Header File

  • Include interface generated header file.
  • Define Component Contract Id. The recommended format of a contract ID is a one-line string as follows:
  • Define class name, same as help string of MSCOM attributes - Define component ID, that is UUID of the interface.
  • Define the class just derives the interface.
  • Declare COM entry map.

    For MsCOM:


    But for XPCOM:

  • Declare methods and properties.

CPP File

  • Include header file.
  • Implement com Entry map.
  • Define methods.

Step 6: Create Module File

  • Define Module file which contains the XPCOM component.
  • RGS file is not necessary, because XPCOM doesn’t use Windows registry.
  • def file is not necessary.

Step 7: Register the DLL

  • Copy your XPT and DLL files to the Firefox components directory.
  • MsCOM is registered with REGSVR32.exe.
    "regsvr32 {yourMsCOM dll} "
  • But XPCOM is registered with REGXPCOM.exe.
  • Delete xpti.dat and compreg.dat from your profile directory (and if there exists "components.list" file, add "yourXPCOM DLL" at the end). Firefox will regenerate them on next restart or we can use regxpcom.exe like this:
    "regxpcom -x { FireFoxDir }\bin {FireFoxDir}\components\{yourXPCOM dll}"

Using the Code

Now start the example:

This is the IDL file for MsCOM.

import "oaidl.idl";
import "ocidl.idl";

    helpstring("ISampleAdd Interface"),
interface ISampleAdd : IDispatch
    [id(1), helpstring("method Add")] HRESULT Add([in] LONG FisrtNumber, [in]
                  LONG SecondNumber, [out,retval] LONG* ResultValue);
Definition of library which is not required for xpcom

Change this IDL like below:

#include "nsISupports.idl"

[scriptable, uuid(658ABC9E-29CC-43E9-8A97-9D3C0B67AE8B)]
// here I used scpiptable to support scripting languages to access this component
interface ISample : nsISupports
    long	Add(in long a, in long b);  

Compile the IDL:

  • For MsCOM, we can compile like:
    MidL /I /Oicf Sample_mscom.idl

    Compiling with midl creates 5 files:

    Sample_mscom.hContains the C++ style interface declarations.
    dlldata.cContains code for proxy DLL. Useful when invoking the object on a different process/computer.
    Sample_mscom.tlbBinary file, with a well defined format that completely describes our interface IAdd along with all its methods. This file is to be distributed to all the clients of our COM component.
    Sample_mscom_p.cContains marshalling code for proxy DLL. Useful while invoking the object on a different process/computer.
    Sample_mscom_i.cContains the interface IIDs
  • But for XPCOM, we need to compile with xpidl. It creates one file in each compilation.

    Here we have to compile like:

    {path_to_ xulrunner-sdk }\bin\xpidl.exe 
    	-m header -I ..\ xulrunner-sdk \idl ISample.idl 
    {path_to_ xulrunner-sdk }\bin\xpidl.exe 
    	-m typelib -I ..\ xulrunner-sdk \idl ISample.idl 
  • Here we will get two files:

    ISample.hContains the C++ style interface declarations.
    ISample.xptBinary file, with a well defined format that completely describes our interface IAdd along with all its methods. This file is to be distributed to all the clients of our XPCOM component.

Header File

#include "ISample.h"

#define SAMPLE_COMPONENT_CLASSNAME "Sample XPCOM Interface Layer"
#define SAMPLE_COMPONENT_CID  { 0x658abc9e, 0x29cc, 0x43e9,
	{ 0x8a, 0x97, 0x9d, 0x3c, 0x0b, 0x67, 0xae, 0x8b } }

class CSample : public ISample
    /* Use this macro when declaring classes that implement this interface. */
    virtual ~CSample();

    //additional member functions
    int Add();

CPP File

#include "Sample.h"

//This macro automatically adds the nsISupports entry

    /* member initializers and constructor code */

    /* destructor code */

/* long Add (in long a, in long b); */
NS_IMETHODIMP CSample::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
    *_retval = a + b;
    return NS_OK;

Module File

#include "nsIGenericFactory.h"
#include "Sample.h"

// This results in a function called CSampleConstructor that 
// can be used in the nsModuleComponentInfo structure.

static nsModuleComponentInfo components[] =

// Implements the nsIModule interface with the module name of name and 
// the component list in components.
NS_IMPL_NSGETMODULE("sample_module", components)

Note: An easy and simple way of learning this is to just use the projects. Build these projects and also you can get help from my "A Simple XPCOM Tutorial". If there are any suggestions, requests or problems, please inform me.


  • 26.07.2010 Initial release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Mazharul Islam Khan
Software Developer (Senior) Eyeball Networks Inc, Dhaka
Bangladesh Bangladesh
I strongly believe, a person's strongest point is to know his/her weak point. I try to be simple.

I have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).

Watch out for my other CodeProject Articles.

You may also be interested in...

Comments and Discussions

GeneralConvert Microsoft COM to XPCOM Pin
bleachli22-Jun-16 17:42
memberbleachli22-Jun-16 17:42 
GeneralMy vote of 5 + comment + Question Pin
mehrcpp8-Aug-10 5:42
membermehrcpp8-Aug-10 5:42 
GeneralMy vote of 5 Pin
Member 19161988-Aug-10 1:54
memberMember 19161988-Aug-10 1:54 
QuestionUse of COM on UNIX/Linux? Pin
Sharjith6-Aug-10 11:05
professionalSharjith6-Aug-10 11:05 
AnswerRe: Use of COM on UNIX/Linux? Pin
Md. Mazharul Islam Khan7-Aug-10 19:13
memberMd. Mazharul Islam Khan7-Aug-10 19:13 
General"Convert Microsoft COM to XPCOM" makes me confusion Pin
liusj20004-Aug-10 23:55
memberliusj20004-Aug-10 23:55 
GeneralRe: "Convert Microsoft COM to XPCOM" makes me confusion Pin
Md. Mazharul Islam Khan5-Aug-10 0:11
memberMd. Mazharul Islam Khan5-Aug-10 0:11 
GeneralMy vote of 5 Pin
Mukit, Ataul26-Jul-10 19:06
memberMukit, Ataul26-Jul-10 19:06 
GeneralMy vote of 5 Pin
ThatsAlok26-Jul-10 4:56
memberThatsAlok26-Jul-10 4:56 
GeneralRe: My vote of 5 [modified] Pin
Md. Mazharul Islam Khan26-Jul-10 18:46
memberMd. Mazharul Islam Khan26-Jul-10 18:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190419.4 | Last Updated 26 Jul 2010
Article Copyright 2010 by Md. Mazharul Islam Khan
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid