Delphi 调用C# 编写的DLL方法
近来,因工作需要,必须解决delphi写的主程序调用c#写的dll的问题。在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:
编写c#dll的方法都一样,首先在vs2005中创建一个“类库”项目testdll,
using system.runtime.interopservices;
namespace testdll
{
public interface i testclass
{
void yourprocedure(stirng param1);
}
[classinterface(classinterfacetype.none)]
public class testclass:i testclass
{
public void yourprocedure (stirng 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