I am having a problem with marshalling a C character array. I have the following C# structure:
[StructLayout(LayoutKind.Explicit, Size = 16, CharSet = CharSet.Ansi), Serializable]
internal struct Header
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 4)]
[FieldOffset(0)]
public string header;
[FieldOffset(4)]
public int version;
[FieldOffset(8)]
public int diroffset;
[FieldOffset(12)]
public int direntries;
}
and the following code to read this structure from a stream:
public static T ReadStruct<T>(this Stream stream) where T : struct
{
var sz = Marshal.SizeOf(typeof(T));
var buffer = new byte[sz];
stream.Read(buffer, 0, sz);
var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var structure = (T) Marshal.PtrToStructure(
pinnedBuffer.AddrOfPinnedObject(), typeof(T));
pinnedBuffer.Free();
return structure;
}
Now my problem is that the header field misses a character after the struct is read. The file where the struct is read from contains the four bytes VPVP but after the struct has been read by ReadStruct the header string only contains VPV. If I take a look at the byte array in the read function in the debugger then that array contains the values 86, 80, 86, 80 which is VPVP. I also tried using LayoutKind.Sequential for the StructLayout but that didn’t change anything.
Am I doing something wrong or why is there a character missing in my string?
The problem you’re having lies in the struct definition, not in writing the bytes to it.
The problem lies right here:
As you’ve stated, you’re writing out the text
VPVP, which is 4 characters long, you’d think. This, however, is not the case. In C, you could declare the string as such:You need that null character (
\0) at the end, to mark off the end of the string. You need to take this into account when marshalling, because you need to reserve space for that “null terminator byte”, if you do not, the C# string will add it for you in the available memory, so it will eat away your last character. So if you’re gonna use a null-terminated string, you will have to make it of length 5.EDIT: Here is a better solution, where you don’t have to worry about null-terminators, you just use a
char[](and you also keep the magic 16 byte size):That way the
char[]is stored in memory, and you can access it as a string through the property, which doesn’t take in any memory of the struct itself.