I am using RobertGiesecke's Unmanaged Exports Nuget package to export a .NET dll function in order to call it in a delphi5 win32 application. Everything is working perfectly when passing and returning standard types (string, int...). But I try to follow the Marshalling Sample (https://sites.google.com/site/robertgiesecke/Home/uploads#TOC-Marshalling-sample) to return an instance of a custom type defined in C# but I do not managed to access the instance in delphi correctly.
In Delphi, my code is:
type
TCreateSampleInstance = procedure(var sample: PSample); stdcall;
TSample = record
Name: WideString;
end;
PSample = ^TSample;
var
sample: PSample;
dllHandle: Cardinal;
proc4: TCreateSampleInstance;
begin
dllHandle := LoadLibrary('myDotNetAssembly.dll');
if dllHandle <> 0 then
try
@proc4 := GetProcAddress(dllHandle, PChar('CreateSampleInstance'));
if Assigned(proc4) then
begin
proc4(sample);
// how to access sample properties ?
Caption := sample^.Name; // Name = '' here instead of 'Test'...
end;
finally
FreeLibrary(dllHandle);
end;
end;
Thank you in advance for your help!
You've probably got an extra layer of indirection. The Delphi code you have marshals a pointer to a pointer to the record. I expect that the C# code marshals a pointer to the record. I expect this not least because it takes quite a bit of effort to marshal a pointer to a pointer to the record in C#.
My guess is that the C# code is like this:
In which case you need to write it all like this:
C#
Delphi
On the Delphi side, I wrote it with load-time linking for simplicity. You can readily adapt to run-time linking if that's a requirement for you.