T
TENTODBV
Unregistered / Unconfirmed
GUEST, unregistred user!
//以下是调用DLL的主程序代码
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TOutFunc=function(AHandle:THandle;ACaption:string):string;stdcall;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle:THandle;
OutFunc:TOutFunc;
begin
LibHandle:=LoadLibrary('LoadDll.dll');
try
if LibHandle=0 then ;
@OutFunc:=GetProcAddress(LibHandle,'OutFunc');
if not (@OutFunc=nil) then
//Form1.Caption:=OutFunc(Application.Handle,'abc');//这行代码出错
Form1.Caption:=OutFunc(Application.Handle,Form1.Caption+'abc');//为何这行代码又可以呢?
finally
FreeLibrary(LibHandle);
end;
end;
end.
//以下是DLL工程源代码
library LoadDLL;
uses
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {Form1};
exports
OutFunc;
{$R *.res}
begin
end.
//以下是DLL中模式窗体的代码
unit DLLFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Calendar1DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
bOK:boolean;
end;
function OutFunc(AHandle:THandle;ACaption:string):string;stdcall;
implementation
{$R *.dfm}
function OutFunc(AHandle:THandle;ACaption:string):string;
var
Form1: TForm1;
begin
Application.Handle:=AHandle;
try
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Form1.ShowModal;
if Form1.bOK then
Result:=ACaption;
finally
Form1.Free;
end;
end;
procedure TForm1.Calendar1DblClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
bOK:=true;
Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
end.
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TOutFunc=function(AHandle:THandle;ACaption:string):string;stdcall;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle:THandle;
OutFunc:TOutFunc;
begin
LibHandle:=LoadLibrary('LoadDll.dll');
try
if LibHandle=0 then ;
@OutFunc:=GetProcAddress(LibHandle,'OutFunc');
if not (@OutFunc=nil) then
//Form1.Caption:=OutFunc(Application.Handle,'abc');//这行代码出错
Form1.Caption:=OutFunc(Application.Handle,Form1.Caption+'abc');//为何这行代码又可以呢?
finally
FreeLibrary(LibHandle);
end;
end;
end.
//以下是DLL工程源代码
library LoadDLL;
uses
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {Form1};
exports
OutFunc;
{$R *.res}
begin
end.
//以下是DLL中模式窗体的代码
unit DLLFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Calendar1DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
bOK:boolean;
end;
function OutFunc(AHandle:THandle;ACaption:string):string;stdcall;
implementation
{$R *.dfm}
function OutFunc(AHandle:THandle;ACaption:string):string;
var
Form1: TForm1;
begin
Application.Handle:=AHandle;
try
Form1:=TForm1.Create(Application);
Form1.Caption:=ACaption;
Form1.ShowModal;
if Form1.bOK then
Result:=ACaption;
finally
Form1.Free;
end;
end;
procedure TForm1.Calendar1DblClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
bOK:=true;
Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;
end.