Passing struct array from C# or VB.NET to C dll

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobbieJosefson
    New Member
    • Aug 2008
    • 4

    #1

    Passing struct array from C# or VB.NET to C dll

    I have a problem passing a struct array to a dll written in C. I have no problem passing it by using marshalling and all that. But the problem is when it returns the array of structures i only get the first instance back nothing more. I know i have to use the inPtr in order to make that word but that is a totaly new area for me so i dont even know where to start. Was wondering if anybody know where i could find a working example which shows how this is done.
    This guy here:
    http://forums.msdn.mic rosoft.com/en-US/netfxbcl/thread/ff06fe3d-863c-40e6-9574-d82a625a6373/
    hade the same problem. But i dont fully understand his solution. The main problem with his example is i dont know what his function looks like on the C side. He says at the end his C function recives a **mystrucarray but i dont know how to mainipulate the values of that. Can anybody help me off in the right direction. Would be so grateful.

    best regards
    /Rob
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    .Net marshaller does not have the support for marshaling struct arrays, so you have to use IntPtr pointers to copy the structs to and from the arrays, one by one.

    First of all you should have the information about the number of structs in the array you are receiving. The unmanaged pointer does not give you that information, but there should be another method in that dll which does.

    Then make sure that your struct has the

    [CODE=C#]
    [StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
    [/CODE]

    attribute before the declaration line, with optional Pack=1 (or whatever) alignment to match the byte alignment of the native dll.

    When declaring your extern function, use IntPtr in place of YOUR_STRUCT**

    [CODE=C#]
    [DllImport("nati ve.dll")]
    // unamanaged signature: void GetArray(int* pSize, YOUR_STRUCT** ppData)
    static extern void GetArray(ref int size, ref IntPtr unmanagedData); // size should be there somewhere :)
    [/CODE]



    Then you can use Marshal.Structu reToPtr and PtrToStructure methods to convert from and to IntPtr. I have these generic static methods in a class I use all the time for COM interop:

    [CODE=C#]
    public static IntPtr GetIntPtrFromSt ructArray<T>(T[] array) where T:new()
    {
    int arrayLen = array.Length;
    int structSize = Marshal.SizeOf( typeof(T));
    IntPtr ptr = Marshal.AllocCo TaskMem(arrayLe n * structSize);
    for (int i = 0; i < arrayLen; i++)
    Marshal.Structu reToPtr(array[i], (IntPtr)(ptr.To Int32() + i * structSize), false);
    return ptr;
    }
    [/CODE]

    [CODE=C#]
    public static T[] GetStructArrayF romIntPtr<T>(In tPtr ptr, int len) where T:new()
    {
    T[] array = new T[len];
    int structSize = Marshal.SizeOf( typeof(T));
    IntPtr ptrPos = ptr;
    for (int i = 0; i < len; i++)
    {
    array[i] = new T();
    Marshal.PtrToSt ructure(ptrPos, array[i]);
    Marshal.Destroy Structure(ptrPo s, typeof(T));
    ptrPos = (IntPtr)(ptrPos .ToInt32() + structSize);
    }
    Marshal.FreeCoT askMem(ptr);
    return array;
    }
    [/CODE]

    It is presumed that you will first use the first method to allocate the memory for IntPtr with AllocCoTaskMem, and then free the memory using FreeCoTaskMem in the second method.

    Comment

    • RobbieJosefson
      New Member
      • Aug 2008
      • 4

      #3
      Hi vekipeki thank you so much you really save me it works great. I would buy you a beer if i could. However i was wondering since it is a really huge dll i am creating i have another problem i was wondering about. I am passing an structure with an array of strutures within it. Such as


      public struct MyStruct{

      Public int size
      Public myInsideStruct Inside[]
      };


      public struct myInsideStruct{

      string name;

      };

      I will pass the Mystruct to the dll which. And the dll will then populate the myInsideStruct array with some number of elements and set the size variable to the amount of elements that are in the myInsideStruct array. Could someone please help me with an example of how this can be done. It would really save me (again)=) I would be so grateful.
      Best regards
      /Rob

      Comment

      • RobbieJosefson
        New Member
        • Aug 2008
        • 4

        #4
        Just want to add my question regard i want to populate an array where the size of it will be known after i called the dll since the size will be decided in the dll.

        Comment

        Working...