Question

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!

Was it helpful?

Solution

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:

public static void CreateSampleInstance(out Sample sample)

In which case you need to write it all like this:

C#

public struct Sample
{
    [MarshalAs(UnmanagedType.BStr)]
    string Name;
}

[DllExport]
public static void CreateSampleInstance(out Sample sample)
{
    sample.Name = "boo yah";
}

Delphi

type
  TSample = record
    Name: WideString;
  end;

procedure CreateSampleInstance(out sample: TSample); stdcall; 
  external 'myDotNetAssembly.dll';

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top