Geekout: How to make a C++ XPCOM Component

This is a geek-out side story. Apologies if you’re not a programmer.

Alex Sirota and Mark Finkle have excellent pages on how to make a C/C++ XPCOM component for Windows. However, neither were what I needed: Alex’s didn’t work for Visual Studio Express, and Mark’s wasn’t specific for Firefox. It took me quite a while to get something working, so I thought I should document what worked for me in concrete specific steps:

  • If you don’t have it, download Visual C++ Express.
  • Download gecko-sdk-win32-msvc-1.8.0.4.zip and extract it to a folder named C:\xulrunner-1.8.0.4
  • Download wintools.zip and extract it to C:\temp\wintools
    • In the zip file, find these files:
      • C:\temp\wintools\buildtools\windows\bin\x86\glib-1.2.dll
      • C:\temp\wintools\buildtools\windows\bin\x86\libIDL-0.6.dll
    • Copy them to C:\xulrunner-1.8.0.4\gecko-sdk\bin
  • Download Mark Finkle’s sample XPCOM visual studio project xpcom-test.zip and extract it into C:\xulrunner-1.8.0.4
  • Your folder structure should now look like this (from Mark’s page):
  • Open the visual studio project by double clicking on C:\xulrunner-1.8.0.4\xpcom-test\test.sln
  • In Visual Studio, select Build->Build Solution. This compiles the code and makes the dll file.
  • Open a command window and cd to C:\xulrunner-1.8.0.4\xpcom-test
    • run this command> xpidl-build.bat comp.idl
    • This will create the xpt file
  • Locate these 2 files:
    • C:\xulrunner-1.8.0.4\xpcom-test\comp.xpt
    • C:\xulrunner-1.8.0.4\xpcom-test\debug\test.dll
  • Copy them to your Firefox components directory at C:\Program Files\Mozilla Firefox\components
  • Open a command window and run these commands:
    • > C:\xulrunner-1.8.0.4\gecko-sdk\bin\regxpcom.exe -x “C:\Program Files\Mozilla Firefox\components”
    • > touch “C:\Program Files\Mozilla Firefox\.autoreg”
  • Restart Firefox
  • Download this test page to your computer.
  • Load the test page in Firefox and click the test button.
  • With any luck, you’ll see that 3+4=7. You can now start modifying the component. Be sure to go back and read the pages from Alex Sirota and Mark Finkle for more details.

Hope this saves someone else the trouble someday!

[Updated 2007/11/29 to fix typo and add Firefox Version info]

My environment:
Firefox version: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10
Windows XP SP 2.

11 thoughts on “Geekout: How to make a C++ XPCOM Component”

  1. hi All,
    I have developed an XPCOM component using the steps for mozilla thundebird.I have written some functions in Javascript.IN javascript its giving me “TypeError: Components.classes[cid] has no properties” in the following line
    objSyncComponent = Components.classes[cid].getService(Components.interfaces.ISyncComponent);

    the cid is properly defined like below
    const cid=”@sample.com/XPCOMServer/CSyncComponent;1″

    can u pls tell me the problem.

    pls reply as soon as u can.

  2. hi

    i cannot fix this issue

    error LNK2019: unresolved external symbol \"unsigned int __cdecl NS_NewGenericModule2(struct nsModuleInfo const *,class nsIModule * *)\" (?NS_NewGenericModule2@@YAIPBUnsModuleInfo@@PAPAVnsIModule@@@Z) referenced in function _NSGetModule

Comments are closed.