COM组件开发以及调用

本文介绍了如何使用C#开发COM组件,并详细步骤演示了用Java和C++调用该组件的过程。首先在Visual Studio 2008中创建并配置C# COM组件,然后通过regasm和gacutil完成注册和程序集添加到缓存。接着利用jacob库在Java中调用组件,最后展示了在C++ Win32 Console Application中调用组件的代码实现。

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

一、C#编写一个COM组件

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

 

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

 

3.       编辑Main.cs

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

// 将 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引入工程,代码示例如下:

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();
		}
	}

输出结果如下:

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文件

#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执行,结果如下:

 

 

 

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值