如何向dll连接库传递参数。

解决方案 »

  1.   

    首先你要了解到动态连接库(DLL)的用途,其实它内部只是一些封装好的函数,只是不想让别你知道罢了, 而且在调用的时候各种程序都可以同时调用它,不需要把它再一次加载, 节省了时间、内存等!
    如上所说,具体如下:
    library Project2;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,
      extctrls,graphics,
      Classes;{$R *.RES}
    procedure ShowStr(c:Pchar); stdcall;
    begin
      Messagebox(0, c, '', MB_OK);
    end;Exports
        ShowStr;
    begin
    end.这个DLL表示如调用DLL中的函数ShowStr将显示你向它传递的参数C,明白吗?下面是一个PROJECT:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Image1: TImage;
        Button1: TButton;
        OpenDialog1: TOpenDialog;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    procedure ShowStr(c:PCHAR);stdcall; external 'project2.dll';
    {$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowStr(pchar('success'));
    end;end.