Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:05:25+00:00 2026-06-02T13:05:25+00:00

I’m currently working with a struct which is written into a buffer. When reading

  • 0

I’m currently working with a struct which is written into a buffer. When reading the struct back into my Application i get the following exception:

Unable to cast object of type ‘System.Windows.Documents.TextStore’ to type ‘System.Version’.

Code

The exception is thrown when casting the result back ( (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); ).

protected static T ReadStruct<T>(Stream input)
    where T : struct
{
    Byte[] buffer = new Byte[Marshal.SizeOf(typeof(T))];
    input.Read(buffer, 0, buffer.Length);
    GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return result;
}

Used data-structure

/// <summary>
/// The header for all binary files
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet=CharSet.Ansi)]
public unsafe struct BinaryHeader
{
    public const String MAGIC_KEY = "TABF";

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    String _Magic;
    Version _Version;
    DateTime _Timestamp;
    Guid _Format;
    Int64 _Reserved0;
    Int64 _Reserved1;
    /// <summary>
    /// The magic value for the test automation binary
    /// </summary>
    public String Magic
    {
        get
        {
            return _Magic;
        }
    }
    /// <summary>
    /// The version of the assembly ( used for debugging )
    /// </summary>
    public Version AssemblyVersion { get { return _Version; } }
    /// <summary>
    /// The formatGuid of the current binary
    /// </summary>
    public Guid FormatGuid { get { return _Format; } }
    /// <summary>
    /// The timestamp of the file
    /// </summary>
    public DateTime Timestamp { get { return _Timestamp; } }

    public BinaryHeader(Guid formatGuid)
    {
        _Reserved0 = 0;
        _Reserved1 = 0;

        _Version = BinaryBase.AssemblyVersion;
        _Format = formatGuid;
        _Timestamp = DateTime.Now;
        _Magic = MAGIC_KEY;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:05:27+00:00Added an answer on June 2, 2026 at 1:05 pm

    Cooking your own binary serialization scheme with this approach is okay but you have to understand the limitations. And a core one is that it can only work on blittable values, simple value type values. The pinvoke marshaller has a workaround for strings with the [MarshalAs] attribute. No such workaround is available for the Version class. The pointer value stored in the struct is going to get written to the file. When you read it back, the more normal mishap is that you’ll get an AccessViolationException. Unless you have the fortune that the pointer happens to dereference a valid object stored on the GC heap. An object of type TextStore in your case.

    That’s not exactly being lucky of course. You’ll have to re-think your approach, the _Version field has to go. Do not hesitate to use the BinaryFormatter class, this is exactly what it was made to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.