Solved

How to use a binary file in C# that was created in C++ or Delphi ?

Posted on 2006-06-27
1
460 Views
Last Modified: 2008-02-01
Say I have a simple Delphi structure:

TestType = record
    first_name: array [0 .. 30] of Char;
    last_name: array [0 .. 30] of Char;
end;

Same strucure in C++:

struct TestType
{
    char first_name[31];
    char last_name[31];
};

Now then, I instantiated an object of that structure and saved it to a file, called "test.bin". I need to be able to read that file in C# 2.0. So I created the following C# structure:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
struct TestType
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]    
    public string first_name;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]    
    public string last_name;
}

Now, to read the file, I do the following:

FileStream testFile = null;
try
{
    BinaryFormatter binFormat = new BinaryFormatter();

    testFile = new FileStream("test.bin", FileMode.Open, FileAccess.Read);

    TestType testType = (TestType)binFormat.Deserialize(testFile);
}
finally
{
    if (testFile != null)
    {
        testFile.Close();
    }
}

When I do that, I get the following exception

"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."


Please help.
0
Comment
Question by:krupini
1 Comment
 
LVL 4

Accepted Solution

by:
fffej78 earned 500 total points
ID: 17003963
The Object serializer is for .NET serialised objects.  I think you just want to do a binary read of the strings from the file and put them in the object yourself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
conditional code and condition difference 9 77
abstract class with all non abstract mentods 6 76
MaxSpan challenge 9 80
scoreUp challenge 14 57
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now