1

I am trying to convert c++ struct to delphi Record for dll call

// 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;

I get empty output

I am not sure if converted record is correct ?

I have checked online char in delphi is Ansichar ?

0

1 Answer 1

2

char version[32] is not the same as AnsiChar, because that AnsiChar is a single character. You need an array of AnsiChar, (version: array [0..31] of AnsiChar) just like what is used in the C code. You'll need the proper declaration for all of the members of the record.

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;

Whether or not stdcall is correct depends on the definition of the LIB_API macro.

Sign up to request clarification or add additional context in comments.

1 Comment

@Maaz Note that you should add error checking on your Windows API calls, and possibly a call to FreeLibrary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.