I am writing a C# application in which it would call a DLL that was written in Delphi. I have the source code of Delphi DLL. I want to send a 2D array from C# to DLL and the DLL creates another 2D array and send it back to C# code. How can I do this?
Share
I’ve written you some sample code for 2D arrays. It’s a bit messy because the marshaller won’t naturally handle 2D arrays. Instead I’ve opted for flattening the arrays, i.e. marshall them as 1D arrays. This won’t have great performance characteristics but perhaps that won’t be significant for you. Only you can know that.
Delphi
C#
The code passes a 2D array from C# to Delphi where it is stored. Then the C# code asks for it back. The
WriteLine()statements show that the same values are returned as were passed.I have arranged for
NativeToManaged()to return the dimensions of the array even though this code already knows them. In reality your C# code is likely going to want to ask the Delphi code for the array size so that it can allocate memory in which to store the values.I won’t go on and on about why I’ve done certain things. From previous questions I think you have enough expertise to work it out from this code. If you do have any questions, leave a comment and I’ll do my best to shed some light!