繁体   English   中英

如何用正确的变量值在Delphi中调用dll C函数?

[英]How to call a dll C function in Delphi with the correct variable values?

我已经将以下内容从“ RFIDAPI.dll” C函数转换为Delphi:

bool SAAT_YTagSelect( void *pHandle, 
                      unsigned char nOpEnable, 
                      unsigned char nMatchType, 
                      unsigned char *MatchData, 
                      unsigned char nLenth )

function SAAT_YTagSelect( pHandle: Pointer; 
                          nOpEnable, 
                          nMatchType, 
                          MatchData, 
                          nLenth: PAnsichar): Boolean; stdcall;

我正在尝试调用该函数,但出现访问冲突。 显然,我没有为nOpEnable 1Byte变量分配正确的值。

可变的nOpEnable蜂鸣器或LED启用(1byte):

1: enable
0: disable
7   6   5   4   3   led buzzer
N/A N/A N/A N/A N/A 1   1
procedure TForm5.Button4Click(Sender: TObject);
 var
  hp: Pointer;
  b: array[0..7] of AnsiChar;
begin
   b[0] := '1';
   b[1] := '1';
   b[2] := '0';
   b[3] := '0';
   b[4] := '0';
   b[5] := '0';
   b[6] := '0';
   b[7] := '0';
    if SAAT_YTagSelect(hp, b, '0x01', '84500080', '8') then
      StatusBar1.Panels[1].Text := 'Tag Selected';
end;

unsigned char参数是1字节整数类型,而不是字符串...因此它们对应于Delphi的Byte而不是PAnsiChar 每个蜂鸣器/ LED的数字是该字节中要设置的位位置,而不是字符串中的字符位置。 因此,原型可能应该是:

function SAAT_YTagSelect(pHandle: Pointer; nOpEnable, nMatchType: Byte; MatchData: PByte; nLenth: Byte): Boolean; stdcall;

呼叫应该是这样的:

procedure TForm5.Button4Click(Sender: TObject);
var
    hp: Pointer;
    b: Byte;
    data: PAnsiChar;
begin
    // set hp appropriately first
    b := 1 or 2; // Bitwise OR the values of each set bit
    data := '84500080';
    if SAAT_YTagSelect(hp, b, 1, PByte(data), 8) then
        StatusBar1.Panels[1].Text := 'Tag Selected';
end;

同样,您也没有将hp指向任何目标,根据其用途,这可能是一个问题。

函数签名的正确翻译是:

function SAAT_YTagSelect(pHandle: Pointer; nOpEnable, nMatchType: Byte; MatchData: PByte; nLenth: Byte): Boolean; stdcall;

用法看起来像这样:

var
  hp: Pointer;
  b: Byte;
  Data: PAnsiChar;
begin
   SAAT_TCPInit(hp, '192.168.0.238', 7086);
   SAAT_Open(hp);
   ...
   b := 1 or 2;
   data := '84500080';
   if SAAT_YTagSelect(hp, b, 1, PByte(data), 8) then
     StatusBar1.Panels[1].Text := 'Tag Selected';
   ...
   SAAT_Close(hp);
end;

我找不到SAAT_YTagSelect()函数的任何文档,因此很难确切地知道MatchData参数的MatchData 考虑到您习惯于将字符串用作数字参数,它甚至可能更像这样:

var
  hp: Pointer;
  b: Byte;
  Data: array[0..7] of Byte;
begin
   SAAT_TCPInit(hp, '192.168.0.238', 7086);
   SAAT_Open(hp);
   ...
   b := 1 or 2;
   data[0] := 8;
   data[1] := 4;
   data[2] := 5;
   data[3] := 0;
   data[4] := 0;
   data[5] := 0;
   data[6] := 8;
   data[7] := 0;
   if SAAT_YTagSelect(hp, b, $01, @data[0], 8) then
     StatusBar1.Panels[1].Text := 'Tag Selected';
   ...
   SAAT_Close(hp);
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM