D
Derrick
I am trying to read a binary file into a struct, but am having trouble
getting all the data.
the struct and a snip of the code follows at the end of the message.
Expected results: 'Sai,,LW,'
Actual results: 'i,,L,'
It appears that only the last letter of the sTmCode is assigned in the
struct.
It appears that only the first letter of the sPos is assisgned in the
struct.
I am sure I am missing something little, but can't see it. any ideas?
Thanks
Derrick
Struct:
[StructLayout(LayoutKind.Sequential)]
struct PlayerIndexRec
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string sTmCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public string sNull1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public string sPos;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string sNull2;
}
Code:
PlayerIndexRec pir = new PlayerIndexRec();
buf = br.ReadBytes(Marshal.SizeOf(pir));
IntPtr buffer = Marshal.AllocHGlobal( Marshal.SizeOf(pir.GetType() ));
Marshal.Copy( buf, 0, buffer, Marshal.SizeOf(pir) );
pir = (PlayerIndexRec)Marshal.PtrToStructure(buffer, pir.GetType() );
Marshal.FreeHGlobal( buffer );
Console.WriteLine("{0},{1},{2},{3}",
pir.sTmCode,
pir.sNull1,
pir.sPos,
pir.sNull2);
getting all the data.
the struct and a snip of the code follows at the end of the message.
Expected results: 'Sai,,LW,'
Actual results: 'i,,L,'
It appears that only the last letter of the sTmCode is assigned in the
struct.
It appears that only the first letter of the sPos is assisgned in the
struct.
I am sure I am missing something little, but can't see it. any ideas?
Thanks
Derrick
Struct:
[StructLayout(LayoutKind.Sequential)]
struct PlayerIndexRec
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string sTmCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public string sNull1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public string sPos;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string sNull2;
}
Code:
PlayerIndexRec pir = new PlayerIndexRec();
buf = br.ReadBytes(Marshal.SizeOf(pir));
IntPtr buffer = Marshal.AllocHGlobal( Marshal.SizeOf(pir.GetType() ));
Marshal.Copy( buf, 0, buffer, Marshal.SizeOf(pir) );
pir = (PlayerIndexRec)Marshal.PtrToStructure(buffer, pir.GetType() );
Marshal.FreeHGlobal( buffer );
Console.WriteLine("{0},{1},{2},{3}",
pir.sTmCode,
pir.sNull1,
pir.sPos,
pir.sNull2);