Form1窗口上放按钮Button1,然后写:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//定义一个函数指针,通过这个指针调用咱的函数
pOpenComm = function (byPort: BYTE; dwCommBaudRate: DWORD): WORD; stdcall;
pReadNum = function (iCard: PInteger; byCardNum: PBYTE ): WORD; stdcall;
var
Form1: TForm1;
OpenComm: pOpenComm;//定义一个函数指针的变量
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle: THandle;
begin
//装入DLL,返回大于零则成功
LibHandle:= LoadLibrary('YourDllName.dll');
if LibHandle > 0 then
begin
//获得进程地址
@OpenComm:= GetProcAddress(LibHandle, 'OpenComm');
//调用DLL
if OpenComm(80, 9600) = 0 then
begin
ShowMessage('函数调用成功!');
end else
ShowMessage('函数调用失败!');
//释放DLL
FreeLibrary(LibHandle);
end else
begin
ShowMessage('Open DLL Failed!
');
end;
end;
end.