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 7432717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:30:09+00:00 2026-05-29T09:30:09+00:00

If I marshal this struct with StructureToPtr and then unmarshal it again with PtrToStructure

  • 0

If I marshal this struct with StructureToPtr and then unmarshal it again with PtrToStructure, my first node has y = {1,2} whilst my second node has y = {1,0}.

I’ve no idea why, perhaps my struct is bad somehow? Removing the bool from the struct makes it work.

using System;
using System.Runtime.InteropServices;

namespace csharp_test
{
    unsafe class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct Node
        {
            public bool boolVar;
            public fixed int y[2];
        }

        unsafe static void Main(string[] args)
        {
            Node node = new Node();

            node.y[0] = 1;
            node.y[1] = 2;
            node.boolVar = true;

            int size = sizeof(Node);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(node, ptr, false);
            Node node2 = (Node)Marshal.PtrToStructure(ptr, typeof(Node));
            Marshal.FreeHGlobal(ptr);
        }
    }
}
  • 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-05-29T09:30:10+00:00Added an answer on May 29, 2026 at 9:30 am

    This indeed goes wrong. It is the StructureToPtr() call that fails to copy enough bytes. You can see this by using Debug + Windows + Memory + Memory1 and putting “ptr” in the address box. Using the sizeof operator isn’t correct but not actually the source of the problem. Only the first element of the array is copied, regardless of the array length. Not sure what causes this problem, I never use fixed in pinvoke. I can only recommend the traditional pinvoke way which works fine:

    unsafe class Program {
        [StructLayout(LayoutKind.Sequential)]
        public struct Node {
            public bool boolVar;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public int[] y;
        }
    
        unsafe static void Main(string[] args) {
            Node node = new Node();
            node.y = new int[2];
    
            node.y[0] = 1;
            node.y[1] = 2;
            node.boolVar = true;
    
            int size = Marshal.SizeOf(node);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(node, ptr, false);
            Node node2 = (Node)Marshal.PtrToStructure(ptr, typeof(Node));
            Marshal.FreeHGlobal(ptr);
        }
    

    You can post to connect.microsoft.com if you want to bring this to the attention of the CLR interop masters.

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

Sidebar

Related Questions

I'm using Castor to marshal/unmarshal my Java objects, one of which contains an EnumMap.
I have an unmanaged struct I'd like to marshal to c sharp that looks
I have an unmanaged struct I'd like to marshal to c# that looks basically
Is it possible to marshal a C-style struct containing bit-fields to a C# struct,
I'm having some trouble with this code: //Creating a new ImageElement Struct ImageElement oElement
I tried to allocate an array of structs in this way: struct T {
I have some problems working with boolean types and marshalling this in a struct
I have a C++ struct that looks like this: struct unmanagedstruct { int flags;
I have a C struct that is defined in a way similar to this:
I'm trying to marshal a struct from a byte[]. Each part works except marshaling

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.