[整理]c#简单调用DELPHI DLL封装窗体
2008-10-13 14:15
615 查看
DELPHI DLL部分代码
Form代码
var
2 Form1: TForm1;
3 procedure SynAPP(App:THandle);stdcall;
4 procedure ShowForm;stdcall;
5
6implementation
7
8uses Unit2;
9
10{$R *.dfm}
11
12procedure SynAPP(App:THandle );stdcall;
13begin
14 Application.Handle := App;
15end;
16
17procedure ShowForm;stdcall;
18begin
19 Form1 := TForm1.Create (Application);
20 Form1.ShowModal;
21 FreeAndNil(Form1);
22end
Class
Function MyString(s:PChar):PChar;stdcall;
2 begin
3 Result :=s;
4 end;
5
6Function MyMax ( X , Y : integer ) : integer ; stdcall ;
7 begin
8 if X > Y then
9 Result := X
10 else
11 Result := Y ;
12 end ;
13
14
15
16{$R *.res}
17
18exports
19 MyString ,
20 MyMax ,
21
22 SynAPP ,
23 ShowForm ;
C#代码
public partial class HelloWord_Transfer_Delphi_DLL : Form
2 {
3 public HelloWord_Transfer_Delphi_DLL()
4 {
5 InitializeComponent();
6 }
7
8 //定义DLL文件名,此文件路径要加到系统Path中
9 private const string _fileDll = @"D:\Demo\Delphi Demo\HelloWord_DLL\HelloWord_DLL.dll";
10 //调用非托管Dll,MyMax是ChartAccess.dll公开的函数名称
11 [DllImport(_fileDll, EntryPoint = "MyMax", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
12 //C#中的申明
13 public static extern int MyMax(int i,int j);
14
15
16 [DllImport(_fileDll, EntryPoint = "MyString", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
17 public static extern string MyString(string s);
18
19 [DllImport(_fileDll, EntryPoint = "SynAPP", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
20 public static extern string SynAPP(IntPtr i);
21
22 [DllImport(_fileDll, EntryPoint = "ShowForm", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
23 public static extern string ShowForm();
24
25
26 private void button1_Click(object sender, EventArgs e)
27 {
28 MessageBox.Show(MyMax(10, 100).ToString());
29 }
30
31 private void button2_Click(object sender, EventArgs e)
32 {
33 MessageBox.Show(MyString("aaaaad"));
34 }
35
36 private void button3_Click(object sender, EventArgs e)
37 {
38 SynAPP(this.Handle);
39 ShowForm();
40 }
41 }
Form代码
var
2 Form1: TForm1;
3 procedure SynAPP(App:THandle);stdcall;
4 procedure ShowForm;stdcall;
5
6implementation
7
8uses Unit2;
9
10{$R *.dfm}
11
12procedure SynAPP(App:THandle );stdcall;
13begin
14 Application.Handle := App;
15end;
16
17procedure ShowForm;stdcall;
18begin
19 Form1 := TForm1.Create (Application);
20 Form1.ShowModal;
21 FreeAndNil(Form1);
22end
Class
Function MyString(s:PChar):PChar;stdcall;
2 begin
3 Result :=s;
4 end;
5
6Function MyMax ( X , Y : integer ) : integer ; stdcall ;
7 begin
8 if X > Y then
9 Result := X
10 else
11 Result := Y ;
12 end ;
13
14
15
16{$R *.res}
17
18exports
19 MyString ,
20 MyMax ,
21
22 SynAPP ,
23 ShowForm ;
C#代码
public partial class HelloWord_Transfer_Delphi_DLL : Form
2 {
3 public HelloWord_Transfer_Delphi_DLL()
4 {
5 InitializeComponent();
6 }
7
8 //定义DLL文件名,此文件路径要加到系统Path中
9 private const string _fileDll = @"D:\Demo\Delphi Demo\HelloWord_DLL\HelloWord_DLL.dll";
10 //调用非托管Dll,MyMax是ChartAccess.dll公开的函数名称
11 [DllImport(_fileDll, EntryPoint = "MyMax", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
12 //C#中的申明
13 public static extern int MyMax(int i,int j);
14
15
16 [DllImport(_fileDll, EntryPoint = "MyString", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
17 public static extern string MyString(string s);
18
19 [DllImport(_fileDll, EntryPoint = "SynAPP", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
20 public static extern string SynAPP(IntPtr i);
21
22 [DllImport(_fileDll, EntryPoint = "ShowForm", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
23 public static extern string ShowForm();
24
25
26 private void button1_Click(object sender, EventArgs e)
27 {
28 MessageBox.Show(MyMax(10, 100).ToString());
29 }
30
31 private void button2_Click(object sender, EventArgs e)
32 {
33 MessageBox.Show(MyString("aaaaad"));
34 }
35
36 private void button3_Click(object sender, EventArgs e)
37 {
38 SynAPP(this.Handle);
39 ShowForm();
40 }
41 }
相关文章推荐
- [轉]c#简单调用DELPHI DLL封装窗体
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- Delphi程序调用C#.Net编译的DLL并打开窗体(详解)
- delphi 调用 C# COM DLL 解析C#数据集
- Delphi 中的DLL 封装和调用对象技术
- C#调用Delphi的dll的两种方法
- C#调用C++写的DLL的简单示例
- Delphi调用DLL文件里的窗体
- C#调用C++Dll封装时遇到的一系列问题
- 关于delphi Com+调用C# DLL的一点说明
- windows服务的托管资源(C#) 静态调用 非托管资源(Delphi-dll) 开机造成无法进入桌面(一)
- 关于Delphi中DLL封装对象的几种方法整理及体会
- 存储过程中调用C#自定义封装的DLL
- C#.NET dll封装(包含UI和实现函数),通过反射调用dll实现控件的快速加载(PrimPointListCreator.cs)
- C# 如何调用 Delphi 编写的 DLL 方法
- delphi如何调用C#的dll?
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- Delphi调用C#dll的问题
- 转:C#调用C++的DLL搜集整理的所有数据类型转换方式