implementation{$R *.DFM} procedure ShowInfo(str:string);stdcall;external 'DLLEXA.DLL' name 'ShowInfo'; procedure TForm1.Button1Click(Sender: TObject); begin showinfo('hello!') end;end.
将Form以ActiveX组件的形式存入DLL则应如下方式写DLL: library DICDLL;uses SysUtils, Classes, Forms, ActiveX, EncodeFrm in 'EncodeFrm.pas' {FrmEncode},//这是本人的秘密Form,不能给你的 frmDecode in 'frmDecode.pas' {FormDecode};//这也是本人的秘密Form,不能给你的{$R *.RES} procedure OpenEncodeForm; begin try FrmEncode:=TFrmEncode.Create(nil); FrmEncode.ShowModal; finally FrmEncode.Free; end; end;procedure OpenDecodeForm; begin try FormDecode:=TFormDecode.Create(nil); FormDecode.ShowModal; finally FormDecode.Free; end; end; exports OpenEncodeForm, OpenDecodeForm; begin CoInitialize(nil); end.调用单元如下: unit DLLDICFrmUnt;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM} procedure OpenEncodeForm; external 'DICDLL.DLL' procedure OpenDecodeForm; external 'DICDLL.DLL'procedure TForm1.Button1Click(Sender: TObject); begin OpenEncodeForm; end;procedure TForm1.Button2Click(Sender: TObject); begin OpenDecodeForm end;end.
给你一个吧, 可能不是最简单的,但还比较好用。功能是,把一个Form放到一个DLL中,由其它程序来打开DLL部分: library DllTest;uses SysUtils, Classes, DllTestF in 'DllTestF.pas' {frmTest1}, Load in 'Load.pas';{$R *.RES}exports Run;begin g_frmClass := TfrmTest1; end.//***********'DllTestF.pas' ************ unit DllTestF;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TfrmTest1 = class(TForm) Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public procedure CreateParams(var Params: TCreateParams); override; { Public declarations } end;implementation{$R *.DFM}procedure TfrmTest1.CreateParams(var Params: TCreateParams); begin inherited; Params.WndParent := GetDesktopWindow; end;procedure TfrmTest1.Button1Click(Sender: TObject); begin Close(); end;end.//*************'Load.pas' unit Load;interfaceuses SysUtils, Forms;type TCForm = class of TForm;function Run: TForm; stdcall;var g_frmClass : TCForm;
implementation function Run: TForm; begin Result := g_frmClass.Create(Application.MainForm); end;end.//*********调用部分: program Loader;uses Forms, LoaderF in 'LoaderF.pas' {frmLoader};{$R *.RES}begin Application.Initialize; Application.CreateForm(TfrmLoader, frmLoader); Application.Run; end.//**********LoadF.pas unit LoaderF;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, GSpdBtn, F2Button;type TRun = function: TForm; TDllHandle = Integer; TfrmLoader = class(TForm) Edit1: TEdit; btnLoad: TButton; btnOpen: TF2Button; dlgOpenDll: TOpenDialog; procedure btnLoadClick(Sender: TObject); procedure btnOpenClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var frmLoader: TfrmLoader;implementation{$R *.DFM}procedure TfrmLoader.btnLoadClick(Sender: TObject); Var DllHandle : TDllHandle; Run : TRun; o: TForm; begin if FileExists(Edit1.Text) then begin DllHandle := LoadLibrary(PChar(Edit1.Text)); if DllHandle <> 0 then begin @Run := GetProcAddress(DllHandle, 'Run'); if @Run <> nil then { if get the address of function } begin o := Run; o.ShowModal; o.Free; end;
FreeLibrary(DllHandle); end; end else ShowMessage(Edit1.Text + ' not Exist!'); end;procedure TfrmLoader.btnOpenClick(Sender: TObject); begin if dlgOpenDll.Execute then Edit1.Text := dlgOpenDll.FileName; end;procedure TfrmLoader.FormCreate(Sender: TObject); begin dlgOpenDll.InitialDir := ExtractFileDir(Application.ExeName); end;end.
dll:(文件名:test.dll) library test; uses sharemem, SysUtils, Classes; function abc(a:integer;var b:integer):integer;stdcall; begin b:=a+1; result:=1; end;exports abc;begin end. 调用test.dll中的abc函数: var function abc(a:integer;var b:integer):integer;stdcall;external'test.dll'; //test.dll和程序放在同一目录下不用写路径 procedure TForm1.Button1Click(Sender: TObject); var a1,b1:integer; begin a1:=9; abc(a1,b1); //调用test.dll的abc函数 end;
Dll文件:library Project1;uses SysUtils, Classes;function MyDllFun: string; begin Result := 'Test'; end; exports MyDllFun;end. 调用DLL文件: 用LOADLIBRARY方法动态调用
把你的E-MAIL 给我,我发一个给你。
end;
FreeLibrary(DllHandle); end; end else ShowMessage(Edit1.Text + ' not Exist!'); end;procedure TfrmLoader.btnOpenClick(Sender: TObject); begin if dlgOpenDll.Execute then Edit1.Text := dlgOpenDll.FileName; end;procedure TfrmLoader.FormCreate(Sender: TObject); begin dlgOpenDll.InitialDir := ExtractFileDir(Application.ExeName); end;end.
FILE->NEW->DLL:
library DLLEXA;uses
SysUtils,
Classes,
Dialogs,
windows;{$R *.RES}
procedure ShowInfo(str:string);stdcall;
begin
showmessage(str+chr(13)+chr(10)+'DLL Test');
end;
exports ShowInfo;beginend.
调用DLL:
……
var
Form1: TForm1;
implementation{$R *.DFM}
procedure ShowInfo(str:string);stdcall;external 'DLLEXA.DLL' name 'ShowInfo';
procedure TForm1.Button1Click(Sender: TObject);
begin
showinfo('hello!')
end;end.
library DICDLL;uses
SysUtils,
Classes,
Forms,
ActiveX,
EncodeFrm in 'EncodeFrm.pas' {FrmEncode},//这是本人的秘密Form,不能给你的
frmDecode in 'frmDecode.pas' {FormDecode};//这也是本人的秘密Form,不能给你的{$R *.RES}
procedure OpenEncodeForm;
begin
try
FrmEncode:=TFrmEncode.Create(nil);
FrmEncode.ShowModal;
finally
FrmEncode.Free;
end;
end;procedure OpenDecodeForm;
begin
try
FormDecode:=TFormDecode.Create(nil);
FormDecode.ShowModal;
finally
FormDecode.Free;
end;
end;
exports
OpenEncodeForm,
OpenDecodeForm;
begin
CoInitialize(nil);
end.调用单元如下:
unit DLLDICFrmUnt;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
procedure OpenEncodeForm; external 'DICDLL.DLL'
procedure OpenDecodeForm; external 'DICDLL.DLL'procedure TForm1.Button1Click(Sender: TObject);
begin
OpenEncodeForm;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
OpenDecodeForm
end;end.
library DllTest;uses
SysUtils,
Classes,
DllTestF in 'DllTestF.pas' {frmTest1},
Load in 'Load.pas';{$R *.RES}exports
Run;begin
g_frmClass := TfrmTest1;
end.//***********'DllTestF.pas' ************
unit DllTestF;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TfrmTest1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure CreateParams(var Params: TCreateParams); override;
{ Public declarations }
end;implementation{$R *.DFM}procedure TfrmTest1.CreateParams(var Params: TCreateParams);
begin
inherited; Params.WndParent := GetDesktopWindow;
end;procedure TfrmTest1.Button1Click(Sender: TObject);
begin
Close();
end;end.//*************'Load.pas'
unit Load;interfaceuses
SysUtils, Forms;type
TCForm = class of TForm;function Run: TForm; stdcall;var
g_frmClass : TCForm;
implementation
function Run: TForm;
begin
Result := g_frmClass.Create(Application.MainForm);
end;end.//*********调用部分:
program Loader;uses
Forms,
LoaderF in 'LoaderF.pas' {frmLoader};{$R *.RES}begin
Application.Initialize;
Application.CreateForm(TfrmLoader, frmLoader);
Application.Run;
end.//**********LoadF.pas
unit LoaderF;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, GSpdBtn, F2Button;type
TRun = function: TForm;
TDllHandle = Integer; TfrmLoader = class(TForm)
Edit1: TEdit;
btnLoad: TButton;
btnOpen: TF2Button;
dlgOpenDll: TOpenDialog;
procedure btnLoadClick(Sender: TObject);
procedure btnOpenClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
frmLoader: TfrmLoader;implementation{$R *.DFM}procedure TfrmLoader.btnLoadClick(Sender: TObject);
Var
DllHandle : TDllHandle;
Run : TRun;
o: TForm;
begin
if FileExists(Edit1.Text) then
begin
DllHandle := LoadLibrary(PChar(Edit1.Text));
if DllHandle <> 0 then
begin
@Run := GetProcAddress(DllHandle, 'Run');
if @Run <> nil then { if get the address of function }
begin
o := Run;
o.ShowModal;
o.Free;
end;
FreeLibrary(DllHandle);
end;
end
else
ShowMessage(Edit1.Text + ' not Exist!');
end;procedure TfrmLoader.btnOpenClick(Sender: TObject);
begin
if dlgOpenDll.Execute then
Edit1.Text := dlgOpenDll.FileName;
end;procedure TfrmLoader.FormCreate(Sender: TObject);
begin
dlgOpenDll.InitialDir := ExtractFileDir(Application.ExeName);
end;end.
library test;
uses
sharemem,
SysUtils,
Classes;
function abc(a:integer;var b:integer):integer;stdcall;
begin
b:=a+1; result:=1;
end;exports
abc;begin
end. 调用test.dll中的abc函数:
var
function abc(a:integer;var b:integer):integer;stdcall;external'test.dll';
//test.dll和程序放在同一目录下不用写路径
procedure TForm1.Button1Click(Sender: TObject);
var
a1,b1:integer;
begin
a1:=9;
abc(a1,b1);
//调用test.dll的abc函数
end;
SysUtils,
Classes;function MyDllFun: string;
begin
Result := 'Test';
end;
exports
MyDllFun;end.
调用DLL文件:
用LOADLIBRARY方法动态调用
FreeLibrary(DllHandle);
end;
end
else
ShowMessage(Edit1.Text + ' not Exist!');
end;procedure TfrmLoader.btnOpenClick(Sender: TObject);
begin
if dlgOpenDll.Execute then
Edit1.Text := dlgOpenDll.FileName;
end;procedure TfrmLoader.FormCreate(Sender: TObject);
begin
dlgOpenDll.InitialDir := ExtractFileDir(Application.ExeName);
end;end.