zoukankan      html  css  js  c++  java
  • Delphi 调用C# 编写的DLL方法

    近来,因工作需要,必须解决Delphi写的主程序调用C#写的dll的问题。在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:
        编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目TestDll,
    using System.Runtime.InteropServices;
    namespace TestDll
    {
         public   interface  ITestClass
        {
           void YourProcedure(string param1);
        }
       [ClassInterface(ClassInterfaceType.None)]
        public   class TestClass:I TestClass
        {
           public void YourProcedure (string param1);
           {    //自己的代码    }
        }  
    }
    完成之后,设置项目的属性“Make assembly COM-Visible”为选中状态。编译之后得到 TestClass.dll,把此dll放到Delphi主程序目录下。打开vs2005自带的工具“Visual Studio 2005命令提示”,输入
    Regasm  路径/TestClass.dll 向系统注册此dll。

    Delphi程序调用此Dll方式有两种:
    一、打开vs2005自带的工具“Visual Studio 2005命令提示”,输入 TlbExp  路径/TestClass.dll 得到一个TestClass.tlb 文件。打开Delphi,选择“Project”--“import type library”找到刚才的TestClass.tlb,点击 CreateUnit,向delphi中引入一个com接口。
    delphi 调用代码如下:
      var aClass: TestClass;
      begin
        aClass : =  CoTestClass.Create;
        aClass. YourProcedure ('参数');
      end;
    二、不需生成tlb文件,仿照调用Excel的方式。代码如下:
     var aClass: Variant;
    begin
      aClass:= CreateOleObject('TestDll.TestClass');
      aClass.YourProcedure ('参数');
    end;

    以上两种方法都可以调用成功,其中调用regasm.exe向系统注册dll是必需的。第一种方法需要生成tlb文件,并引入delphi中,操作繁琐,但可以看到接口的定义。第二种方法操作简单,但看不到接口的定义。

    转:http://blog.csdn.net/genispan/article/details/4294487

  • 相关阅读:
    call()与apply()的作用与区别
    Tomcat8/9的catalina.out中文乱码问题解决
    怎样查看Jenkins的版本
    每日日报2020.8.18
    528. Random Pick with Weight
    875. Koko Eating Bananas
    721. Accounts Merge
    515. Find Largest Value in Each Tree Row
    286. Walls and Gates (Solution 1)
    408. Valid Word Abbreviation
  • 原文地址:https://www.cnblogs.com/hhmm99/p/11753749.html
Copyright © 2011-2022 走看看