請教調用dll中的函數問題 |
尚未結案
|
zhong
一般會員 ![]() ![]() 發表:46 回覆:17 積分:12 註冊:2004-08-02 發送簡訊給我 |
EastRiver.dll中有個函數定議如下:
function ReadRecord(hPort: THandle; data: PChar; timeString: PChar; var mark: Char; FirstRecord: Boolean): Boolean; stdcall; external EASTRIVER_API
//參數說明:
// hPort : 端口句柄, 需要聯機
// data : 讀回的卡號字符串, 緩沖區需大於20位字節長度
// timeString : 讀回的14位刷卡時間字符串, 格式為yyyymmddhhmmss, 緩沖區需大於20位字節長度
// mark : 讀卡機標識字符.
// FirstRecord : 是否只讀取最頂端記錄,否則刪除最頂端記錄,讀取下一條記錄
//返回值:
// True : 成功
// False : 失敗 在delphi中我是這樣調用的:
unit FMAIN; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
hLibModule: HMODULE;
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject);
begin
hLibModule := LoadLibrary ('EastRiver.dll');
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
if hLibModule <> 0 then
FreeLibrary (hLibModule);
end; procedure TForm1.Button3Click(Sender: TObject);
var
a:integer;
data:pchar;
timeString:pchar;
varmark:char;
FirstRecord:Boolean; rre:function (hPort: THandle; data: PChar; timeString: PChar; var mark: Char; FirstRecord: Boolean): Boolean; stdcall;
begin
a:=strtoint(edit1.Text);
//varmark:=edit2.text;
rre:= GetProcAddress (hLibModule, 'ReadRecord');
if not assigned (rre) then
ShowMessage ('找不到 ReadRecord 函數¡')
else begin
if rre(a,data,timestring,varmark,firstrecord) then
begin
showmessage('讀取成功!');
end
else
begin
showmessage('讀取失敗!');
end;
end;
end; end.
請問以上寫法是否正確.
|
xxxxzxx
一般會員 ![]() ![]() 發表:21 回覆:49 積分:24 註冊:2004-11-15 發送簡訊給我 |
您好zhong:改成:
引言: unit FMAIN; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Edit2: TEdit; Label1: TLabel; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private hLibModule: HMODULE; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function ReadRecord(hPort: THandle; data: PChar; timeString: PChar; var mark: Char; FirstRecord: Boolean): Boolean; stdcall;//加上原函数的宣告 procedure TForm1.FormCreate(Sender: TObject); begin hLibModule := LoadLibrary ('EastRiver.dll'); end; procedure TForm1.FormDestroy(Sender: TObject); begin if hLibModule <> 0 then FreeLibrary (hLibModule); end; procedure TForm1.Button3Click(Sender: TObject); var a:integer; data:pchar; timeString:pchar; varmark:char; FirstRecord:Boolean; rre:function (hPort: THandle; data: PChar; timeString: PChar; var mark: Char; FirstRecord: Boolean): Boolean; stdcall; begin a:=strtoint(edit1.Text); rre:= GetProcAddress (hLibModule, 'ReadRecord'); if not assigned (rre) then ShowMessage ('找不到 ReadRecord 函數¡') else begin if rre(a,data,timestring,varmark,firstrecord) then begin showmessage('讀取成功!'); end else begin showmessage('讀取失敗!'); end; end; end; end.不过要是我写会是这样: procedure TForm1.Button3Click(Sender: TObject); var a:integer; data:pchar; timeString:pchar; varmark:char; FirstRecord:Boolean; hLibModule: THandle; begin a:=strtoint(edit1.Text); hLibModule := LoadLibrary ('EastRiver.dll'); if hLibModule <> 0 then begin @ReadRecord := GetProcAddress(hLibModule,'ReadRecord'); if @ReadRecord <> nil then begin ReadRecord(a,data,timestring,varmark,firstrecord); ShowMessage ('讀取成功!') end else begin showmessage('讀取失敗!'); end; freelibrary(hLibModule); end else begin ShowMessage ('找不到 ReadRecord 函數¡'); end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |