怎样调用BPL包中的过程???

转自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.

转载于:https://www.cnblogs.com/yuezh/archive/2012/09/17/2688272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值