⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 DLL的静态调用和动态调用 静态加载的DLL不能被卸载掉 动态调用: LoadLibrary LoadLibraryA LoadLibraryW 3个函数的演示
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, madExceptVcl;

type
  TForm1 = class(TForm)
    Edit1:TEdit;
    Button1:TButton;
    Button2:TButton;
    Edit2:TEdit;
    Label1:TLabel;
    Button3:TButton;
    Edit3:TEdit;
    procedure Button1Click(Sender:TObject);
    procedure Button2Click(Sender:TObject);
    procedure Button3Click(Sender:TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1:TForm1;

implementation

{$R *.DFM}

//若不存在该DLL,则运行时 报错

function TestDll(i:integer):integer; stdcall;
  external 'test.dll';

procedure TForm1.Button1Click(Sender:TObject);
begin
  //  ShowMessage('调用test.dll里面的 TestDll函数');
  Edit1.Clear;
  Edit1.Text := IntToStr(TestDll(1));
end;

procedure TForm1.Button2Click(Sender:TObject);
type
  TIntFunc = function(i:integer):integer; stdcall;
var
  Th:Thandle;
  Tf:TIntFunc;
  Tp:TFarProc;
begin
  Edit2.Clear;
  Th := LoadLibrary('Cpp.dll'); {装载DLL}
  if Th > 0 then
  try
    //  ShowMessage('调用Cpp.dll里面的 TestC 函数');
    Tp := GetProcAddress(Th, PChar('TestC'));
    if Tp <> nil then begin
      Tf := TIntFunc(Tp);
      Edit2.Text := IntToStr(Tf(2)); {调用TestC函数}
    end
    else
      ShowMessage('TestC函数没有找到');
  finally
    FreeLibrary(Th); {释放DLL}
  end
  else
    ShowMessage('Cpp.dll没有找到');
end;
//end;

procedure TForm1.Button3Click(Sender:TObject);
type
  TIntFunc = function(i:integer):integer; stdcall;
var
  Th:Thandle;
  Tf:TIntFunc;
  Tp:TFarProc;
begin
  Edit3.Clear;

  Th := LoadLibraryW('Cpp.dll'); {装载DLL}
  if Th > 0 then
  try
    //  ShowMessage('调用Cpp.dll里面的 TestC 函数');
    Tp := GetProcAddress(Th, PChar('TestC'));
    if Tp <> nil then begin
      Tf := TIntFunc(Tp);
      Edit3.Text := IntToStr(Tf(3)); {调用TestC函数}
    end
    else
      ShowMessage('TestC函数没有找到');
  finally
    FreeLibrary(Th); {释放DLL}
  end
  else
    ShowMessage('Cpp.dll没有找到');
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -