【转自元宝兄】关于delphi Com+调用C# DLL的一点说明
最近公司进行产品改造,由于存在部分代码是用delphi写的Com+需要调用C#写的Dll方法,经过网上一些搜索和摸索,调用已经OK。但是至于两者之间的事务问题还需要用到的朋友深入研究。
现在来说一下调用过程:【本地调试通过,环境是XP,delphi7.0,vs2005】
首先在vs2005中创建一Class Library项目,添加2个cs文件,代码分别为:
声明一个接口
![](https://images.cnblogs.com/cnblogs_com/baitl/pic/c.JPG)
注意红线标示的部分。
然后对此编译成功的DLL【beep_Class.dll】进行处理,打开vs2005自带的命令行工具。输入 tlbexp beep_Class.dll
生成 beep_Class.tlb文件。
元宝注解:
这一步有个更好的方法,即在VS2005中DLL的“项目属性”下“生成事件”添加“生成后事件命令行”:
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\tlbexp" "$(TargetPath)"
下一步是打开delphi7,新建一个Application,在Form上增加一Button。然后选择Project下的,import type library,把刚才生成的Tlb文件【beep_Class.tlb】添加进来,然后点击 CreateUnit就ok了
delphi中的Button事件代码如下:
![](https://images.cnblogs.com/cnblogs_com/baitl/pic/a.JPG)
注意:运行的时候要把Beep_Class.dll放在程序目录中。
现在来说一下调用过程:【本地调试通过,环境是XP,delphi7.0,vs2005】
首先在vs2005中创建一Class Library项目,添加2个cs文件,代码分别为:
声明一个接口
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
namespace beep_Class
5
{
6
public interface IGO
7
{
8
string GO();
9
}
10
}
实现该接口![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
using System.Runtime.InteropServices;
2
namespace beep_Class
3
{
4
[ClassInterface(ClassInterfaceType.None)]
5
public class Class1:IGO
6
{
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
public string GO()
9
{
10
return "aaaaabbbb";
11
}
12
}
13
}
然后在生成类库之前设置一下该项目的属性,如下图所示:![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
注意红线标示的部分。
然后对此编译成功的DLL【beep_Class.dll】进行处理,打开vs2005自带的命令行工具。输入 tlbexp beep_Class.dll
生成 beep_Class.tlb文件。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下一步是打开delphi7,新建一个Application,在Form上增加一Button。然后选择Project下的,import type library,把刚才生成的Tlb文件【beep_Class.tlb】添加进来,然后点击 CreateUnit就ok了
delphi中的Button事件代码如下:
1
procedure TForm1.Button1Click(Sender: TObject);
2
var
3
co:Class1;
4
a:string;
5
begin
6
co:= CoClass1.Create;
7
a:=co.GO();
8
showmessage(a);
9
end;
10
end.
编译通过,运行结果如下图:![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
注意:运行的时候要把Beep_Class.dll放在程序目录中。