转自http://topic.csdn.net/t/20030702/17/1983040.html
一、在包中新建一个窗体单元,提供输出的函数,过程。
unit frmDemo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TDemoForm = class(TForm)
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{声明函数和过程}
procedure SayHello;stdcall;
function Add(a1,a2:Integer):Integer;stdcall;
{导出函数和过程}
exports
SayHello,
Add;
var
DemoForm: TDemoForm;
implementation
{$R *.dfm}
procedure SayHello;
begin
ShowMessage( '你好! ');
end;
function Add(a1,a2:Integer):Integer;
begin
Result:=a1+a2;
end;
procedure TDemoForm.Button1Click(Sender: TObject);
begin
Self.close;
end;
initialization
RegisterClass(TDemoForm);
finalization
UnRegisterClass(TDemoForm);
end.
二、在主程序中调用包中的对象以及函数过程
具体见代码如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB,ADODB;
type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
PackageModule: HModule;
public
{ Public declarations }
end;
{声明函数和过程}
TAddFunc = function(a1,a2:Integer):Integer;stdcall;
TSayHelloProc = procedure;stdcall;
var
MainForm: TMainForm;
implementation
uses
DemoClassInt;
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
try
PackageModule:=LoadPackage( 'DemoPackage.bpl ')
except
On E:Exception do
begin
MessageBeep(Word(-1));
ShowMessage(E.Message);
Application.Terminate;
end;
end;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if PackageModule <> 0 then
UnloadPackage(PackageModule);
end;
{Package中函数的调用}
procedure TMainForm.Button1Click(Sender: TObject);
var
AddFunc:TAddfunc;
value:Integer;
a1,a2:Integer;
begin
@AddFunc:=GetProcAddress(PackageModule,PChar( 'Add '));
if @AddFunc <> nil then
begin
Value:=AddFunc(2,2);
showMessage( '结果是: '+IntToStr(Value));
end;
end;
{Package中过程的调用}
procedure TMainForm.Button2Click(Sender: TObject);
var
SayHelloProc:TSayHelloProc;
begin
@SayHelloProc:=GetProcAddress(PackageModule,PChar( 'SayHello '));
if @SayHelloProc <> nil then
SayHelloProc;
end;
end.