kamal
kamal

Reputation: 759

Marshalling datatype issue

Below is the code which I am trying to convert from c++ into c#

struct PSB {
   short    type_of_psb;
   short    call_dependent;
   int32    del_psb_status;
   uint32   seq_number[2];
   int32    uma_psb_status;
   short    psb_reserved[6];
} 

Previously, I wrote below structure.

        [StructLayout(LayoutKind.Explicit)]
        public struct PSB
        {
            [FieldOffset(0)]
            public short type_of_psb;
            [FieldOffset(2)]
            public short call_dependent;
            [FieldOffset(4)]
            public int del_psb_status;
            [FieldOffset(8)]
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public uint32 seq_number; // Here is the problem
            [FieldOffset(16)]
            public int uma_psb_status;
            [FieldOffset(20)]
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public short[] psb_reserved;
        }

but above c# code does not work, problem is in member seq_number

So i changed it into

public ulong seq_number from uint seq_number, and I removed above marshalas attribute.

Now it is working with legacy code. I don't know why ? What is the problem if I defined like as array ?

Upvotes: 0

Views: 65

Answers (1)

goric
goric

Reputation: 11915

Your C++ struct and your MarshalAs attribute both listed the type as an array, but your C# struct was only declaring a number. I'm not sure exactly what error you were getting, but it looks like you were trying to convert an array of two numbers into a single number, which obviously would cause some issues.

Try keeping MarshalAs attribute as-is in your posted struct and simply replace public uint32 seq_number; with public uint32[] seq_number;.

Upvotes: 1

Related Questions