查看文章
 
利用VC创建XPCOM组件
2007-03-19 16:23

创建准备

1、下载SDK

http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.7/

从如上路径,选择最近的版本下载

2、创建GUID

使用VC下的guidgen 生成GUID,例如90758A97-A6F3-4ea4-8953-16BD2EE3A977

3、创建接口文件定义

#include "nsISupports.idl"

[scriptable, uuid(90758A97-A6F3-4ea4-8953-16BD2EE3A977)]

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值。

如果不成功,请把xpidlIMyComponent.idl,全部写成完整路径。

在这一步可能会出现缺少DLL的现象。

如出现此类情况,请在这里下载

http://ftp.mozilla.org/pub/mozilla.org/mozilla/source/wintools.zip       

解压缩后请按照同样方法把…\wintools\buildtools\windows\bin\x86加如环境变量中。

执行通过,得到IMyComponent.hIMyComponent.xpt 2个文件。

2、创建新文件

根据IMyComponent.h创建文件MyComponent.hMyComponent.cppMyComponentModule.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  { 0x597a60b0, 0x5272, 0x4284, { 0x90, 0xf6, 0xe9, 0x6c, 0x24, 0x2d, 0x74, 0x6 } }

/* 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(PRInt32 a, PRInt32 b, PRInt32 *_retval)

{

    *_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.hMyComponent.cppMyComponentModule.cpp添加到工程中。

2、工程配置

tools->options->Directories中的include fileslibrary files中将SDK中的include文件夹与lib文件夹添加进去。

Project ->setting->link->input下添加4lib文件: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、  测试页面

打开火狐浏览器,访问该页面,弹出相应结果。


类别:火狐插件制作||添加到搜藏 |分享到i贴吧|浏览(2820)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu