Curiosity is bliss    Archive    Feed    About    Search

Julien Couvreur's programming blog and more

How to write C++ XPCOM components

 

I've been playing around with XPCOM to extend the functionality of Firefox. "Creating XPCOM components" is the best step by step introduction I have found so far. It details how to develop a C++ XPCOM component using windows and Visual Studio (2003).
Here's the summary of the steps:

  1. Download the Gecko SDK. It allows you to write XPCOM components without building Mozilla itself (which I still didn't manage to do :-[ ).
  2. Start an empty Win32 project in Visual Studio, selecting the "Dynamic Linked Library (DLL)" option.
  3. Create and IDL file describing the interface of your component.
  4. Run xpidl twice on the IDL file, with "xpidl -I <geckosdk/idl path> -m header <yourIDL file>" and "xpidl -I <geckosdk/idl path> -m typelib <yourIDL file>".
  5. Create a cpp file that includes the generated header file (which includes a a handy template for your class).
  6. Implement the main entry point (NSGetModule) and the XPCOM module and factory, as well as your own component (matching the signature in the generated header file). The tutorial provides a sample file which you can re-use (but there are some little code mistakes, see errata section).
  7. Configure Visual Studio to search headers in the Gecko SDK/include directory (with the "Additional Include Directories" setting in the "C/C++"->"General" tab). Do the same thing to include the Gecko SDK/lib directory (using the "Additional Library Directories" setting in the "Linker"->"General" tab).
  8. Build your component.
  9. Add a post-build event in Visual Studio to copy the XPCOM dll to Firefox's components directory with "copy $(OutDir)\<yourXPCOM>.dll <FirefoxDir>\components".
  10. Copy the xpt file (typelib) generated by xpidl to the Firefox components directory.
  11. Run regxpcom with "regxpcom -x <GeckoSDK>\bin <FireFoxDir>\components\<yourXPCOM>.dll".
  12. You can optionally configure Visual Studio to run and debug regxpcom to troubleshoot the previous step (thru the "Command", "Command Arguments" and "Working Directory" in the "Debugging" tab). It'll allow you to verify your dll gets loaded. The NSGetModule method makes for a good first breakpoint.
  13. Build a simple unittest html file, using "netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");" to get access to XPConnect/XPCOM. Use the Components object to create an instance of your component.
  14. Attach to the running Firefox.exe process to debug the component.

General thought:
Java, Perl and Python can interoperate with XPCOM (see Blackwood for Java, PyXPCOM for Python and PlXPCOM for Perl). I wonder how difficult it would be to support .Net? You would be able to call XPCOM from .Net and implement XPCOM components using in .Net.

______________________________________

Sorry, it don't work with me.
I have an exception when I run my javascipt ("classe has no property")

Posted by: sim (January 1, 2005 03:42 AM) ______________________________________

Erm so where's the errata section?

Posted by: Blurhead (February 15, 2005 11:59 PM) ______________________________________

I can't seem to find the errata anymore. It may have been integrated into an updated version of the main body?

Posted by: Julien Couvreur (February 17, 2005 10:20 AM) ______________________________________

http://www.mozilla.org/projects/xpcom/book/cxc/errata.html

Posted by: Craig (February 19, 2005 04:23 PM) ______________________________________

same as sim.
it doesn't work.
javasript throw an exception "classe has no property"

Posted by: sky_water (July 15, 2005 04:58 AM) ______________________________________

i want know how to change functionality of fireox
using xpcom in linux any one can tell me which step i should follow to create my own components....
thanx in advance.....

Posted by: patil balaji (June 21, 2006 05:33 AM) ______________________________________

Can anyone help me in integrating flash player in XPCOM.

Posted by: Amol (July 26, 2006 05:14 AM) ______________________________________

Abv details about xpcom in c++ is very clear and good......

PLZ give details abt " How to write XPCOM component in JAVA"

Posted by: FPK (August 17, 2006 10:27 PM)
comments powered by Disqus