打印本文 打印本文 关闭窗口 关闭窗口
XPCOM--LINUX下的组件开发技术
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2839  更新时间:2009/4/23 10:51:32  文章录入:mintao  责任编辑:mintao
  NS_IMETHOD Hello(const char *in_str, char **out_str) { return !_to ? NS_ERROR_NULL_POINTER : _to->Hello

(in_str, out_str); }

 

#if 0

/* Use the code below as a template for the implementation class for this interface. */

 

/* Header file */

class nsMyCom : public nsIMyCom

{

public:

  NS_DECL_ISUPPORTS

  NS_DECL_NSIMYCOM

 

  nsMyCom();

  virtual ~nsMyCom();

  /* additional members */

};

 

/* Implementation file */

NS_IMPL_ISUPPORTS1(nsMyCom, nsIMyCom)

 

nsMyCom::nsMyCom()

{

  /* member initializers and constructor code */

}

 

nsMyCom::~nsMyCom()

{

  /* destructor code */

}

 

/* void Hello (in string in_str, [retval] out string out_str); */

NS_IMETHODIMP nsMyCom::Hello(const char *in_str, char **out_str)

{

    return NS_ERROR_NOT_IMPLEMENTED;

}

 

/* End of implementation class template. */

#endif

 

 

#endif /* __gen_nsIMyCom_h__ */

//---------------------------------------------------------

 

从上面可以看到, xpidl生成了对应该接口的头文件,同时还包括对该头文件实现的C++类模板.下一步的工作一样很轻松,

我们将#if 0 至#endif 之间的代码分别复制到新建的nsMyCom.h 和nsMyCom.cpp文件中,

注意其中有新增的代码,下面是生成的两个文件.

//filename: nsMyCom.h

#include "nsImyCom.h"

 

#define NS_MYCOM_CID /

  {0x5217115e, 0x22fe, 0x4d01, { 0x96, 0x6d, 0x9b, 0x27, 0xff, 0xda, 0x64, 0x98 }}     

  //类似WINDOWS 中CLSID

 

 

#define NS_MYCOM_CONTRACTID "@westsoft.org/mycom;1"   //类似WINDOWS中的progid;

 

class nsMyCom : public nsIMyCom

{

public:

  NS_DECL_ISUPPORTS

  NS_DECL_NSIMYCOM

 

  nsMyCom();

  virtual ~nsMyCom();

  /* additional members */

};

 

 

 

//filename: nsMyCom.cpp

#include "nsMyCom.h"

#include "nsMemory.h"

#include <cstdio>

#include <cstdlib>

#include <string>

NS_IMPL_ISUPPORTS1_CI(nsMyCom, nsIMyCom)      //此处的宏已修改.

nsMyCom::nsMyCom()

{

}

 

nsMyCom::~nsMyCom()

{

}

 

/* void Hello (in string in_str, [retval] out string out_str); */

NS_IMETHODIMP nsMyCom::Hello(const char *in_str, char **out_str)

{

      printf("\n-----------------\n");

      printf("%s\n", in_str);

      std::string str_tmp = "your input is: ";

      str_tmp += in_str;

      *out_str = (char*)malloc(str_tmp.length() + 1);

      *out_str = (char*)str_tmp.c_str();

 

      return NS_OK;

}

 

4、完成组件的工厂方法及注册模块。

组件本身的实现就上面两个类即可以了. 但是我们仅把上面的类生成动态库是不能作为组件工作的,我们还需要做一件事情.实现组件的注册及创建相关的功能.这几乎是一个固定的模式.

 

下面是该部分的代码,跟MS中的实现类似,用了许多的宏:

#include "nsIGenericFactory.h"

#include "nsMyCom.h"

 

NS_GENERIC_FACTORY_CONSTRUCTOR(nsMyCom)

 

static NS_METHOD nsMyComRegistrationProc(nsIComponentManager *aCompMgr,

nsIFile *aPath, const char *registryLocation, const char *componentType, const nsModuleComponentInfo

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口