mainform.pas

来自「DLL的动态调用」· PAS 代码 · 共 60 行

PAS
60
字号
unit MainForm;

interface

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

type
  TDLLDemo = function(x, y: Integer): Integer; stdcall;
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  DllHandle: THandle;
  DllPointer: Pointer;
  MyDLLFunc: TDLLDemo;
implementation


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DllHandle := LoadLibrary('DLLDemo.dll');
  if DllHandle > 0 then
    try
      DllPointer := GetProcAddress(DllHandle, PChar('AddIt'));
      if DllPointer <> nil then begin
        MyDLLFunc := TDLLDemo(DllPointer);
        Edit1.Text := IntToStr(MyDLLFunc(5, 3));
      end
      else
        ShowMessage('没有找到AddIt');
      DllPointer := GetProcAddress(DllHandle, PChar('SubIt'));
      if DllPointer <> nil then begin
        MyDLLFunc := TDLLDemo(DllPointer);
        Edit2.Text := IntToStr(MyDLLFunc(5, 3));
      end
      else
        ShowMessage('没有找到SubIts');
    finally
      FreeLibrary(DllHandle);
    end
    else
      ShowMessage('没有找到DLLDemo.dll');
end;

end.

⌨️ 快捷键说明

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