473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

reading binary file to struct

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);

Nov 16 '05 #1
5 7043
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."

Nov 16 '05 #2
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
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:Aq**************@cpmsftngxa10.phx.gbl...
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."

Nov 16 '05 #3
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.M arshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=3 )> Public sTmCode As
String

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

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

<VBFixedString(3),System.Runtime.InteropServices.M arshalAs(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."

Nov 16 '05 #4
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.Unmanaged Type.ByValTStr,
SizeConst:=3)> Public sTmCode As String
<VBFixedString(2),
MarshalAs(System.Runtime.InteropServices.Unmanaged Type.ByValTStr,
SizeConst:=2)> Public sNull1 As String
<VBFixedString(2),
MarshalAs(System.Runtime.InteropServices.Unmanaged Type.ByValTStr,
SizeConst:=2)> Public sPos As String
<VBFixedString(3),
MarshalAs(System.Runtime.InteropServices.Unmanaged Type.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

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:f9*************@cpmsftngxa10.phx.gbl...
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.M arshalAs(System.Runtime.In
teropServices.UnmanagedType.ByValTStr,SizeConst:=3 )> Public sTmCode As
String

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

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

<VBFixedString(3),System.Runtime.InteropServices.M arshalAs(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."

Nov 16 '05 #5
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."

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Derrick | last post by:
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,'...
3
by: poifull | last post by:
Hi All, What is the proper way to read a binary file into a byte? I am using BinaryReader to read from a Stream and call the ReadByte method of the BinaryReader object. The method I'm using...
6
by: al jones | last post by:
I picked a good project to try to learn to use VS - and up till now everything has worked the way I expect. The code is from a VS.net class, which I picked up on the web, from which I've extracted...
1
by: RML | last post by:
Hi, My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++ WinCE app. The VC++ app writes the following structure to the file. typedef struct { short ID; TCHAR Num; }...
8
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
3
by: acp26b | last post by:
I need to read a binary to put into a struct. For some reason the code will not work. In this section of code i am just looping through the file to get the number of records so i can then build...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
1
by: ziopulcher | last post by:
Good morning. I need some help in order to load a binary file. I know that the first 3200 bytes of this file are a "text" header (40 rows x 80 columns). I tried to read them by using the "get"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.