Delphi中的dll操作

利用delphi dll wizard进行dll的编写。

创建:保存时改dll名称

library test2;

uses
  SysUtils,
  Classes,
  forms,
  dialogs;

{$R *.res}
function test():string;
begin
  showmessage('a');
end;
exports
  test;//必须的
begin
end.

调用:在调用单元的implement下面 function test():string;external 'xxxxxxx\test2.dll';

button中调用test方法即可。

动态调用dll:

type
  //晚绑定,也就是动态调用外部函数主要用以下三个命令:
  //LoadLibrary:获取 DLL
  //GetProcAddress:获取函数
  //FreeLibrary:释放

  //定义一个过程类型,参数要和需要的函数一致
  TMB = function(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    MB: TMB;  {声明函数 MB}
    inst: LongWord;  {声明一个变量来记录要使用的 DLL 句柄}
  public
    { Public declarations }
  end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  inst := LoadLibrary('user32.dll');
  if inst <> 0 then
    MB := GetProcAddress(inst, 'MessageBoxW');

end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeLibrary(inst);  {记得释放}
end;