DLL封装Interface(接口)(D2007+win764位)
相关资料:
http://blog.csdn.net/liangpei2008/article/details/5394911
结果注意:
1.函数的传参方向必须一至。
DLL实例代码:
ZJQInterface.pas
1 unit ZJQInterface;
2
3 interface
4
5 type
6 IInterface1 = interface
7 function Func1: Integer;
8 function Func2: Integer;
9 end;
10
11 IInterface2 = interface
12 procedure Proc1;
13 procedure Proc2;
14 end;
15
16 implementation
17
18 end.
ZJQInterfaceDll.dpr
1 //************************************************************//
2 // DLL封装接口
3 //************************************************************//
4 library ZJQInterfaceDll;
5
6 { Important note about DLL memory management: ShareMem must be the
7 first unit in your library's USES clause AND your project's (select
8 Project-View Source) USES clause if your DLL exports any procedures or
9 functions that pass strings as parameters or function results. This
10 applies to all strings passed to and from your DLL--even those that
11 are nested in records and classes. ShareMem is the interface unit to
12 the BORLNDMM.DLL shared memory manager, which must be deployed along
13 with your DLL. To avoid using BORLNDMM.DLL, pass string information
14 using PChar or ShortString parameters. }
15 uses
16 Dialogs,
17 SysUtils,
18 Classes,
19 ZJQInterface in 'ZJQInterface.pas';
20
21 type
22 TClass1 = class(TInterfacedObject, IInterface1, IInterface2)//可以继承一下接口
23 public
24 procedure Proc1;
25 procedure Proc2;
26 function Func1: Integer;
27 function Func2: Integer;
28 end;
29
30 {$R *.res}
31 { TClass1 }
32
33 function TClass1.Func1: Integer;
34 begin
35 ShowMessage('IInterface1.Func1');
36 Result := 0;
37 end;
38
39 function TClass1.Func2: Integer;
40 begin
41 ShowMessage('IInterface1.Func2');
42 Result := 0;
43 end;
44
45 procedure TClass1.Proc1;
46 begin
47 ShowMessage('IInterface2.Proc1');
48 end;
49
50 procedure TClass1.Proc2;
51 begin
52 ShowMessage('IInterface2.Proc2');
53 end;
54
55 function CreateInterface: IInterface1; stdcall;//stdcall关键字必须有,因为UNIT1中也用到了这个,必须一至。
56 begin
57 Result := TClass1.Create;
58 end;
59
60 exports
61 CreateInterface;
62
63 begin
64 end.
EXE实例代码:
Unit1.pas
1 //************************************************************//
2 //EXE调用DLL的接口实例
3 //程序运行不了,请生成新的DLL放在EXE的同目录里。
4 //************************************************************//
5 unit Unit1;
6
7 interface
8
9 uses
10 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
11 Dialogs, StdCtrls,
12 ZJQInterface, jpeg, ExtCtrls;//引入接口单元
13
14 type
15 TForm1 = class(TForm)
16 Button1: TButton;
17 Image1: TImage;
18 procedure Button1Click(Sender: TObject);
19 private
20 { Private declarations }
21 public
22 { Public declarations }
23 end;
24 //静态调用
25 function CreateInterface: IInterface1; stdcall; external 'ZJQInterfaceDll.dll';//stdcall需要与DLL中的一至
26
27 var
28 Form1: TForm1;
29
30 implementation
31
32 {$R *.dfm}
33
34 procedure TForm1.Button1Click(Sender: TObject);
35 var
36 i1: IInterface1;
37 begin
38 i1 := CreateInterface;
39 i1.Func1;
40 //i1 := nil;//全局的接口要写这行,局部的可以不写。
41 end;
42
43 end.
ZJQInterfaceExe.dpr
1 program ZJQInterfaceExe;
2
3 uses
4 Forms,
5 Unit1 in 'Unit1.pas' {Form1};
6
7 {$R *.res}
8
9 begin
10 Application.Initialize;
11 ReportMemoryLeaksOnShutdown := True;//打开内存溢出提示
12 Application.MainFormOnTaskbar := True;
13 Application.CreateForm(TForm1, Form1);
14 Application.Run;
15 end.
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我