【问题标题】:C++ struct to Delphi Record dll callC++ 结构到 Delphi Record dll 调用
【发布时间】:2020-06-20 12:32:31
【问题描述】:

我正在尝试将 c++ struct 转换为 delphi Record 以进行 dll 调用

// c++ struct

typedef struct dll_info_t
{
   char version[32];
   char r_type[128];
   char b_date[32];
   char c_list[32];
   char build[32];
}dll_info_t;

LIBEX_EXPORT int LIB_API Dll_get_lib_info(dll_info_t* info);

// Delphi Converted

dll_info_t = record
 version:AnsiChar;
 r_type:AnsiChar;
 b_date:AnsiChar;
 c_list:AnsiChar;
 build:AnsiChar;
end;
        
Dll_get_lib_info: Function (info : dll_info_t) : Integer; stdcall;

var
  hHandle:THandle;
begin
  hHandle := LoadLibrary(Dl_path);
    @Dll_get_lib_info:=GetProcAddress(hHandle, PChar('Dll_get_lib_info'));

    if Assigned(Dll_get_lib_info) then begin
     Dll_get_lib_info(info);
     ShowMessage(info.version); // <- I Get Empty Output
     ShowMessage(info.release_type); // <- I Get Empty Output
     ShowMessage(info.build_date); // <- I Get Empty Output
     ShowMessage(info.change_list); // <- I Get Empty Output
    end;

end;

我得到空输出

我不确定转换后的记录是否正确?

我已经在 delphi 中检查了在线 char 是 Ansichar 吗?

【问题讨论】:

    标签: c++ delphi struct record


    【解决方案1】:

    char version[32]AnsiChar 不同,因为 AnsiChar 是单个字符。您需要一个 AnsiChar 数组 (version: array [0..31] of AnsiChar),就像在 C 代码中使用的一样。您需要为记录的所有成员正确声明。

    type
      dll_info_t = record
        version: array [0..31] of AnsiChar;
        r_type: array [0..127] of AnsiChar;
        b_date: array [0..31] of AnsiChar;
        c_list: array [0..31] of AnsiChar;
        build: array [0..31] of AnsiChar;
      end;
    
    var 
      Dll_get_lib_info: Function(out info: dll_info_t): Integer; stdcall;
      hMod: HMODULE;
      info: dll_info_t;
    begin
      hMod := LoadLibrary(Dl_path);
      @Dll_get_lib_info := GetProcAddress(hMod, 'Dll_get_lib_info');
    
      if Assigned(Dll_get_lib_info) then begin
        Dll_get_lib_info(info);
        ShowMessage(info.version);
        ShowMessage(info.release_type);
        ShowMessage(info.build_date);
        ShowMessage(info.change_list);
      end;
    end;
    

    stdcall 是否正确取决于LIB_API 宏的定义。

    【讨论】:

    • @Maaz 请注意,您应该在 Windows API 调用上添加错误检查,并可能调用FreeLibrary
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多