下面是我动态调用DLL的代码(测试过没问题),现在我想在不改动现有代码的情况下,加一个函数,把主调程序的S变量,传到DLL窗体上的label1.caption中.主调代码:unit Unit1; //主调程序interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
tshowcalendar=function(ahandle:thandle;acaption:pchar):tdatetime;stdcall;
edllloaderror=class(exception);
Tmainform = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
mainform: Tmainform;implementation{$R *.dfm}procedure Tmainform.Button1Click(Sender: TObject);
var
libhandle:thandle;
showcalendar:tshowcalendar;
s:STRING;
begin
libhandle:=loadlibrary('dll.dll');
try
if libhandle=0 then
raise edllloaderror.Create('DLL不存在!');
@showcalendar:=getprocaddress(libhandle,'showcalendar');
if not (@showcalendar=nil) then
showcalendar(application.MainForm.Handle,pchar(caption))
else
raiselastwin32error;
finally
freelibrary(libhandle);
end;end;DLL窗体代码:unit dll_1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TdllForm = class(TForm)
Label1: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Label1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
dllform: Tdllform;function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
implementation{$R *.dfm}
function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
var
dllform: Tdllform;
begin
application.Handle := ahandle;
dllform := tdllform.Create(application);
try
dllform.Caption := acaption;
dllform.ShowModal;
Result := Now;
finally
dllform.Free;
end;
end;procedure TdllForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
tshowcalendar=function(ahandle:thandle;acaption:pchar):tdatetime;stdcall;
edllloaderror=class(exception);
Tmainform = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
mainform: Tmainform;implementation{$R *.dfm}procedure Tmainform.Button1Click(Sender: TObject);
var
libhandle:thandle;
showcalendar:tshowcalendar;
s:STRING;
begin
libhandle:=loadlibrary('dll.dll');
try
if libhandle=0 then
raise edllloaderror.Create('DLL不存在!');
@showcalendar:=getprocaddress(libhandle,'showcalendar');
if not (@showcalendar=nil) then
showcalendar(application.MainForm.Handle,pchar(caption))
else
raiselastwin32error;
finally
freelibrary(libhandle);
end;end;DLL窗体代码:unit dll_1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TdllForm = class(TForm)
Label1: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Label1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
dllform: Tdllform;function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
implementation{$R *.dfm}
function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
var
dllform: Tdllform;
begin
application.Handle := ahandle;
dllform := tdllform.Create(application);
try
dllform.Caption := acaption;
dllform.ShowModal;
Result := Now;
finally
dllform.Free;
end;
end;procedure TdllForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;
function showcalendar(ahandle: thandle; acaption: PChar;S:string): tdatetime; stdcall; 如果不想改这个函数就自己再定义一个函数
procedure SetCaption(s:string);stdcall;
procedure SetCaption(s:string);stdcall;
begin
label1.caption:=s;
end;
var
dllform: Tdllform;
begin
application.Handle := ahandle;
dllform := tdllform.Create(application);
dllform.label1.caption:=s;
try
dllform.Caption := acaption;
dllform.ShowModal;
Result := Now;
finally
dllform.Free;
end;
end;
DLL窗体代码这样写不知对不对,另外下面是主调不知如何改.请教了..........
type
tshowcalendar=function(ahandle:thandle;acaption,s:pchar):tdatetime;stdcall; procedure Tmainform.Button1Click(Sender: TObject);
var
libhandle:thandle;
showcalendar:tshowcalendar;
s:STRING;
begin
libhandle:=loadlibrary('dll.dll');
try
if libhandle=0 then
raise edllloaderror.Create('DLL不存在!');
@showcalendar:=getprocaddress(libhandle,'showcalendar');
if not (@showcalendar=nil) then
showcalendar(application.MainForm.Handle,pchar(caption))
else
raiselastwin32error;
finally
freelibrary(libhandle);
end;
2:DLL窗体代码这边改的不知对不对呀.function showcalendar(ahandle: thandle; acaption,s: PChar): tdatetime; stdcall;
var
dllform: Tdllform;
begin
application.Handle := ahandle;
dllform := tdllform.Create(application);
dllform.label1.caption:=s;
try
dllform.Caption := acaption;
dllform.ShowModal;
Result := Now;
finally
dllform.Free;
end;
end;
可以用Variant
可惜CSDN把这篇帖子删了。
否则给你链接。
你可以试一下Variant
这个参数不仅让你可以传String,还可以操作更多的类型。
而且也不用ShareMM
2:DLL窗体代码这边改的不知对不对呀.function showcalendar(ahandle: thandle; acaption,s: PChar): tdatetime; stdcall;
var
dllform: Tdllform;
begin
application.Handle := ahandle;
dllform := tdllform.Create(application);
dllform.label1.caption:=s;
try
dllform.Caption := acaption;
dllform.ShowModal;
Result := Now;
finally
dllform.Free;
end;
end;