Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非托管结构到托管结构_C#_Marshalling - Fatal编程技术网

C# 非托管结构到托管结构

C# 非托管结构到托管结构,c#,marshalling,C#,Marshalling,我在C#中接收到来自C++库回调的结构指针 C++结构(包装为1): 在C#中,我有一个等价物: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable] public struct SomeStruct { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public int[] data1 [Ma

我在
C#
中接收到来自
C++
库回调的结构指针

C++
结构(包装为1):

C#
中,我有一个等价物:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
public struct SomeStruct
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public int[] data1

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public int [] data2;

    public int data3;
    public int data4;
};
var数据包含右
IntPtr

SomeStruct ss = (SomeStruct )Marshal.PtrToStructure(pointer, typeof(SomeStruct));
但我收到了垃圾,我做错了什么


谢谢

下面是一个最简单可行的例子,使用VC++

您可能需要检查以下各项:

  • 代码中的所有内容都正确吗
  • 你使用的不是VC++吗?
    • 如果是这样,那么检查该编译器的打包和对齐行为
导出符号的Win32 DLL项目:

struct test
{
    int data1[8];
    int data2[8];
    int data3;
    int data4;
};

EXTERN_C MYDLL_API test* Test()
{
    test* pTest = new test;
    for (int i = 0; i < 8; i++)
    {
        pTest->data1[i] = i;
        pTest->data2[i] = i;
    }
    pTest->data3 = 1234;
    pTest->data4 = 5678;
    return pTest;
}
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var intPtr = Test();
            var ptrToStructure = Marshal.PtrToStructure<Test1>(intPtr);
        }

        [DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Test();
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct Test1
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data1;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data2;

        public readonly int data3;
        public readonly int data4;
    }
}
struct测试
{
int data1[8];
int data2[8];
int数据3;
int data4;
};
EXTERN_C MYDLL_API测试*测试()
{
测试*pTest=新测试;
对于(int i=0;i<8;i++)
{
pTest->data1[i]=i;
pTest->data2[i]=i;
}
pTest->data3=1234;
pTest->data4=5678;
返回pTest;
}
C#测试程序:

struct test
{
    int data1[8];
    int data2[8];
    int data3;
    int data4;
};

EXTERN_C MYDLL_API test* Test()
{
    test* pTest = new test;
    for (int i = 0; i < 8; i++)
    {
        pTest->data1[i] = i;
        pTest->data2[i] = i;
    }
    pTest->data3 = 1234;
    pTest->data4 = 5678;
    return pTest;
}
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var intPtr = Test();
            var ptrToStructure = Marshal.PtrToStructure<Test1>(intPtr);
        }

        [DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Test();
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct Test1
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data1;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data2;

        public readonly int data3;
        public readonly int data4;
    }
}
使用系统;
使用System.Runtime.InteropServices;
命名空间控制台应用程序1
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
var intPtr=Test();
var ptrToStructure=Marshal.ptrToStructure(intPtr);
}
[DllImport(“MyDLL.dll”,CallingConvention=CallingConvention.Cdecl)]
公共静态外部IntPtr测试();
}
[StructLayout(LayoutKind.Sequential)]
内部结构测试1
{
[Marshallas(UnmanagedType.ByValArray,SizeConst=8)]
公共只读int[]数据1;
[Marshallas(UnmanagedType.ByValArray,SizeConst=8)]
公共只读int[]数据2;
公共只读int数据3;
公共只读int数据4;
}
}
结果:

struct test
{
    int data1[8];
    int data2[8];
    int data3;
    int data4;
};

EXTERN_C MYDLL_API test* Test()
{
    test* pTest = new test;
    for (int i = 0; i < 8; i++)
    {
        pTest->data1[i] = i;
        pTest->data2[i] = i;
    }
    pTest->data3 = 1234;
    pTest->data4 = 5678;
    return pTest;
}
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var intPtr = Test();
            var ptrToStructure = Marshal.PtrToStructure<Test1>(intPtr);
        }

        [DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Test();
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct Test1
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data1;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public readonly int[] data2;

        public readonly int data3;
        public readonly int data4;
    }
}

到目前为止看起来不错,包括更多显示如何获得
指针的代码。
仍然看不到我的代码中的问题,无论如何,确认该方法是正确的很好,因此非常感谢,我将深入检查,现在我确信这是一些细节。@EduardoLista试着关注代码的不同之处-例如,使用
typedef
而不是
struct
,或者传递给
StructLayoutAttribute
Pack=1
参数。