一、使用c#编写一个COM组件
1.打开VS2012,新建项目-类库,取名叫MyCom,点击确定
2.编辑Class1.cs
using System; using System.Text; using System.Runtime.InteropServices; namespace MyCom { public interface MyInterface { int add(int a, int b); string hello(string name); } public class MyClass : MyInterface { public int add(int a, int b) { return a + b; } public string hello(string name) { return string.Format("Hello, {0}, This is a COM Test", name); } } }
3.右键项目-属性
在【应用程序】中点击【程序集信息】,并勾上【使程序集COM可见】
在【生成】中勾选【为COM互操作注册】
在【签名】中勾选【为程序集签名】,并选择下拉框中的【新建】,密钥名填MyCom,不勾选【使用密码保护密钥文件】,最后点击确定
保存项目属性后,Shift+F6生成,进入bin\Debug目录发现已经生成MyCom.dll、MyCom.pdb、MyCom.tlb
4.注册DLL,regasm MyCom.dll /tlb:MyCom.tlb
5.注册COM,将程序集添加到缓存,gacutil /i MyCom.dll,到此为止,COM组件已经编写完成了。
二、使用Ruby调用COM组件
这里需要使用到Ruby的win32ole库
require "win32ole" com = WIN32OLE.new(‘MyCom.MyClass‘) puts com.add(1,2) puts com.hello(‘Jack‘)
输出的结果如下
3 Hello, Jack, This is a COM Test
根据以上例子,后面将研究利用微软的UIAutomation封装成COM组件,为Ruby提供底层接口,实现轻量级GUI自动化测试工具的开发
c#生成COM组件供Ruby调用,布布扣,bubuko.com
原文:http://www.cnblogs.com/nzgeneral/p/3671945.html