Home All Groups Group Topic Archive Search About

Reading a binary file

Author
8 May 2006 3:19 PM
RML
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[10];
} PARTS_STRUCT;

In my VB code I read the file with this code...

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
    Public Structure Parts_Struct
        Public ID As Short
        <MarshalAs(UnmanagedType.TBStr, SizeConst:=10)> _
        Public Num As String
    End Structure

        Dim Parts As New Parts_Struct
        Dim fs As New FileStream(FName, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fs)
        Parts.ID = br.ReadInt16()
        Parts.Num = br.ReadChars(20)

When I run the code, the ID field is Ok, but I only get the 1st letter that
was saved in the "Num" field.

RML

Author
8 May 2006 4:20 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"RML" <R**@discussions.microsoft.com> schrieb:
> 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[10];
> } PARTS_STRUCT;
>
> In my VB code I read the file with this code...
>
>    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
>    Public Structure Parts_Struct
>        Public ID As Short
>        <MarshalAs(UnmanagedType.TBStr, SizeConst:=10)> _
>        Public Num As String

Try changing 'TBStr' to 'UnmanagedType.ByValTStr'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 May 2006 5:01 PM
RML
Thanks.  I did try that and still get the same results.


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "RML" <R**@discussions.microsoft.com> schrieb:
> > 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[10];
> > } PARTS_STRUCT;
> >
> > In my VB code I read the file with this code...
> >
> >    <StructLayout(LayoutKind.Sequential, Pack:=1)> _
> >    Public Structure Parts_Struct
> >        Public ID As Short
> >        <MarshalAs(UnmanagedType.TBStr, SizeConst:=10)> _
> >        Public Num As String
>
> Try changing 'TBStr' to 'UnmanagedType.ByValTStr'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>