Solved

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

Posted on 2006-06-27
1
462 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 144
word0 challenge 3 91
allswap challenge 6 98
rhino JavaScript import, load 25 84
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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