简体   繁体   中英

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

I've converted the following from "RFIDAPI.dll" C function to Delphi:

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

to

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

I am trying to call the function and I am getting an Access Violation. Apparently I am not assigning the correct value to the nOpEnable 1Byte variable.

Variable nOpEnable Buzzer or LED enable(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;

The unsigned char parameters are 1-byte integral types, not strings... so they correspond to Delphi's Byte rather than PAnsiChar . The numbers for each buzzer/LED are the bit positions to set in that byte, not a character position in a string. So, the prototype should probably be:

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

and the call should be something like:

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;

Also you haven't pointed hp at anything, which is likely a problem depending on what it's for.

The correct translation of the function signature is:

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

And the usage would look something like this:

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;

I can't find any documentation for the SAAT_YTagSelect() function, so it is difficult to know for sure exactly what the MatchData parameter is expecting. Considering that you have a habit of using strings for numeric parameters, it might even be something more like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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