初学 用的天极的那个模板 程序里结合了一个静态调用testdll 不过这个用在button1上 动态用在button2上
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,testdll;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
type
ShowCalendar = function(AHandle: THandle; ACaption: String): TDateTime;stdcall;
var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x,y:integer;
beginx:=strtoint(edit1.text);
y:=strtoint(edit2.text);
edit3.Text:= inttostr(max(x,y));
end;procedure TForm1.Button2Click(Sender:TObject);var
OneHandle:THandle;
begin
OneHandle := LoadLibrary('Clendar.dll'); // 程序编译从这里开始报错
try
if OneHandle <> 0 then //多数报Illegal character in input file: ' ' ($A1A1)
@ShowCalendar := GetProcAddress(OneHandle, 'ShowCalendar'); //这行报'(' expected but ':=' found
if not (@ShowCalendar = nil) then //这行说没'('
Label1.Caption := DateToStr(ShowCalendar(Application.Handle, Caption)) //这行说没')'
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle);
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,testdll;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
type
ShowCalendar = function(AHandle: THandle; ACaption: String): TDateTime;stdcall;
var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x,y:integer;
beginx:=strtoint(edit1.text);
y:=strtoint(edit2.text);
edit3.Text:= inttostr(max(x,y));
end;procedure TForm1.Button2Click(Sender:TObject);var
OneHandle:THandle;
begin
OneHandle := LoadLibrary('Clendar.dll'); // 程序编译从这里开始报错
try
if OneHandle <> 0 then //多数报Illegal character in input file: ' ' ($A1A1)
@ShowCalendar := GetProcAddress(OneHandle, 'ShowCalendar'); //这行报'(' expected but ':=' found
if not (@ShowCalendar = nil) then //这行说没'('
Label1.Caption := DateToStr(ShowCalendar(Application.Handle, Caption)) //这行说没')'
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle);
end;
end;
end.
你应该先 定义一个 ShowCalendar 类型 的变量
var
showCalFun: ShowCalendar;
begin
@showCalFun := GetProcAddress(OneHandle, 'ShowCalendar');
end;
试试
//注意路径,如果Clendar.dll没有和exe文件放在同一目录下,就要指定目录。或者放到system32目录中去!
动态载入DLL文件 可以参考一下。。uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
Label1: TLabel;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tgetn=function:pchar;stdcall;
var
Form1: TForm1;
implementation{$R *.dfm}procedure TForm1.SpeedButton1Click(Sender: TObject);
var
onehandle:Thandle;
fun:Tgetn;
begin
onehandle:=loadlibrary('getname.dll');
try
if onehandle<>0 then
@fun:=getprocaddress(onehandle,'getnames');
if not(@fun=nil)then
label1.Caption:=fun ;
finally
freelibrary(onehandle);
end;
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,testdll; type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
type
TShowCalendar = function(AHandle: THandle; ACaption: String): TDateTime;stdcall; //这里定义回调函数?那下面要具体实例化
var
Form1: TForm1;
implementation {$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x,y:integer;
begin x:=strtoint(edit1.text);
y:=strtoint(edit2.text);
edit3.Text:= inttostr(max(x,y));
end; procedure TForm1.Button2Click(Sender:TObject);
var
OneHandle:THandle;
ShowCalendar: TShowCalendar; //定义一个实例
begin
OneHandle := LoadLibrary('Clendar.dll'); //这里数据类型错了,改为 OneHandle := LoadLibrary(PChar('Clendar.dll'));
try
if OneHandle <> 0 then //这里可设断点观察,OneHandle := 0的话,就代表'Clendar.dll'路径就错了
@ShowCalendar := GetProcAddress(OneHandle, 'ShowCalendar'); //
if not (@ShowCalendar = nil) then
Label1.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error;
finally
FreeLibrary(OneHandle);
end;
end; end.
具体没有编译,楼主再试试
library Clendar;
uses
SysUtils,
Classes;
{$R *.res}
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
DLLForm:TDLLForm;
begin
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
try
DLLForm.Caption := ACaption;
DLLForm.ShowModal;
Result := DLLForm.calDLLCalendar.CalendarDate; //返回设定日期
finally
DLLForm.Free;
end;
end;begin
end.
麻烦老大们在看看我的dll 里面有什么错误