深圳全飞鸿

标题: C#编写COM组件,验证有效 [打印本页]

作者: admin    时间: 2019-6-24 23:01
标题: C#编写COM组件,验证有效
1、新建一个类库项目

2. 项目属性->应用程序->程序集信息->使程序集COM可见

3. 项目属性->生成->为COM互操作注册

4. 切换到“签名”选项卡

  勾选“为程序集签名”

  在下面的下拉框里面选择“<新建...>”

  在弹出的对话框里面,输入MyKey。。或者随便取个名字

  去掉“使用密码保护文件(P)”的选项

5.、开始编码,任何一个公开的类,必须有一个 I开通的接口定义

参考:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;

  5. namespace nagomes.tools.zxing
  6. {
  7.     [ComVisible(true)]
  8.     [Guid("7C49E406-C4A7-4279-909E-83427FB4C8E3")]
  9.     public interface ITrsn_print
  10.     {
  11.         void Initialize();
  12.         void Dispose();
  13.         int Test(int x, int y);
  14.     }

  15.     [ComVisible(true)]
  16.     [Guid("105063F8-6B56-4806-B674-1EFD730F01C0")]
  17.     [ProgId("nagomes_tools_zxing.Trsn_print")]
  18.     public class Trsn_print : ITrsn_print
  19.     {
  20.         public void Initialize()
  21.         {
  22.             // nothing to do
  23.         }

  24.         public void Dispose()
  25.         {
  26.             // nothing to do
  27.         }

  28.         public int Test(int x, int y)
  29.         {
  30.             return x + y;
  31.         }
  32.     }

  33. }
复制代码


作者: admin    时间: 2019-6-24 23:02
delphi 引用:

zhuce:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm nagomes.tools.zxing.dll

  1. uses ComObj;
  2. var
  3. a: olevariant;
  4. i:integer;
  5. begin
  6. a := CreateOleObject('nagomes_tools_zxing.Trsn_print');
  7. i:=a.Test(3,4);
  8. showmessage(inttostr(i));
  9. end;
复制代码

作者: zhgc    时间: 2019-6-26 00:20
delphi调用没有问题,但是在用VB6调用时会报错

  1. Private Sub Command1_Click()
  2. Dim xlApp As Object
  3. Set xlApp = CreateObject("nagomes_tools_zxing.Trsn_print")
  4. MsgBox xlApp.Test(3, 4)
  5. End Sub
复制代码


改为带/codebase参数的注册方式
  1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /u nagomes.tools.zxing.dll
  2. C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase nagomes.tools.zxing.dll
  3. pause
复制代码


但是已明确指出/codebase不能注册没签名的程序 ,不知道后面会不会有问题。暂时xp和win10上可以用!

作者: zhgc    时间: 2019-6-26 00:28
php测试

<?php
ini_set("com.allow_dcom","true");

try {
                        $word = new COM("nagomes_tools_zxing.Trsn_print");
                        print "$word->Add('22','33')";
            }catch (Exception $e){
                    print_r($e->getMessage());
            }
?>
作者: admin    时间: 2019-6-26 00:50
这个注册过程应该好一点:

  1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /u nagomes.tools.zxing.dll
  2. gacutil /i nagomes.tools.zxing.dll
  3. C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm  nagomes.tools.zxing.dll
  4. pause
复制代码


注意gacutil有版本差异,一定要用对应framework4.0的

gacutil下载







欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2