//DLL--------------------------
library dllcomserver;
uses
SysUtils,
Classes,
yf in 'yf.pas' {Form1};
{$R *.res}
type
TComp = class
public
function max(x, y: Integer): Integer; virtual; abstract;
procedure test; virtual; abstract;
end;
TCompClass = class of TComp;
TComplmpl = class(TComp)
public
function max(x, y: Integer): Integer; override;
procedure test; override;
end;
{ TComplmpl }
function TComplmpl.max(x, y: Integer): Integer;
begin
if x > y then
Result := x
else
Result := y;
end;
procedure TComplmpl.test;
begin
Form1 := TForm1.Create(nil);
Form1.ShowModal;
Form1.Free;
end;
function CreateComp: TCompClass; stdcall;
begin
Result := TComplmpl;
end;
exports
CreateComp;
begin
end.
//build with runtime packages 打上勾
//主程序--------------------------------
unit Unit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,
Controls,Forms,Dialogs,StdCtrls;
type
TComp = class
public
function max(x, y: Integer): Integer; virtual; abstract;
procedure test; virtual; abstract;
end;
TCompClass = class of TComp;
//TIntFunction = function(): TCompClass;stdcall; TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn7: TButton;
btn8: TButton;
btn9: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure btn7Click(Sender: TObject);
procedure btn8Click(Sender: TObject);
procedure btn9Click(Sender: TObject);
private
{ Private declarations }
MyComp: TComp;
HInst: THandle;
public
{ Public declarations }
end;
function CreateComp: TCompClass; stdcall; external 'dllcomserver.dll';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// mycomp := CreateComp.Create;
showmessage(inttostr(Application.Handle));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// MyComp.Free;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
showmessage(inttostr(MyComp.max(StrToInt(edt1.Text), StrToInt(edt2.Text))));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
FPointer: TFarProc;
// MyFunct: TIntFunction;
begin
{ HInst := SafeLoadLibrary('dllcomserver.dll');
if HInst > 0 then
try
FPointer := GetProcAddress(HInst,'CreateComp');
if FPointer <> nil then
begin
MyFunct := TIntFunction(FPointer);
MyComp :=MyFunct.Create;
end
else
ShowMessage('Function not found');
finally
end
else
ShowMessage('DLL not found');}
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
MyComp.Free;
FreeLibrary(HInst);
end;
procedure TForm1.btn7Click(Sender: TObject);
begin
MyComp := CreateComp.Create; // 去掉这句 Application.Handle 就不为 0
end;
procedure TForm1.btn8Click(Sender: TObject);
begin
MyComp.Free;
end;
procedure TForm1.btn9Click(Sender: TObject);
begin
MyComp.test;
end;
end.//build with runtime packages 打上勾现象:
主程序showmessage : Application.Handle = 0将主程序的build with runtime packages 去掉,或者
去掉MyComp := CreateComp.Create
则 Application.Handle 就不为 0 了。
或者动态调用Dll也没问题。求解
library dllcomserver;
uses
SysUtils,
Classes,
yf in 'yf.pas' {Form1};
{$R *.res}
type
TComp = class
public
function max(x, y: Integer): Integer; virtual; abstract;
procedure test; virtual; abstract;
end;
TCompClass = class of TComp;
TComplmpl = class(TComp)
public
function max(x, y: Integer): Integer; override;
procedure test; override;
end;
{ TComplmpl }
function TComplmpl.max(x, y: Integer): Integer;
begin
if x > y then
Result := x
else
Result := y;
end;
procedure TComplmpl.test;
begin
Form1 := TForm1.Create(nil);
Form1.ShowModal;
Form1.Free;
end;
function CreateComp: TCompClass; stdcall;
begin
Result := TComplmpl;
end;
exports
CreateComp;
begin
end.
//build with runtime packages 打上勾
//主程序--------------------------------
unit Unit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,
Controls,Forms,Dialogs,StdCtrls;
type
TComp = class
public
function max(x, y: Integer): Integer; virtual; abstract;
procedure test; virtual; abstract;
end;
TCompClass = class of TComp;
//TIntFunction = function(): TCompClass;stdcall; TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn7: TButton;
btn8: TButton;
btn9: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure btn7Click(Sender: TObject);
procedure btn8Click(Sender: TObject);
procedure btn9Click(Sender: TObject);
private
{ Private declarations }
MyComp: TComp;
HInst: THandle;
public
{ Public declarations }
end;
function CreateComp: TCompClass; stdcall; external 'dllcomserver.dll';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// mycomp := CreateComp.Create;
showmessage(inttostr(Application.Handle));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// MyComp.Free;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
showmessage(inttostr(MyComp.max(StrToInt(edt1.Text), StrToInt(edt2.Text))));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
FPointer: TFarProc;
// MyFunct: TIntFunction;
begin
{ HInst := SafeLoadLibrary('dllcomserver.dll');
if HInst > 0 then
try
FPointer := GetProcAddress(HInst,'CreateComp');
if FPointer <> nil then
begin
MyFunct := TIntFunction(FPointer);
MyComp :=MyFunct.Create;
end
else
ShowMessage('Function not found');
finally
end
else
ShowMessage('DLL not found');}
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
MyComp.Free;
FreeLibrary(HInst);
end;
procedure TForm1.btn7Click(Sender: TObject);
begin
MyComp := CreateComp.Create; // 去掉这句 Application.Handle 就不为 0
end;
procedure TForm1.btn8Click(Sender: TObject);
begin
MyComp.Free;
end;
procedure TForm1.btn9Click(Sender: TObject);
begin
MyComp.test;
end;
end.//build with runtime packages 打上勾现象:
主程序showmessage : Application.Handle = 0将主程序的build with runtime packages 去掉,或者
去掉MyComp := CreateComp.Create
则 Application.Handle 就不为 0 了。
或者动态调用Dll也没问题。求解
begin
// mycomp := CreateComp.Create; 你说的是这个吗?我屏蔽掉了啊
showmessage(inttostr(Application.Handle));
end;
所以如果用DLL打包窗体,要把EXE的APP.HANDLE传进去替换!
procedure TForm1.FormCreate(Sender: TObject);
begin
// mycomp := CreateComp.Create;
showmessage(inttostr(Application.Handle));//你的意思是我这的Application.Handle是DLL的吗?
end;