I. Basic CONCEPTS
Xpcom is a cross-platform component model, all of which is called Cross Platform Component Object Module. Xpcom implements a framework (framework) that allows developers to break a single, monolithic software project and break down into smaller modular fragments (pieces), which are also components (components). Xpcom component development is similar to a Microsoft COM component, with COM based, easy to learn xpcom.
Second, build the development environment
Xpcom development requires the Gecko SDK, as the Gecko SDK is included in the XULRunner, so as long as the XULRunner SDK can be downloaded for development, the following is still the development environment with XULRunner 2.0.
Third, create the first xpcom component
1, new Isamplecomponents.idl file, enter the following code:
The code is as follows |
Copy Code |
#include "Nsisupports.idl"
[Scriptable, UUID (7F35DE58-C0E3-4480-9CA0-EB2A83F1B9CE)] Interface Isamplecomponents:nsisupports { Long Sum (in long Afirst, in long Asecond); }; |
The code is as follows |
Copy Code |
D:xulrunner2.0bin>xpidl-m header-i ". IDL "Isamplecomponents.idl D:xulrunner2.0bin>xpidl-m typelib-i ". IDL "Isamplecomponents.idl |
The code is as follows |
Copy Code |
/*samplecomponents.h */ #pragma once #ifndef _sample_components_h_ #define _SAMPLE_COMPONENTS_H_ #include "ISampleComponents.h" #define Sample_components_contractid @lintclr. com/ Isamplecomponents;1 " #define sample_components_cid {0x7f35de58, 0xc0e3, 0x4480, {0x9c, 0xa0, 0xeb, 0x2a , 0x83, 0xf1, 0XB9, 0xce}}; class samplecomponents:public isamplecomponents { public: Ns_decl_isupports ns_decl_isamplecomponents samplecomponents (); &NBSP private: ~samplecomponents (); }; #endif |
Where the UUID is a binary length 128-bit numeric identifier generated by a specific algorithm that indicates the uniqueness of the interface, can be produced by the Microsoft GUID tool, Nsisupports This interface is the most basic interface, we inherit this interface, Add a sum method two long type parameter and return type long.
2. Compile Isamplecomponents.idl file
Copy the file to the Xulrunner2.0bin directory, enter the directory at the command line, and type the following command:
If there are no errors in both lines, the ISampleComponents.h and isamplecomponents.xpt two files are created.
3, the use of VS2010 development components
(1) Create a new Win32 DLL empty project and place the three files in the second step in the project directory and add ISampleComponents.h to the project.
(2) Then create a new SampleComponents.h and SampleComponents.cpp two files, which read as follows:
SampleComponents.cpp
(4) Create a new SampleComponentsModule.cpp file and type the following code:
(5) Configuring compilation options, compiling components
Configuration Properties->vc++ Directory-> Additional include directory: D:xulrunner2.0include;
Configuration Properties->vc++ Directory-> Library directory: d:xulrunner2.0lib;
Configuration Properties->c/c++-> Preprocessor-> preprocessing definition: xpcom_glue
Xp_win
Xp_win32
Xpcom_glue_use_nspr
Configuration Properties-> linker-> Enter-> Additional dependencies: Nspr4.lib
Xpcom.lib
Xpcomglue_s_nomozalloc.lib
Click Build to generate component success.
code is as follows |
copy code |
/*samplecompo nents.cpp*/ #include "SampleComponents.h" ns_impl_isupports1 (samplecomponents, isamplecomponents) samplecomponents::samplecomponents () { } &NBSP samplecomponents::~samplecomponents () { } ns_ Imethodimp samplecomponents::sum (PRInt32 afirst PRInt32 asecond, PRInt32 *_retval ns_outparam) { & nbsp; *_retval = Afirst + Asecond; return NS_OK; } |
The code is as follows |
Copy Code |
#include "Mozilla/moduleutils.h" #include "nsIClassInfoImpl.h" #include "SampleComponents.h"
Ns_generic_factory_constructor (samplecomponents)
Ns_define_named_cid (SAMPLE_COMPONENTS_CID);
static const Mozilla::module::cidentry ksamplecomcids[] = { {&ksample_components_cid, False, NULL, samplecomponentsconstructor}, {NULL} };
static const Mozilla::module::contractidentry ksamplecontracts[] = { {Sample_components_contractid, &ksample_components_cid}, {NULL} };
static const Mozilla::module::categoryentry ksamplecategories[] = { {"My-category", "My-key", Sample_components_contractid}, {NULL} };
static const Mozilla::module Ksamplemodule = { Mozilla::module::kversion, Ksamplecomcids, Ksamplecontracts, Ksamplecategories };
Nsmodule_defn (nssamplemodule) = &kSampleModule; Ns_impl_mozilla192_nsgetmodule (&ksamplemodule) |