I have created a DLL with one exported function using the latest version of Delphi (12.1). The function takes one parameter: a record type variable.
library MyDLL;
uses
System, SysUtils;
type
TMyRecord = record
MyString: AnsiString;
MyInteger: Integer;
end;
function FillRecord(var Rec: TMyRecord): Boolean; stdcall; export;
begin
Rec.MyString := 'Hello from Delphi';
Rec.MyInteger := 42;
Result := True;
end;
exports
FillRecord;
begin
end.
In my C++ Builder 6.0 application, I have declared the following:
struct TMyRecord {
char *MyString;
int MyInteger;
};
extern "C" __declspec(dllimport) bool __stdcall FillRecord(TMyRecord *Rec);
When calling the 'FillRecord' function from my C++ Builder application, I do not get the expected results:
TMyRecord iMyRec;
Memo1->Lines->Clear();
Memo1->Lines->Add(Format("Address: %p", ARRAYOFCONST((&iMyRec))));
if (FillRecord(&iMyRec)) {
String iData = iMyRec.MyString;
Memo1->Lines->Add("iMyRec.MyString: " + iData);
int iNumber = iMyRec.MyInteger;
Memo1->Lines->Add("iMyRec.MyInteger: " + IntToStr(iNumber));
} else {
Memo1->Lines->Add("Error calling FillRecord");
}
I am expecting:
iMyRec.MyString: Hello from Delphi
iMyRec.MyInteger: 42
But I am getting:
iMyRec.MyString: H
iMyRec.MyInteger: 42
I am drawing a blank when trying to figure out what I am doing wrong. Any inputs/suggestions to solve my issue would be greatly appreciated. Thank you