首页 > 其他 > 详细

c#生成COM组件供Ruby调用

时间:2014-04-18 04:19:49      阅读:2036      评论:0      收藏:0      [点我收藏+]

一、使用c#编写一个COM组件

1.打开VS2012,新建项目-类库,取名叫MyCom,点击确定

2.编辑Class1.cs

bubuko.com,布布扣
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);
        }
    }
}
bubuko.com,布布扣

3.右键项目-属性

在【应用程序】中点击【程序集信息】,并勾上【使程序集COM可见】

bubuko.com,布布扣

在【生成】中勾选【为COM互操作注册】

bubuko.com,布布扣

在【签名】中勾选【为程序集签名】,并选择下拉框中的【新建】,密钥名填MyCom,不勾选【使用密码保护密钥文件】,最后点击确定

bubuko.com,布布扣

保存项目属性后,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

c#生成COM组件供Ruby调用

原文:http://www.cnblogs.com/nzgeneral/p/3671945.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!