DELPHI 调用标准C接口DLL char *value


 delphi调用标准C接口DLL char * 的调用,因内部实现的不同。并无标准代码可用,需要跟据接口实际情况来调整入参类型与格式。

 随手所记,并未全部验证

调用方式0

申明

iReadM1Card:function(SecNr:Integer;DataBlock:Integer; SecKey:PAnsiChar;PwdType:Integer;pOutInfo:PAnsiChar):Integer;stdcall; var Errcode:Integer; ReadInfo:PAnsiChar; OutValue1: WideString; begin OutValue:=''; ReadInfo:=GetMemory(200); try Errcode:=iReadM1Card(1,0,'AA',0,ReadInfo) ; OutValue1:=StrPas(ReadInfo); if Errcode<0 then begin ...... Exit; end; OutValue:=leftstr(OutValue1,7) ;//取前7位 finally FreeMemory(ReadInfo); end;

申明格式1

interface

uses SysUtils;

const CallDLL='DC_Reader.dll';

var AERROR:STRING;
{函数名称:iReadM1Card
  函数功能:通用读M1卡
  long __stdcall iReadM1Card(int SecNr, int DataBlock, char *SecKey, int PwdType, char *pOutInfo)

   参数说明:[IN] SecNr        扇区编号(0~15)
      [IN] DataBlock    块编号(0~3)
      [IN] SecKey        密钥数据,为空时默认“FFFFFFFFFFFF”
      [IN] PwdType        密钥类型 0:0套Type-A, 4:0套Type-B 1:1套Type-A, 5:1套Type-B 2:2套Type-A, 6:2套Type-B
      [OUT] pchOutInfo    成功:数据信息 失败:错误描述信息

  返回值:  成功:返回0;
      失败:返回小于0的错误码。}
  iReadM1Card:function(SecNr:Integer;DataBlock:Integer; SecKey:PAnsiChar;PwdType:Integer;var pOutInfo:PAnsiChar):Integer;stdcall;

通用动态DLL加载

var
    LibHandle:THandle;


 LibHandle := Loadlibrary(PChar(CallDLL) );//'DC_Reader.dll'
  if LibHandle <= 32 then
  begin
    AERROR := '加载动态链接库'+CallDLL+'失败,错误代码:' + IntToStr(LibHandle);
     exit;
  end;
  try

   @iReadM1Card := GetProcAddress(LibHandle, PChar('iReadM1Card'));
   

     Result:=True;
  except
     on E:Exception do
     begin
       ......end;
  end;

调用方式1

var Errcode:Integer;
  ReadInfo:PAnsiChar;
  OutValue1: WideString;
begin
  Result:=False;


    OutValue:='';
    ReadInfo:=GetMemory(1000);
    try
      Errcode:=iReadM1Card(1,0,'AA',0,ReadInfo) ;
       OutValue1:=StrPas(ReadInfo);
      if Errcode<0 then
      begin
       ......
        Exit;
      end;
      OutValue:=leftstr(OutValue1,7) ;/取前7位
    finally
      FreeMemory(ReadInfo);
    end;
       
  Result:=True;

 调用方式2

var Errcode:Integer;
 P:Pointer;
 buff:array[0..32] of Char;
begin
  p:=AnsiStrAlloc(32);
   ZeroMemory(@p,32);
      Errcode:=iReadM1Card1(1,0,'AC14654634CA',0,p) ;
        CopyMemory(@buff[0],p,32);

 申明格式2 数组

  iReadM1Card:function(SecNr:Integer;DataBlock:Integer; SecKey:PAnsiChar;PwdType:Integer;var pOutInfo:TBytes):Integer;stdcall;

调用格式2

var Errcode:Integer;
  ReadInfo:TBytes;
 OutValue, OutValue1: WideString;
 i:Integer;
Begin

 OutValue:='';

    SetLength(ReadInfo,32);
    try
      Errcode:=iReadM1Card(1,0,'AA',0,ReadInfo) ;

   for I := Low(ReadInfo) to High(ReadInfo) do
       begin
         OutValue1:=OutValue1 + Char(ReadInfo[i]);
       end;
    if Errcode<0 then
      begin
        Exit;
      end;
      OutValue:=leftstr(OutValue1,7) ;//取前7位
    finally
      FreeMemory(ReadInfo);
    end;
        
end