惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
博客园 - 司徒正美
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog

博客园 - 周黔

delphi firemonkey 12.3 动态绑定TFDMemTable到TStringGrid delphian firemonkey. android:exported 错误 RAD Studio 12 Delphi 12 实现手机无线wifi调试 (巧用Android Studio 2024 连WIFI) delphi 11 HMACSha512 Android Studio 2024 不需要三方插件,直接wifi 开发调试,真方便 CEF4Delphi(DELPHI Google Chrome 浏览器封装) 折腾 (2)--打开一个网页 CEF4Delphi(DELPHI Google Chrome 浏览器封装) 折腾 (1)--安装 Delphi WINAPI 任意拖动移动控件 Apache NetBeans IDE 12.3 双击无反应怪事 [转]Regions and Clipping in GDI+ delphi 10.4 JSON 建立数组示例 delphi 10.3实现TNetHTTPClient 异步 POST 另类实现返回附加标记及Unicode 编码与解码 \u - 周黔 小程序开发笔记1 Delphi DLL 消息循环 Synchronize SendMessage PostMessage 用户中心 - 博客园 delphi 跨版本DLL调用嵌入窗体实现 DELPHI GDI + TGPFont UnitPixel 问题解决 delphi DLL image 动态绘图 句柄处理 delphi 每英寸相素点取值偏差
DELPHI 调用标准C接口DLL char *value
周黔 · 2022-05-30 · via 博客园 - 周黔

 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