Compile the XPCOM component in C ++
Let's take a look at the examples provided on the official website and download the sample program xpcom-test.
See the article https://developer.mozilla.org/en/How_to_build_a_binary_XPCOM_component_using_Visual_Studio for configuration methods
There is no need to download the Gecko-SDK in this article. If you use a newer version of xul-runner, you can use its SDK,
If you do not use the Gecko-SDK, you can directly use the xul-runner SDK. You need to modify some project configurations:
1. In the project properties, "The path of the additional include directory is (xul-runner dirpath)/sdk/include," The path of the additional library is "(xul-runner dirpath)/sdk/lib
2. Because XPCOM needs to be generated using the IDL tool. h and. xpt file, each time using the command line method is cumbersome, you can use the batch processing file in this routine, the xpidl-build.bat file in the example program to execute this job, set custom build steps in the project properties-> command line-> xpidl-build.bat, which is automatically generated when the project is compiled. h and. xpt File
Compile the project and generate the. dll file and. xpt file. Now let's take a look at how to use this component.
Test Components
The testing component method introduced in this article is too simple to understand. After continuous research, I provide the following two methods:
Method 1: Call through web pages
1. Place the DLL and xpt files in the components directory of Firefox. In Windows, the path is C:/program files/Mozilla Firefox/components.
2. Delete the registration files compreg. dat and xpti. dat under profiles. The path of profiles under Windows is % appdata %/Mozilla/Firefox/profiles/**. default.
3. Restart firefoxand open the xpcom-test.html webpage.
Xpcom-test.html code
<HTML> <br/> <SCRIPT> <br/> function test () {<br/> try {<br/> // normally Firefox extensions implicitly have XPCOM privileges, <br/> // but since this is a file we have to request it. <br/> Netscape. security. privilegemanager. enableprivilege ("universalxpconnect"); <br/> const cid = "@ starkravingfinkle.org/specialthing1_1"; <br/> var OBJ = components. classes [CID]. createinstance (); <br/> OBJ = obj. queryInterface (components. interfaces. ispecialthing); <br/>}< br/> catch (ERR) {<br/> alert (ERR); <br/> return; <br/>}< br/> var res = obj. add (3, 4); <br/> alert ('3 + 4 = '+ Res); <br/> var name = obj. name; <br/> alert ('name = '+ name); <br/> obj. name = 'new name'; <br/> name = obj. name; <br/> alert ('name = '+ name ); <br/>}< br/> </SCRIPT> <br/> <body> <br/> <input type = "button" value = "test" onclick = "Test (); "> <br/> </body> <br/> </ptml>
Note: Netscape. security. privilegemanager. enableprivilege ("universalxpconnect"); this sentence must be added; otherwise, it will have no effect. If this sentence is left blank in the afternoon, it cannot be called, this statement is used to authorize the API that allows us to access the browser.
Method 2: Use xulrunner
You need to create the following directories and files on the disk:
The {appname} name can be written at will, that is, a name can be given to the directory as the root directory. The "|" used below indicates the depth of the directory.
|-Application. ini
|-Chrome
| -- Chrome. manifest
| -- XPCOM-test
| --- Test. XUL
|-Defaults
| -- Preferences
| --- Prefs. js
|-The compiled DLL and xpt files are put under the components directory.
| -- Comp. xpt
| -- Test. dll
Application. ini file
[App] <br/> Vendor = AliciaLy <br/> Name = xpcomtest <br/> Version = 3.0.5 <br/> BuildID = 201002251 <br/> [Gecko] <br /> MinVersion = 1.9.0.5 <br/> MaxVersion = 1.9.1. *
Chrome. manifest
Content tests xpcom-test/
Test. xul
<? Xml version = "1.0"?> <Br/> <? Xml-stylesheet href = "chrome: // global/skin/" type = "text/css"?> <Br/> <window id = "controller-example" title = "Controller Example" <br/> xmlns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <br/> <mce: script type = "text/javascript"> <! -- <Br/> function callXPCOM () {<br/> try {<br/> const cid = "@ starkravingfinkle.org/specialthing1_1"; <br/> var obj = Components. classes [cid]. createInstance (); <br/> obj = obj. queryInterface (Components. interfaces. ISpecialThing); <br/>}< br/> catch (err) {<br/> alert (err); <br/> return; <br/>}< br/> var res = obj. add (3, 4); <br/> alert ('3 + 4 = '+ res); <br/> var name = obj. name; <br/> alert ('name = '+ Name); <br/> obj. name = 'new name'; <br/> Name = obj. name; <br/> alert ('name = '+ Name); <br/>}< br/> // --> </mce: script> <br/> <button id = "testXPCOM" label = "Test XPCOM" oncommand = "callXPCOM (); "/> <br/> </window> <br/>
If the control needs to be displayed in Chinese in this file, it must be saved as UTF-8
Prefs. js
Pref ("toolkit. defaultChromeURI", "chrome: // tests/content/test. xul ");
After these files are written, use the command line to enter the xulrunner.exe directory, where xulrunner-sdk/bin
Enter the xulrunner {appname dir}/application. ini command.
A new window with buttons appears in the upper-left corner of the window.