none
Passing structure containing pointer from c# to unmanaged dll RRS feed

  • Question

  • How can I pass following structure from c# to unmanaged dll ?

    typedef struct _TEST_
    {
        int length;
        BYTE *byData
    } TEST;

    Thursday, January 8, 2009 2:27 PM

Answers

  • public struct TEST
    {
        public int length; //assuming 32-bit int
        [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1)] public byte[] byData;
    }


    -Rob Teixeira
    • Marked as answer by Zhi-Xin Ye Wednesday, January 14, 2009 9:33 AM
    Thursday, January 8, 2009 6:24 PM
  • If "struct TEST" is in fact a variable-length structure, you should read How do I deal with variable length structures? from the N/Direct FAQ. In short, you have to do your own marshalling by hand.

           -Steve
    • Marked as answer by Zhi-Xin Ye Wednesday, January 14, 2009 9:33 AM
    Friday, January 9, 2009 2:26 AM

All replies

  • public struct TEST
    {
        public int length; //assuming 32-bit int
        [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1)] public byte[] byData;
    }


    -Rob Teixeira
    • Marked as answer by Zhi-Xin Ye Wednesday, January 14, 2009 9:33 AM
    Thursday, January 8, 2009 6:24 PM
  • If "struct TEST" is in fact a variable-length structure, you should read How do I deal with variable length structures? from the N/Direct FAQ. In short, you have to do your own marshalling by hand.

           -Steve
    • Marked as answer by Zhi-Xin Ye Wednesday, January 14, 2009 9:33 AM
    Friday, January 9, 2009 2:26 AM