下面是我动态调用DLL的代码(测试过没问题),现在我想在不改动现有代码的情况下,加一个函数,把主调程序的S变量,传到DLL窗体上的label1.caption中.
问题:根据下面代码,把新的传递函数加进去.(如能帮忙写出代码,不胜感激.先谢谢了.)主调代码: unit Unit1; //主调程序 interface uses
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; interface uses
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;
问题:根据下面代码,把新的传递函数加进去.(如能帮忙写出代码,不胜感激.先谢谢了.)主调代码: unit Unit1; //主调程序 interface uses
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; interface uses
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;
全局变量dllform定义来有什么用?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
tshowcalendar=function(ahandle:thandle;acaption:pchar):tdatetime;stdcall;
TSetLabelCaption=procedure(s:pchar);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;
SetLabelCaption:TSetLabelCaption;
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; @SetLabelCaption:=getprocaddress(libhandle,'SetLabelCaption');
if not (@showcalendar=nil) then
showcalendar(pchar(caption))
else
raiselastwin32error; finally
freelibrary(libhandle);
end; end;
DLL窗体代码: unit dll_1; interface uses
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;
procedure SetLabelCaption(S:PChar);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 SetLabelCaption(S:PChar);stdcall;
begin
dllform.label1.caption := s;
end;
procedure TdllForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;
[Warning] Unit1.pas(46): Symbol 'RaiseLastWin32Error' is deprecated
[Error] Unit1.pas(50): Incompatible types: 'Cardinal' and 'PAnsiChar'
[Warning] Unit1.pas(52): Symbol 'RaiseLastWin32Error' is deprecated
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'unit Unit1; //主调程序 interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
tshowcalendar=function(ahandle:thandle;acaption:pchar):tdatetime;stdcall;
TSetLabelCaption=procedure(s:pchar);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;
SetLabelCaption:TSetLabelCaption;
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; @SetLabelCaption:=getprocaddress(libhandle,'SetLabelCaption');
if not (@showcalendar=nil) then
showcalendar(pchar(caption)) 这里报错,是不是可以直接把caption换成我的A变量呀,要不要用那个函数转换?
else
raiselastwin32error; finally
freelibrary(libhandle);
end; end;