In Delphi I have a structure like this:
TCustomerInfo = Packed Record
CustomerNo: Integer;
FirstName: String[50];
LastName: String[50];
End;
With a dummy-proc like this:
procedure GetCustomer(CustomerNo: Integer; var CustomerInfo: TCustomerInfo);
begin
CustomerInfo.CustomerNo := 19901;
CustomerInfo.FirstName := 'JOHN';
CustomerInfo.LastName := 'DOE';
end;
In C# I have this:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
struct CUSTOMER_INFO
{
public Int32 CustomerNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
public string FirstName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)]
public string LastName;
}
With an imported Delphi function like this:
[DllImport("Exceline.dll")]
extern static void GetCustomer(Int32 CustomerNo, ref CUSTOMER_INFO CustomerInfo);
The idea is to make sure all memory allocation and storage is being handled by the C# application.
My problem is that nothing gets assigned to my C# struct upon return from GetCustomer :-/
I finally came up with a solution which avoids all the Alloc/FreeHGlobal, but if this is truly bulletproff with regards to the garbage collector is another matter.
The solution is to first clear the TCustomer structure with FillChar then copy the data using the Move procedure.
The delphi record looks like this:
Then i copy the string to the structure with a procedure:
Within a proc more or less like this:
Finally the C# struct looks something like this: