1

I have a 3rd party DLL containing the following function:

SDK_API FunctionInQuestion(char* name, myStruct table[row][column]);

I'm pretty sure this function WILL modify myStruct table[row][column].

I need to call this from .net, here is how I tried (the used language is VB.NET, but if you know how to do it in C# thats not a problem, I'm pretty sure the principles are the same)

<System.Runtime.InteropServices.DllImportAttribute("dllinquestion.dll", EntryPoint:="FunctionInQuestion", CallingConvention:=Runtime.InteropServices.CallingConvention.Cdecl)> _
Public Shared Function FunctionInQuestion(ByVal name As System.Text.StringBuilder, ByRef table()() As myStruct) As Integer
End Function

myStruct C:

typedef struct
{
   unsigned short int x;  
   unsigned short int y;  
}myStruct;

myStruct .net:

<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure myStruct
    Public x As UShort
    Public y As UShort
End Structure

I've been Googling/searching Stackoverflow for a couple hours now and tried every 'solution' thus far, but I haven't been able to make it work. Please if you redirect/vote to close the question, I beg you to first look at the other question. If it's really a marshalling of a 2D struct array AND you are sure the question contains an answer, then please by all means close this, all the better.

4
  • You aren't going to be able to marshal it like that. In VB or C#. Marshal as a linear array, and do the 2D indexing conversion to 1D index manually. Commented Aug 10, 2015 at 13:30
  • @DavidHeffernan Im okay with converting my 2D array to 1D, I can do that no problem. But are you sure the C++ code will not break if I pass a linear array instead of what it expects whatever that is? Will/won't it matter? Commented Aug 10, 2015 at 13:36
  • In memory, the C++ code is really myStruct table[][column]. You'd just got a row major array. The elements are 0,0, 0,1, ..., 0,column-1, 1,0, 1,1, ..., 1,column-1, 2,0 and so on. Commented Aug 10, 2015 at 13:58
  • @DavidHeffernan Thank you, it worked. It did work when I tried it an hour ago, but I did some testing to make sure everything went Ok. If you would be so kind to post an answer I'd be able to accept it, because it is the correct answer, thank you. Commented Aug 10, 2015 at 14:42

1 Answer 1

1

You should marshal this as a simple linear array. I'm more familiar with C#, but in VB I think it would run like this:

ByVal table() As myStruct

Then you'll need to perform the 2D to 1D indexing conversion manually in the .net code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.