I have a binary file with the following contents:
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?