您的位置:首页 > 编程语言 > Delphi

[整理]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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: