C#.NET com组件的编写

环境:VS2010...WIN7系统
设置工程属性:“生成”-> “为COM Interop注册”。
当然也可以为每个接口设置COM可见性,ComVisibleAttribute类提供了这样的控制。
更改AssemblyInfo.cs……设置COM可见

// 将? ComVisible 设Θ?置?为a false 使?此?程ì序ò集ˉ中D的?类え?型í
// 对? COM 组哩?件t不?可é见?。£如?果?需è要癮从洙?COM 访?问ê此?程ì序ò集ˉ中D的?类え?型í,?
// 则ò将?该?类え?型í上?的? ComVisible 特?性?设Θ?置?为a true。£
[assembly: ComVisible(true)]

建立源码

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MyLib
{
    [ComVisible(true)]
    [Guid("2CBD3D76-35F1-4f9d-9C1B-9DBFEE412F77")]
    public interface IMyClass
    {
        void Initialize();
        void Dispose();
        String VC(String path);
    }

    [ComVisible(true)]
    [Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0e")]
    [ProgId("MyLib.MyClassa")]
    public class MyClass : IMyClass
    {
        public void Initialize()
        {
            //nothing todo
        }
        public void Dispose()
        {
            //nothing todo
        }
        public String VC(String path)
        {
           //  x + y;
            if (path.Trim().Equals(""))
            {

                return "no pathx";
            }

            fastvcAtiLib.fastvcyjmC2k c = new fastvcAtiLib.fastvcyjmC2k();
            //   c.novc = true;   //4 test .only show pic .not show vercode text
            // c.oriImg = true;   //img not process and get vercode... cant echo show img pic ..
            c.vg = 1;
            c.sid = "c2n";
          
            string VerifyCode;
            try
            {
                VerifyCode = c.RecByte(path);
            }
            catch (Exception ex)
            {
                VerifyCode = ex.Message;
            }
            return VerifyCode;
        }
    }
}
注册COM组件DLL
regasm   ClassLibrary1.dll /codebase

如果你的DLL不 是强签名的,则必须要使用/codebase参数

反注册使用
regasm  /u ClassLibrary1.dll

使用HTML中VBS脚本测试 

<script language="VBScript">
Dim o : Set o=CreateObject("MyLib.MyClass")  
  o.Initialize
    MsgBox "1 + 2 = " & o.vc("c:\dx.jpg")
    o.Dispose
    Set o=Nothing
</script>
提示不能创建 ACTIVEX 控件……

原因:是因为注册没有注册上……

提示找不到路径

说明已经注册上了。可能DLL不是强签名的,而又没有指定/CODEBASE属性,导致注册里没有这个键值。而查找不到。

转载于:https://blog.51cto.com/13243523/2044647

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值