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.

Follow me on twitter.

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

  1. John Says:

    Hello,

    I have Gecko/20071115 Firefox/2.0.0.10

    and I have a problem:

    1. This command are wrong
    Open a command window and run these commands:
    “C:\xulrunner-1.8.0.4\gecko-sdk\bin” -x “C:\Program Files\Mozilla Firefox\components”

    2. I was trying with this command

    C:\xulrunner-1.8.0.4\gecko-sdk\bin\regxpcom -x C:\Program Files\Mozilla Firefox\components

    and i get a informetion:

    “Can not initialize XPCOM GLUE
    Can not aquire component registrar
    Registration failed: Files\Mozila
    Can not initialize XPCOM GLUE
    Can not aquire component registrar
    Registration failed:
    Files\componets”

    What is wrong ?
    I don’t take any mistakes with instrucions above.

  2. stan Says:

    I never encountered a problem with XPCOM GLUE. It looks like some people found solutions over on Alex’s page. Are you developing on Windows? This to be more of an issue on Linux machines.

  3. Arvin Says:

    Hi,
    I have met a strange problem with firefox (v2.0.0.9) that xpt file can not be
    loaded into firefox from a specified directory. I followed the site at
    http://www.mozilla.org/projects/plugins/first-install-problem.html to set
    all of registries as required. Also I looked at registries setting from
    other companies. Registriy setting should be correct.
    If I put both the files npTest.dll and nsTest.xpt into the
    directory C:\Program Files\Mozilla Firefox\plugins, everything works
    correctly without any problems. If I put the both files into another directory
    other than C:\Program Files\Mozilla Firefox\plugins with setting regsitries
    as showed at the site
    http://www.mozilla.org/projects/plugins/first-install-problem.html, firefox
    can see the npTest.dll only. It seems that xpt file can’t be loaded into firefox and all of
    javascript calls fail, even though I set the XPTPath to point to the
    directory and the file nsTest.xpt.
    Please give me help! I am running out ideas. I tried to do internet
    seach, but I found nothing.
    Thanks in advance.

  4. Marius Says:

    I added this due some unsolved externals and
    I cannot fix:

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

  5. Sad guy Says:

    1. Touch command? where is it ? …
    2. After I touched manually I get a message box saying:
    Type Error: components.classes[cid] has no properties.

    I double-checked everything. Same error.
    Later I tried ‘http://developer.mozilla.org/en/docs/Installing_plugins_to_Gecko_embedding_browsers_on_Windows’
    and I get much more errors, unsolved __debug_malloc
    then I downloaded in a brand new folder Alex’s page sample and building that application with VC 7.0 then with 7.1 then with 8.0 express and follwing the install I still get same error:

    Type Error: components.classes[cid] has no properties.

    Based on Alex’s page feedback where the encountered problems are 80/20 with the article I consider that Gecko Gringos should take a break and organize their stuff before messing around so many programmers and provide a functional sample taht covewrs all mozilla releases. I hate Microsoft! but getting an ActiveX working for all IE releases took me 10 minutes.
    That’s why I am so sad. :(

  6. Sad guy - Happy Says:

    FInally WORKS. Mozilla Version 2.0.0.6

    You have to delete compreg.dat file. The file is located in:

    D:\Documents and Settings\USER\Application Data\Mozilla\Firefox\Profiles\pdwxn2sp.default\compreg.dat

    I found all touched files by mozilla at startup running Filemon
    from sysinternals.com

  7. Patrick Madden Says:

    Thanks very much for this. I was able to create my own project and XPCom component in one day with your help!!

    Thanks again,
    PVM

  8. stan Says:

    Oops!, Touch command is part of Cygwin.

    http://cygwin.com/

    I forgot that it’s not standard in windows. You could also open the file in a text editor and re-save it. All you want is to change the ‘modified date’ for the file.

  9. Paul Says:

    Thanks very much. I managed to be able to run xpidl.exe finally.

  10. Harish Says:

    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.

  11. vetrivel r Says:

    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