?
Solved

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

Posted on 2006-06-27
1
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
fffej78 earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 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