本文介绍了Marshal.StructureToPtr失败,布尔和固定大小的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用 PtrToStructure 再次名帅这个结构与 StructureToPtr ,然后解组吧,我的第一个节点具有Y = {1,2},而我的第二个节点Y = {1,0}。



我不知道为什么,也许是我的结构是坏不知何故?删除布尔从结构使得它的工作。



 使用系统。使用System.Runtime.InteropServices 
;

命名空间csharp_test
{
不安全类节目
{
[StructLayout(LayoutKind.Sequential)]
公共结构节点
{
公共BOOL boolVar;
公共固定INT Y [2];
}

不安全的静态无效的主要(字串[] args)
{
节点node =新节点();

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

INT大小= sizeof运算(节点);
IntPtr的PTR = Marshal.AllocHGlobal(大小);
Marshal.StructureToPtr(节点,PTR,FALSE);
节点节点2 =(节点)Marshal.PtrToStructure(PTR的typeof(节点));
Marshal.FreeHGlobal(PTR);
}
}
}


解决方案

这的确是出了问题。它是StructureToPtr()调用无法复制足够的字节。您可以通过使用调试+的Windows +内存+ MEMORY1并把PTR在地址栏中看到这一点。使用sizeof操作符,但并非问题的根源。仅数组的第一元素被复制,而不考虑所述阵列的长度。不知道是什么原因导致这个问题,我从来没有在使用的PInvoke的固定的。我只能推荐传统的PInvoke方法的正常工作:

 不安全类节目{
[StructLayout(LayoutKind.Sequential )
公共结构节点{
公共BOOL boolVar;
[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 2)]
公众诠释[]ÿ;
}

不安全的静态无效的主要(字串[] args){
节点node =新节点();
node.y =新INT [2];

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

INT大小= Marshal.SizeOf(节点);
IntPtr的PTR = Marshal.AllocHGlobal(大小);
Marshal.StructureToPtr(节点,PTR,FALSE);
节点节点2 =(节点)Marshal.PtrToStructure(PTR的typeof(节点));
Marshal.FreeHGlobal(PTR);
}

您可以张贴到connect.microsoft.com如果你想带这CLR的互操作高手的注意。


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);
        }
    }
}
解决方案

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.

这篇关于Marshal.StructureToPtr失败,布尔和固定大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:47