I am using RGiesecke's "Unmanaged Exports" package to create a dll from C# that can be called from a Delphi application.
Specifically, I am looking to pass an array of arrays of a struct.
What I have made work in C# is
public struct MyVector
{
public float X;
public float Y;
}
[DllExport]
public static void DoStuff([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
MyVector[] vectors, int count)
{
// Do stuff
}
Which can then be called from Delphi, doing something like this:
unit MyUnit
interface
type
TVector = array[X..Y] of single;
TVectorCollection = array of TVector;
procedure TDoExternalStuff(const vectors : TVectorCollection; count : integer; stdcall;
procedure DoSomeWork;
implementation
procedure DoSomeWork;
var
vectors : array of TVector;
fDoExternalStuff : TDoExternalStuff;
Handle: THandle;
begin
// omitted: create and fill vectors
Handle := LoadLibrary('MyExport.dll');
@fDoExternalStuff := GetProcAddress(Handle, 'DoStuff');
fDoExternalStuff(vectors, Length(vectors));
end;
end.
However, what I really need to do is to pass an array of array of TVector. An array of structs that hold an array of TVector would also do. But writing
[DllExport]
public static void DoStuff([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
MyVector[][] vectors, int count)
{
// Do stuff
}
Does not work with Delphi
...
TVectorCollection = array of array of TVector;
...
procedure DoSomeWork;
var
vectors : array of array of TVector;
fDoExternalStuff : TDoExternalStuff;
Handle: THandle;
begin
// omitted: create and fill vectors
Handle := LoadLibrary('MyExport.dll');
@fDoExternalStuff := GetProcAddress(Handle, 'DoStuff');
fDoExternalStuff(vectors, Length(vectors)); //external error
end;
And I would also be a bit surprised if it did, since I am not specifying the length of the individual elements of the jagged array anywhere.
Is there a way for me to setup my DllExport function to be able to marshal this type of element?
TVectorCollection, be careful about this. On the C# side this is treated as a raw pointer. So it's fine as you have it, but if you were to go in the opposite direction this would break down.