Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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