locked
which unmanaged type should be used...?? RRS feed

  • Question

  • Hi,

    for my graduation thesis, i need to call a native DLL into C#..the DLL has struct like this

    typedef struct
    {
       unsigned char TagType;
       unsigned char AntNum;
       unsigned char Ids[12];
    }TagIds;

    i have converted the struct into C# like this

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

    public struct TagIds

    {

    public byte TagType;

    public byte AntNum;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]

    public string[] Ids;

    }


    am i wrong or is there any mistake..??

    thanks in advance

    Wednesday, January 27, 2010 12:20 AM

Answers

  • you converted char[] to string[], which is not supported by the default marshaller.  If you want to continue using an arrary to store the ids, i suggest you to use BYTE in your declaration, which is typedefed into unsigned char.

    //Generated by P/invoke Interop Assistant version 1.0
        /// BYTE[12]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
        public byte[] Ids;


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, January 27, 2010 1:29 AM

All replies

  • you converted char[] to string[], which is not supported by the default marshaller.  If you want to continue using an arrary to store the ids, i suggest you to use BYTE in your declaration, which is typedefed into unsigned char.

    //Generated by P/invoke Interop Assistant version 1.0
        /// BYTE[12]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=12, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
        public byte[] Ids;


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, January 27, 2010 1:29 AM
  • P/invoke Interop Assistant  tool converts the struct

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

    public struct TagIds

    {
    ....

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]

    public string Ids;
    }

    i don't have enough practise with interoperability and don't know all marshalling rules..which one should i choose??

    Wednesday, January 27, 2010 8:25 AM
  • Depends on how you plan to use the data. Do you want an array of bytes, or a string?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, January 27, 2010 1:02 PM
  • Depends on how you plan to use the data. Do you want an array of bytes, or a string?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP


    so i want to use array of bytes, i need to do

    public struct TagIds

    {
    ...

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12,ArraySubType=UnmanagedType.I1)]

    public byte[] Ids;

    }


    if i want to use a string, i need to do

    public struct TagIds

    {
    ...

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)] public string Ids;

    }

    is it true??

    Wednesday, January 27, 2010 2:15 PM
  • Yes, when your structure's charset setting is ANSI.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Wednesday, January 27, 2010 6:52 PM
  • i read an article and it was said althouhg string is a reference type, when a string parameter is passed to a method it behaves like a value type..but Arrays behave like reference types..?? if so, for this structure if i use string,does callee see the changes a caller makes to string type..??

    otherwise we have done this using [in,out] directional attribute,ref or out keyword for the parameter which is defined with this structure type when the imported function is declared??
    Thursday, January 28, 2010 8:37 AM
  • Both string and byte array are reference types.  It does not matter how data is stored in memory on the managed heap. The way the default marshaller copy data to/from the native heap matters.

    The default marshaller uses the COM heap, so you need to allocate memory on the COM heap if you return a block of memory in ref. 


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Thursday, January 28, 2010 12:18 PM