Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

convert c++ .h to c#

Back to Forum: Tech Off
  • User profile image
    matrixr

    hey all

    i have a c++ header file with a crazy looking struct which is about 150 lines. anyone here know of a tool which can convert that into c# ...even partially so i can fix up the odd things because right now i have no idea where to begin.

    heres some part of the struct
    ...
    struct LINK_MESSAGE {
      unsigned char MessageLength;
      union {
        unsigned char Data[255];
        struct {
          unsigned char MessageType;
          union {
            struct /* NAME_DATA (8 bit) */ {
              unsigned char ItemType8;
              unsigned char ItemNumber8;
              unsigned char ItemName8[16];
            };
            struct /* NAME_DATA (16 bit) */ {
              unsigned char ItemType16;
              unsigned char ItemNumber16MSB;
              unsigned char ItemNumber16LSB;
              unsigned char ItemName16[16];
            };
    ...and it goes on and on

  • User profile image
    Maurits

    One possibility is just to do this and be done with it.

    struct LINK_MESSAGE {
      unsigned char MessageLength;
      unsigned char Data[255];
    }

  • User profile image
    matrixr

    i dont know if that'll work because i need to interept and construct messages. for example:

    LINK_MESSAGE msg;
    msg.MessageLength = i;
    msg.MessageType = MessegeType.TEXT;
    msg.Data[n] = x;
    msg.Data[n+1] = y;
    msg.UserCode = "blahblah";
    ...etc

  • User profile image
    Maurits

    The more lengthy way is to make a list of all the struct members, their types, and their byte offsets... then use StructLayout to place each member at the byte offset it belongs.

    See Sven's post

  • User profile image
    matrixr

    ok, after messing around for awhile this is where im getting stuck.

    struct R_OMNI_LINK_MESSAGE {
      unsigned char MessageLength; // fieldoffset = 0
      union {
        unsigned char Data[255]; // filedoffset = 4
        struct { // stuct has no name?
          unsigned char MessageType; // since this struct is inside a union offset is 4?
          union {
            struct /* olmNAME_DATA (8 bit) */ { // offset of this??
              unsigned char ItemType8;
            };
          };  // union
        };  // struct
      }; // union
    };

    c# code i came up with for the above bit of code is:
    [StructLayout(LayoutKind.Explicit)]
            public struct R_OMNI_LINK_MESSAGE
            {
            [FieldOffset(0)] public int MessageLength;
            [FieldOffset(4)][MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public string Data;
            [FieldOffset(??)] public int MessageType;

            //olmNAME_DATA (8 bit)
            [FieldOffset(??)][MarshalAs(UnmanagedType.LPStr)] public string ItemType8;
    ...

    any ideas?

  • User profile image
    Maurits

    matrixr wrote:
    ok, after messing around for awhile this is where im getting stuck.

    struct R_OMNI_LINK_MESSAGE {
      unsigned char MessageLength; // fieldoffset = 0 yup
      union {
        unsigned char Data[255]; // filedoffset = 4 no, 1 - a char is one byte
        struct { // stuct has no name? field offset is still 1
          unsigned char MessageType; // since this struct is inside a union offset is 4? field offset is still 1
          union { now field offset is 2
            struct /* olmNAME_DATA (8 bit) */ { // offset of this?? still 2
              unsigned char ItemType8; still 2
            };
          };  // union
        };  // struct
      }; // union
    };

    c# code i came up with for the above bit of code is:
    [StructLayout(LayoutKind.Explicit)]
            public struct R_OMNI_LINK_MESSAGE
            {
            [FieldOffset(0)] public intbyte MessageLength;
            [FieldOffset(41)][MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public string Data;
            [FieldOffset(??1)] public int byte MessageType;

            //olmNAME_DATA (8 bit)
            [FieldOffset(??2)][MarshalAs(UnmanagedType.LPStr)] public string ItemType8;
    ...

    any ideas?


    Not sure about those LPStr and ByValTStr things... try it and see, I guess
    If it doesn't work then ItemType8 should be a byte, and Data should be a 255-byte array (of some kind that doesn't need to store an extra byte for its length)

    (sings) I've been riding through the desert on a struct with no name...

  • User profile image
    matrixr

    your right LPStr and ByValTStr doesnt work, byte works.
    [FieldOffset(1)][MarshalAs(UnmanagedType.U1, SizeConst=255)] public byte Data; // UnmanagedType.U1 seems to get rid of the 'incorrectly aligned or overlapped' error.

    now it errors out near this part
    struct /* olmSYSTEM_STATUS */ { // offset is 2
        unsigned char TimeDateValidFlag;
        unsigned char AreaSecurityMode[8];
        struct { // another struct here? if i give it offset 2 ...i get incorrectly aligned error
            unsigned char Status;
        } ExpansionEnclosure[8];
    };

    [FieldOffset(2)][MarshalAs(UnmanagedType.U1, SizeConst=8)] public ExpansionEnclosure[] Enclosure;

    [StructLayout(LayoutKind.Sequential)]
            public struct ExpansionEnclosure
            {
                public byte Status;
            }
    tried giving it 3 since currentoffset(2)+1 byte inside ExpansionEnclosure struct, still get incorrectly aligned error.

  • User profile image
    Maurits

    matrixr wrote:
    your right LPStr and ByValTStr doesnt work, byte works.
    [FieldOffset(1)][MarshalAs(UnmanagedType.U1, SizeConst=255)] public byte Data; // UnmanagedType.U1 seems to get rid of the 'incorrectly aligned or overlapped' error.

    now it errors out near this part
    struct /* olmSYSTEM_STATUS */ { // offset is 2 ok, I'll take your word for it
        unsigned char TimeDateValidFlag; // offset is still 2
        unsigned char AreaSecurityMode[8]; // offset is 3 but this is a char*, not a char so it's four bytes
        struct { // another struct here? if i give it offset 2 ...i get incorrectly aligned error // see the next line down
            unsigned char Status;
        } ExpansionEnclosure[8]; // a struct {} * - four bytes.  offset is 7 (3 + 4), next offset would be 11 (7 + 4)
    };

    [FieldOffset(2)][MarshalAs(UnmanagedType.U1, SizeConst=8)] public ExpansionEnclosure[] Enclosure;

    [StructLayout(LayoutKind.Sequential)]
            public struct ExpansionEnclosure
            {
                public byte Status;
            }
    tried giving it 3 since currentoffset(2)+1 byte inside ExpansionEnclosure struct, still get incorrectly aligned error.

  • User profile image
    andyandy

    Maybe you're using the wrong tool for the job?

    Could be easier to create a wrapper class with Managed C++ which you then can use from C#.

  • User profile image
    matrixr

    hmm, dont i still have to create the equivalent managed structure?

  • User profile image
    Jerrold

    C# makes me hate c++

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.