DLL封装Interface(接口)(D2007+win764位)-LMLPHP

相关资料:

http://blog.csdn.net/liangpei2008/article/details/5394911

结果注意:

1.函数的传参方向必须一至。

DLL实例代码:

ZJQInterface.pas

 unit ZJQInterface;

 interface

 type
IInterface1 = interface
function Func1: Integer;
function Func2: Integer;
end; IInterface2 = interface
procedure Proc1;
procedure Proc2;
end; implementation end.

ZJQInterfaceDll.dpr

 //************************************************************//
// DLL封装接口
//************************************************************//
library ZJQInterfaceDll; { Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Dialogs,
SysUtils,
Classes,
ZJQInterface in 'ZJQInterface.pas'; type
TClass1 = class(TInterfacedObject, IInterface1, IInterface2)//可以继承一下接口
public
procedure Proc1;
procedure Proc2;
function Func1: Integer;
function Func2: Integer;
end; {$R *.res}
{ TClass1 } function TClass1.Func1: Integer;
begin
ShowMessage('IInterface1.Func1');
Result := ;
end; function TClass1.Func2: Integer;
begin
ShowMessage('IInterface1.Func2');
Result := ;
end; procedure TClass1.Proc1;
begin
ShowMessage('IInterface2.Proc1');
end; procedure TClass1.Proc2;
begin
ShowMessage('IInterface2.Proc2');
end; function CreateInterface: IInterface1; stdcall;//stdcall关键字必须有,因为UNIT1中也用到了这个,必须一至。
begin
Result := TClass1.Create;
end; exports
CreateInterface; begin
end.

EXE实例代码:

Unit1.pas

 //************************************************************//
//EXE调用DLL的接口实例
//程序运行不了,请生成新的DLL放在EXE的同目录里。
//************************************************************//
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
ZJQInterface, jpeg, ExtCtrls;//引入接口单元 type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//静态调用
function CreateInterface: IInterface1; stdcall; external 'ZJQInterfaceDll.dll';//stdcall需要与DLL中的一至 var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
i1: IInterface1;
begin
i1 := CreateInterface;
i1.Func1;
//i1 := nil;//全局的接口要写这行,局部的可以不写。
end; end.

ZJQInterfaceExe.dpr 

 program ZJQInterfaceExe;

 uses
Forms,
Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin
Application.Initialize;
ReportMemoryLeaksOnShutdown := True;//打开内存溢出提示
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
05-11 21:59