2

I am trying to use a structure from a C DLL in a C# application. I can't get the equivalent data types for the structure I am dealing with. I am dealing with these data types from C:

struct teststruct {
    unsigned short protocol_type;       //c#'s UInt16 ?
    unsigned char hardwareSize;         //c#'s byte?
    UCHAR dest[6];                      //unsigned char and thus byte in c#?
    unsigned char ip_version : 4;       //How to get the exact same thing in c#?!!
    unsigned char Nonce_Sum_Flag: 1;    //the same as above
    unsigned char ip[4];                // !!
    unsigned short  in6_addr_src[8];    // !!
    char* astring;                      //string!?    
    char  anarray[10];                  //string? or char[]?
    const char* conststring;            //string? or array[] or char[]?   
};

For the structure itself I think putting the in advance

[StructLayout(LayoutKind.Sequential)]

suffices.

This is my C# structure; as it is clear, I'm stuck with the middle two and last 3 data types. I have no clue how to go about them!

[StructLayout(LayoutKind.Sequential)]
public struct teststruct
{
    public UInt16 protocol_type;                     //c's unsigned short ?
    public byte hardwareSize;                        //c's unsigned char?
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public byte[] dest;                              //c's unsigned char ?
    unsigned char ip_version : 4;                    //How to get the exact same thing in c#?!!
    unsigned char Nonce_Sum_Flag : 1;                //the same as above
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public byte[] ip;                                 // right?!
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public UInt16[]   in6_addr_src;                   // !!
    char* astring;                                    // !?    
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public byte[] anarray;                           //string? or char[]?
    const char* conststring;                         //string? or array[] or char[]?   
}

Would someone especially advise me on the C unique notion on bit allocation? I mean this line:

unsigned char Nonce_Sum_Flag: 1;

and also the last 3 ones!

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224

3 Answers3

2

use [MarshalAs(UnmanagedType.LPStr)]string for char*

For bitfield unsigned char ip_version : 4; , there is no direct support in c#, you got implement on your own this link may help.

Balu
  • 2,247
  • 1
  • 18
  • 23
2

Most datatypes were guessed right.

Check out the table below, it's a table with the C++ datatypes which are supposed to be the C# datatypes, sizes are included too.

Columns order: C++ datatype, C# datatype, Size

BOOL                            bool                    1 byte
BYTE                            byte                    1 byte
CHAR                            byte                    1 byte
DECIMAL                         Decimal                 16 bytes
DOUBLE                          double                  8 bytes
DWORD                           uint, UInt32            4 bytes
FLOAT                           float, single           4 bytes
INT, signed int                 int, Int32              4 bytes
INT16, signed short int         short, Int16            2 bytes
INT32, signed int               int, Int32              4 bytes
INT64                           long, Int64             8 bytes
LONG                            int, Int32              4 bytes
LONG32, signed int              int, Int32              4 bytes
LONG64                          long, Int64             8 bytes
LONGLONG                        long, Int64             8 bytes
SHORT, signed short int         short, Int16            2 bytes
UCHAR, unsigned char            byte                    1 byte
UINT, unsigned int              uint, UInt32            4 bytes
UINT16  ushort,                 UInt16                  2 bytes
UINT32, unsigned int            uint, UInt32            4 bytes
UINT64  ulong,                  UInt64                  8 bytes
ULONG, unsigned long            uint, UInt32            4 bytes
ULONG32 uint,                   UInt32                  4 bytes
ULONG64 ulong,                  UInt64                  8 bytes
ULONGLONG   ulong,              UInt64                  8 bytes
void*, pointers                 IntPtr                  x86=4 bytes, x64=8 bytes

Source

  • 1
    that doesn't answer the question, and the link doesn't either. – thumbmunkeys Mar 28 '14 at 09:33
  • @thumbmunkeys The question is about the datatypes, which one to use for `C#` compared to his old `C` struct –  Mar 28 '14 at 09:37
  • the quesiton was specifically about the bitfields and the arrays, but in part at least the link answers it – thumbmunkeys Mar 28 '14 at 09:40
  • @thumbmunkeys So at the end we're still talking about the datatypes, am I right? That's why I provided the link. –  Mar 28 '14 at 09:42
  • No, the question was specifically about the bitfields and the arrays, that is not answered in your link. Also, the link points to a website, where I am not sure that the link will stay for the future. Generally, your answer should answer the question to avoid situations like this. – thumbmunkeys Mar 28 '14 at 12:23
  • This answer is weak. It contains no information beyond an off site link. – David Heffernan Mar 31 '14 at 17:52
  • char is 2 bytes in C#! – Hossein Apr 05 '14 at 11:14
2

In C# you have to read all the bit fields as a single byte or int, and pack/unpack them using bit mask functions.

david.pfx
  • 10,520
  • 3
  • 30
  • 63