查看文章 |
创建准备1、下载SDK http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.7/ 从如上路径,选择最近的版本下载 2、创建GUID 使用VC下的guidgen 生成GUID,例如 3、创建接口文件定义 #include "nsISupports.idl"
[scriptable, uuid( interface IMyComponent : nsISupports { long Add(in long a, in long b); };
创建控件1、使用SDK下的xpidl.exe 在CMD中输入命令 xpidl -m header -I_DIR_ IMyComponent.idl will create the IMyComponent.h header file xpidl -m typelib -I_DIR_ IMyComponent.idl will create the IMyComponent.xpt typelib file 请注意如果是在其他目录下,请在CMD里把SDK下的目录加在环境变量里,
D:\Program Files\Microsoft Visual Studio8\VC>PATH = D:\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\bin;D:\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\idl;D:\gecko-sdk-i586-pc-msvc-1.8b1\gecko-sdk\include;%PATH%
或者可以在“我的电脑-属性-高级-环境变量”里修改path值。
如果不成功,请把xpidl,IMyComponent.idl,全部写成完整路径。
在这一步可能会出现缺少DLL的现象。 如出现此类情况,请在这里下载 http://ftp.mozilla.org/pub/mozilla.org/mozilla/source/wintools.zip 解压缩后请按照同样方法把…\wintools\buildtools\windows\bin\x86加如环境变量中。
执行通过,得到IMyComponent.h,IMyComponent.xpt 2个文件。
2、创建新文件 根据IMyComponent.h创建文件MyComponent.h,MyComponent.cpp,MyComponentModule.cpp。
/* MyComponent.h*/ #ifndef _MY_COMPONENT_H_ #define _MY_COMPONENT_H_
#include "IMyComponent.h"
#define MY_COMPONENT_CONTRACTID "@mydomain.com/XPCOMSample/MyComponent;1" #define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample" #define MY_COMPONENT_CID { 0x
/* Header file */ class MyComponent : public IMyComponent { public: NS_DECL_ISUPPORTS NS_DECL_IMYCOMPONENT
MyComponent(); virtual ~MyComponent(); /* additional members */ };
#endif //_MY_COMPONENT_H_
/* MyComponent.cpp*/ #include "MyComponent.h"
NS_IMPL_ISUPPORTS1(MyComponent, IMyComponent)
MyComponent::MyComponent() { /* member initializers and constructor code */ }
MyComponent::~MyComponent() { /* destructor code */ }
/* long Add (in long a, in long b); */ NS_IMETHODIMP MyComponent::Add(PRInt { *_retval = a + b; return NS_OK; }
/* MyComponentModule.cpp*/
#include "nsIGenericFactory.h" #include "MyComponent.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)
static nsModuleComponentInfo components[] = { { MY_COMPONENT_CLASSNAME, MY_COMPONENT_CID, MY_COMPONENT_CONTRACTID, MyComponentConstructor, } };
NS_IMPL_NSGETMODULE("MyComponentsModule", components)
编译控件1、创建工程 使用VC,创建新的DLL工程,将IMyComponent.h, MyComponent.h,MyComponent.cpp,MyComponentModule.cpp添加到工程中。
2、工程配置 在tools->options->Directories中的include files和library files中将SDK中的include文件夹与lib文件夹添加进去。 在Project ->setting->link->input下添加4个lib文件:nspr4.lib, plds4.lib ,plc4.lib ,xpcomglue.lib 在Project ->setting->C++ ->general->Preprocessor definitions添加MYCOMPONENT_EXPORTS,XPCOM_GLUE
3、编译 生成MyComponent.dll,将其与IMyComponent.xpt放入Mozilla Firefox\components\文件夹下。
运行控件1、 创建HTML文件 /* MyComponentTest.html */ <HTML> <SCRIPT> var FLDR_COMPONENTS = getFolder("Components"); var FLDR_PLUGINS = getFolder("Plugins"); var FLDR_PREFS = getFolder("Program","defaults/pref"); var FLDR_WINSYS = getFolder("Win System");
function MyComponentTestGo() { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); const cid = "@mydomain.com/XPCOMSample/MyComponent;1"; obj = Components.classes[cid].createInstance(); obj = obj.QueryInterface(Components.interfaces.IMyComponent); } catch (err) { alert(err); return; } var res = obj.Add(3, 4); alert('Performing 3+4. Returned ' + res + '.'); } </SCRIPT> <BODY> <BUTTON ONCLICK="MyComponentTestGo();">Go</BUTTON> </BODY> </HTML>
将如上页面放在服务器上。
2、 测试页面 打开火狐浏览器,访问该页面,弹出相应结果。
|