delphi 如何使用PUCHAR调用API?

ruoxqz4g  于 2023-08-04  发布在  其他
关注(0)|答案(5)|浏览(111)

我正在尝试使用 Delphi 下的API。以下是API文档:

OKERR ENTRY SCardCLMifareStdAuthent 
    (IN SCARDHANDLE ulHandleCard,IN ULONG ulMifareBlockNr,
    IN UCHAR ucMifareAuthMode,IN UCHAR ucMifareAccessType,IN UCHAR ucMifareKeyNr,
    IN PUCHAR pucMifareKey,IN ULONG ulMifareKeyLen);

字符串
pucMifareKey:
指向六字节Mifare键的指针。
目前为止我一直在尝试的代码:

function Auth():Integer;
type
  TSCardCLMifareStdAuthent = function(SCARDHANDLE: cardinal; ulMifareBlockNr: ULONG;
   ucMifareAuthMode, ucMifareAccessType, ucMifareKeyNr: byte; pucMifareKey: puchar;
   ulMifareKeyLen: cardinal):LONG;
var
  SCardCLMifareStdAuthent: TSCardCLMifareStdAuthent;
  hDLL: Integer;
  CardHandle: cardinal;
  i: integer;
  Key: array of UCHAR;
begin
  Result:=1;
  //CardHandle is defined here
  SetLength(Key, 6);
  for i := low(key) to high(key) do
    Key[i] := $FF;
  hDLL := LoadLibrary('scardsyn.dll');
  @SCardCLMifareStdAuthent := GetProcAddress(hDLL, 'SCardCLMifareStdAuthent');
  if @SCardCLMifareStdAuthent <> nil then
    Result:=SCardCLMifareStdAuthent(CardHandle, $00, 96, 0, 0, ^Key, 6);
  FreeLibrary(hDLL);
end;


我得到的错误是:
不兼容类型:“Byte”和“Charar”位于
结果:=SCardCL.... due ^键指针。
有什么想法吗

j13ufse2

j13ufse21#

type
  TSCardCLMifareStdAuthent = function(SCARDHANDLE: cardinal; ulMifareBlockNr: ULONG;
    ucMifareAuthMode, ucMifareAccessType, ucMifareKeyNr: byte; pucMifareKey: puchar;
    ulMifareKeyLen: cardinal):LONG;

字符串
我认为你应该用'UCHAR'替换'byte':

ucMifareAuthMode, ucMifareAccessType, ucMifareKeyNr: byte;


把'byte'当作'char'是一种不好的做法。

u91tlkcl

u91tlkcl2#

我不知道有问题的DLL,但对于Delphi 2009年之前的版本,我会做以下操作:

function Auth: integer;
type
  TSCardCLMifareStdAuthent = function(SCARDHANDLE: cardinal;
    ulMifareBlockNr: ULONG; ucMifareAuthMode, ucMifareAccessType,
    ucMifareKeyNr: byte; pucMifareKey: PAnsiChar;
    ulMifareKeyLen: Cardinal): longint;
var
  SCardCLMifareStdAuthent: TSCardCLMifareStdAuthent;
  hDLL: Integer;
  CardHandle: Cardinal;
  Key: string;
begin
  Result := 1;
  //CardHandle is defined here...
  Key := StringOfChar(Chr($FF), 6);
  hDLL := LoadLibrary('scardsyn.dll');
  if hDLL <> 0 then begin
    @SCardCLMifareStdAuthent := GetProcAddress(hDLL, 'SCardCLMifareStdAuthent');
    if @SCardCLMifareStdAuthent <> nil then begin
      Result := SCardCLMifareStdAuthent(CardHandle, $00, 96, 0, 0,
        PChar(Key), Length(Key));
    end;
    FreeLibrary(hDLL);
  end;
end;

字符串
您得到的访问冲突可能来自C字符串不是以0结尾的。使用字符串并将其转换为PCar将使事情变得更容易。

l7wslrjt

l7wslrjt3#

你不会碰巧在2009年 Delphi 吧?如果是,则“char”被定义为2个字节。试试“AnsiChar”。

omqzjyyz

omqzjyyz4#

您声明键是一个6字节(uchar)数组。你正在声明一个动态数组来沿着,为什么不直接声明一个数组呢?

var
  {...}
  MyKey: array[0..5] of UCHAR;
begin
  {...}
  Result:=SCardCLMifareStdAuthent(CardHandle, $00, 96, 0, 0, @MyKey, 6);

字符串

eivnm1vs

eivnm1vs5#

试试这个:
1.介绍了UCHAR数组的新类型。
1.引入指针类型的新局部变量。
1.将此变量设置为指向Key数组的指针
1.在函数调用中使用此指针

type 
  TArrayOfUchar = array of UCHAR;

var 
  ...
  Key  : TArrayOfUchar;
  PKey : ^TArrayOfUchar; 
begin
  ...
  PKey = @Key;
  if @SCardCLMifareStdAuthent <> nil then
    Result:=SCardCLMifareStdAuthent(CardHandle, $00, 96, 0, 0, PKey, 6);

字符串

相关问题