reading binary file to struct

  • Thread starter Thread starter Derrick
  • Start date Start date
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);
 
Hi Derrick,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that your deserialization code could not
deserialize a struct properly. If there is any misunderstanding, please
feel free to let me know.

I think there might be something wrong with the size you have allocated for
the struct. You only allocated 3 chars for the first string member.
However, "Sai" actually consists of 4 chars. The system will add a '\0' at
the end of it automatically to indicate that the string is ended. So Would
you try to increase the SizeConst for each member?

[StructLayout(LayoutKind.Sequential)]
struct PlayerIndexRec
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)] 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;
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks for the reply

I tried that prior to posting ie.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)] public string sTmCode;
changed to this
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)] public string sTmCode;

The problem with that is that the struct is a different size than the data
in the file - so when I read the data, it goes out of alignment.

I have been able to read the file using VB6.0 with the following Type:
Type PlayerIndexRec
sTmCode As String * 3
sNull1 As String * 2
sPos As String * 2
sNull2 As String * 3
End Type

I found this text in help 'The behavior in managed code differs from the
Microsoft Visual Basic 6.0 behavior, which is not null terminated (for
example, MyString As String * 5).' Could I be using the wrong
UnmanagedType?

Derrick
 
Hi Derrick,

Since Strings in VB6 are fixed length, when converting to .NET apps, we
have to add VBFixedString(length) in the attribute. Please try to use the
following in VB.NET and deserialize it agiain. I haven't found an
equivalance in C#.

Structure PlayerIndexRec

<VBFixedString(3),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=3)> Public sTmCode As
String

<VBFixedString(2),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=2)> Public sNull1 As String

<VBFixedString(2),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=2)> Public sPos As String

<VBFixedString(3),System.Runtime.InteropServices.MarshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=3)> Public sNull2 As String
End Structure

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks for the tip.

I converted the code to VB and tested, but am still getting the same
problem.

Following is code snippets - hopefully you can see something I am doing
wrong...
<struct>
Structure PlayerIndexRec
<VBFixedString(3),
MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=3)> Public sTmCode As String
<VBFixedString(2),
MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=2)> Public sNull1 As String
<VBFixedString(2),
MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=2)> Public sPos As String
<VBFixedString(3),
MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=3)> Public sNull2 As String
End Structure

<Deserialize function>
Private Function RawDeserialize(ByVal rawdatas As Byte(), ByVal anytype As
Type) As Object
Dim rawsize As Integer = Marshal.SizeOf(anytype)
If rawsize > rawdatas.Length Then
Return Nothing
End If
Dim buffer As IntPtr = Marshal.AllocHGlobal(rawsize)
Marshal.Copy(rawdatas, 0, buffer, rawsize)
Dim retobj As Object = Marshal.PtrToStructure(buffer, anytype)
Marshal.FreeHGlobal(buffer)
Return retobj
End Function

<code to read data to struct>
With br
Do While .PeekChar <> -1
Dim pir As New PlayerIndexRec
buf = br.ReadBytes(Marshal.SizeOf(pir))
pir = CType(RawDeserialize(buf, pir.GetType()), PlayerIndexRec)
Console.WriteLine("tmCode: '{0}', sNull1: '{2}', sPos: '{1}', sNull2:
'{3}'", _
pir.sTmCode, _
pir.sPos, _
pir.sNull1, _
pir.sNull2)
Loop
br.Close()
End With
 
Hi Derrick,

Could you also show me the code in VB6 that serializes the object?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top