COM组件开发以及调用( C#)

开发工具:Visual Studio 2008,Eclipse3.7 Indigo,Visual C++ 6.0

一、C#编写一个COM组件

1.       打开Visual Studio2008[文件]->[新建]->[项目]

2.       项目类型=Visual C#,模版=类库,名称=MyCom,解决方案=MyCom,点击[确定]

3.       编辑Main.cs

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace MyCom 
  7.  
  8.     public interface MyInterface 
  9.     { 
  10.         int add(int a, int b); 
  11.  
  12.         string sayHello(string msg); 
  13.  
  14.         string mergeString(string a, string b); 
  15.     } 
  16.  
  17.     public class MyClass : MyInterface 
  18.     { 
  19.         public int add(int a, int b) 
  20.         { 
  21.             return a + b; 
  22.         } 
  23.  
  24.         public string sayHello(string msg) 
  25.         { 
  26.             return "Hello, " + msg; 
  27.         } 
  28.  
  29.         public string mergeString(string a, string b) 
  30.         { 
  31.             return a + b; 
  32.         } 
  33.     } 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCom
{

    public interface MyInterface
    {
        int add(int a, int b);

        string sayHello(string msg);

        string mergeString(string a, string b);
    }

    public class MyClass : MyInterface
    {
        public int add(int a, int b)
        {
            return a + b;
        }

        public string sayHello(string msg)
        {
            return "Hello, " + msg;
        }

        public string mergeString(string a, string b)
        {
            return a + b;
        }
    }
}


4.       编辑AssemblyInfo.cs文件

将assembly:ComVisible(false)改为true

  1. // 将 ComVisible 设置为 false 使此程序集中的类型 
  2. // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 
  3. // 则将该类型上的 ComVisible 属性设置为 true。 
  4. [assembly: ComVisible(true)] 
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 属性设置为 true。
[assembly: ComVisible(true)]

5.       编辑项目的属性,右击MyCom选择属性

[应用程序]选项卡中点击[程序集信息…]

勾上[使程序集COM可见],然后点击[确定]

[生成]选项卡上勾上[COM互操作注册]

[签名]选项卡中勾上[为程序集签名],选择下拉框中的<新建…>

秘钥文件名称=MyCom,去掉[使用密码保护密钥文件]的勾,点击[确定],最后保存

6.       生成Dlltld文件,选择菜单栏上的[生成]->[生成MyCom],进入项目的根目录下的bin\Debug目录会发现MyCom.dll, MyCom.pdb, MyCom.tlb文件已经生成

7.       注册MyCom.dll文件,进入SDK命令行,CDMyCom工程的根目录下的bin\Debug目录

然后运行regasm MyCom.dll /tlb:MyCom.tlb命令

下面用regedit命令进入注册表,查看HKEY_CLASSES_ROOT下的MyCom.MyClass已经存在了,则说明已经完成注册。

再继续运行gacutil –i MyCom.dll将程序集添加到缓存中

到此为止MyCom这个COM组件的开发已经完成了。

二、Java调用COM组件,这里还是以调用MyCom为例子

这里需要用到jacob项目来调用,可以事先下载好,我的版本是jacob-1.7-M2版本,将其中的jacob1.17-M2-x64.dlljacob1.17-M2-x86.dll拷贝到windowssystem32目录下,在新建的Java工程中将jacob.jar引入工程,代码示例如下:

  1. public static void main(String[] args) 
  2.     { 
  3.         ActiveXComponent com = new ActiveXComponent("MyCom.MyClass"); 
  4.          
  5.         try 
  6.         { 
  7.             //调用int add(int a, int b); 
  8.             Variant res = Dispatch.call(com, "add",1,2); 
  9.             System.out.println(res.toString()); 
  10.              
  11.             //调用string sayHello(string msg); 
  12.             res = Dispatch.call(com, "sayHello","Scott"); 
  13.             System.out.println(res.toString()); 
  14.              
  15.             //调用string mergeString(string a, string b); 
  16.             res = Dispatch.call(com, "mergeString","Please call me : ", "Scott"); 
  17.             System.out.println(res.toString()); 
  18.         } 
  19.         catch(Exception e) 
  20.         { 
  21.             e.printStackTrace(); 
  22.         } 
  23.     } 
public static void main(String[] args)
	{
		ActiveXComponent com = new ActiveXComponent("MyCom.MyClass");
		
		try
		{
			//调用int add(int a, int b);
			Variant res = Dispatch.call(com, "add",1,2);
			System.out.println(res.toString());
			
			//调用string sayHello(string msg);
			res = Dispatch.call(com, "sayHello","Scott");
			System.out.println(res.toString());
			
			//调用string mergeString(string a, string b);
			res = Dispatch.call(com, "mergeString","Please call me : ", "Scott");
			System.out.println(res.toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

输出结果如下:

  1. 3 
  2. Hello, Scott 
  3. Please call me : Scott 
3
Hello, Scott
Please call me : Scott


三、C++调用COM组件

1. 创建一个MyCom的Win32 Console Application

2.       右击项目,选择Settingwindow改成console点击[OK]

3.       下面将MyCom.dll,MyCom.tlb,MyCom.pdb拷贝到当前工程的根目录下

4.       编写代码,编辑MyCom.cpp文件

  1. #include "stdafx.h" 
  2. #include <iostream.h> 
  3.  
  4. #import "..\MyCom\MyCom.tlb" named_guids raw_interfaces_only 
  5.  
  6. int main(int argc, char* argv[]) 
  7.     CoInitialize(NULL); 
  8.      
  9.     MyCom::MyInterfacePtr ptr; 
  10.      
  11.     ptr.CreateInstance(MyCom::CLSID_MyClass); 
  12.      
  13.     long result = 0; 
  14.     long *res = &result; 
  15.      
  16.     //调用int add(int a, int b) 
  17.     ptr->add(1,2,res); 
  18.     cout << result << endl; 
  19.  
  20.     //调用string sayHello(string msg) 
  21.     BSTR b_msg = _com_util::ConvertStringToBSTR("scott!"); 
  22.     BSTR b_result; 
  23.     BSTR *b_res = &b_result; 
  24.     ptr->sayHello(b_msg,b_res); 
  25.     cout << _com_util::ConvertBSTRToString(b_result) << endl; 
  26.  
  27.     //调用string mergeString(string a, string b) 
  28.     BSTR a = _com_util::ConvertStringToBSTR("I'm "); 
  29.     BSTR b = _com_util::ConvertStringToBSTR(" Scott"); 
  30.     ptr->mergeString(a,b,b_res); 
  31.     cout << _com_util::ConvertBSTRToString(b_result) << endl; 
  32.  
  33.     return 0; 
#include "stdafx.h"
#include <iostream.h>

#import "..\MyCom\MyCom.tlb" named_guids raw_interfaces_only

int main(int argc, char* argv[])
{
	CoInitialize(NULL);
	
	MyCom::MyInterfacePtr ptr;
	
	ptr.CreateInstance(MyCom::CLSID_MyClass);
	
	long result = 0;
	long *res = &result;
	
	//调用int add(int a, int b)
	ptr->add(1,2,res);
	cout << result << endl;

	//调用string sayHello(string msg)
	BSTR b_msg = _com_util::ConvertStringToBSTR("scott!");
	BSTR b_result;
	BSTR *b_res = &b_result;
	ptr->sayHello(b_msg,b_res);
	cout << _com_util::ConvertBSTRToString(b_result) << endl;

	//调用string mergeString(string a, string b)
	BSTR a = _com_util::ConvertStringToBSTR("I'm ");
	BSTR b = _com_util::ConvertStringToBSTR(" Scott");
	ptr->mergeString(a,b,b_res);
	cout << _com_util::ConvertBSTRToString(b_result) << endl;

	return 0;
}


F7编译,Ctrl+F5执行,结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值