Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3603966
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:56:17+00:00 2026-05-18T20:56:17+00:00

I have a binary file with the following contents: The following code is used

  • 0

I have a binary file with the following contents:

alt text

The following code is used to read this contents in old VB6 program:

Private Type tpClient
    Firstname As String
    LastName As String
    Birth As String
    Adres As String
    Geslacht As String
    IDNummer As Long
    SSNummer As String
    DatabaseID As Long
    Telefoon1 As String
    Telefoon2 As String
End Type

Open strFilePath For Random Access Read As #intFileNumber
Get #intFileNumber, 1, ClientData ' ClientData is of type tpClient

Now I’m trying to read this with my new C# program:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct PatientStruct
{
    [MarshalAs(UnmanagedType.BStr)]
    public string FirstName;

    [MarshalAs(UnmanagedType.BStr)]
    public string LastName;

    [MarshalAs(UnmanagedType.BStr)]
    public string BirthDate;

    [MarshalAs(UnmanagedType.BStr)]
    public string Address;

    [MarshalAs(UnmanagedType.BStr)]
    public string Gender;

    [MarshalAs(UnmanagedType.BStr)]
    public string IdNumber;

    [MarshalAs(UnmanagedType.BStr)]
    public string SsNumber;

    [MarshalAs(UnmanagedType.BStr)]
    public string DatabaseId;

    [MarshalAs(UnmanagedType.BStr)]
    public string Telephone1;

    [MarshalAs(UnmanagedType.BStr)]
    public string Telephone2;
}

byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
T stuff = (PatientStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();

However, I get an AccessViolationException on the call to Marshal.PtrToStructure.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:56:18+00:00Added an answer on May 18, 2026 at 8:56 pm

    First of all, your struct should not be a struct at all, but a class. Structs are intended for small, immutable types that represent a single value.

    Making a data type marshal exactly as you want is really tricky, and as you are not doing interop you really don’t need marshalling at all. It’s easier to just use a BinaryReader to read the data from the file.

    The simple data types can be read straight off, and the strings can be read like this:

    string value = reader.ReadChars(reader.ReadShort());
    

    Specify an appropriate single byte encoding when opening the reader, for example windows-1252.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having following issue with reading binary file in C. I have read
I have the following code header(Content-Description: File Transfer); header('Content-Type: audio/mp3'); header(Content-Disposition: attachment; filename= .
I have a header file like this: /* * APP 180-2 ALG-254/258/772 implementation *
I'm trying to use the following code to create a zip file from a
I have some code that allows users to upload file attachments into a varbinary(max)
I am using the following tcl code to store a file from my deskstop
I have a binary file. There are 2288*2288 longitude float values stored in top
Assuming that I have to write to a binary file. I can use the
I have this data file, which is supposed to be a normal ASCII file.
I have a table with a 'Wav' column that is of type 'VARBINARY(max)' (storing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.