C# 编组时的大数组

C# 编组时的大数组,c#,marshalling,C#,Marshalling,我在.dll文件中使用一个c函数,该函数引用struct数组并返回一个字节,该字节决定我的操作是否成功。 它在c项目中运行良好 当我发送一个小尺寸的arrayup到7个元素时,效果很好 然后是假的 该数组来自以下结构 [StructLayout(LayoutKind.Sequential)] public struct MainStruct { [MarshalAsAttribute(UnmanagedType.Struct, SizeConst = 5)

我在.dll文件中使用一个c函数,该函数引用struct数组并返回一个字节,该字节决定我的操作是否成功。 它在c项目中运行良好 当我发送一个小尺寸的arrayup到7个元素时,效果很好 然后是假的

该数组来自以下结构

    [StructLayout(LayoutKind.Sequential)]
    public struct MainStruct
    {
        [MarshalAsAttribute(UnmanagedType.Struct, SizeConst = 5)]
        public Struct2 Struct2Object;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem1;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem2;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem3;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem4;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem5;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem6;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem7;

        [MarshalAsAttribute(UnmanagedType.U8, SizeConst = 1)]
        public UInt64 Elem8;

    };

似乎在结构中硬编码了8个字节。当然,如果超过这个限制,你会得到内存损坏

如果要发送的字节数可能不受限制,我会使用Marshal.AllocHGlobal分配一个未管理的缓冲区,并将其传递给非托管函数

    __declspec(dllexport) TU08 GetBuffer(MainStruct *OBJ)
    {
    TU64 SelectedOBIS;
    TU08 XdrBuffer[Dlms_mXdrMaxBuffer];
    TU08 *pXdrBuffer;
    TU32 i;
    TU32 NumberOfElements;

    pXdrBuffer = XdrBuffer;
    SelectedOBIS = ProfilesData[LOG_ID_1].ProfilesOBISCodes;

        pXdrBuffer = XdrBuffer;
        Dlms_gXdrInitBuffer();
        /////// Get Data ///////
        if(Main_iSendGetRequest(SelectedOBIS, 7, 2, XdrBuffer) == false)
            return 0;
        /////// Extract Data ///////
        if(*XdrBuffer == Dlms_mXdrArray)
        {
            NumberOfElements = *(pXdrBuffer + 1);
            pXdrBuffer += 2; // 2 for array tag and its number of fields 
            for(i=0; i<NumberOfElements; i++)
            {
                if(*pXdrBuffer == Dlms_mXdrStruct)
                {
    pXdrBuffer += 2; // 2 for struct tag and its number of fields 
    pXdrBuffer = gGetDateTime(pXdrBuffer, &OBJ[i].TimeStamp);
    pXdrBuffer = gGetLongUnsigned(pXdrBuffer, &OBJ[i].StatusRegister);
    pXdrBuffer = gGetUnsigned(pXdrBuffer, &OBJ[i].EventCode);
    }
            }
        }
    return 1;
}
函数的封送处理是

    [DllImport("dll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern byte GetBuffer([In, Out] MainStruct[] Profile);

关键是,如果传递给函数的数组大小为7或更小,它在C中运行良好,在windows窗体应用程序中也运行良好

我尝试使用指针来获取数据:。。 到目前为止,一切正常

MainStruct OBJ = (MainStruct)Marshal.PtrToStructure(pointertostruct, typeof(MainStruct));

指针获取数据,但它只是用零初始化对象

你的问题中没有足够的信息。函数的本机定义是什么样子的?你是如何在C中定义它的?您能否调试本机函数以了解其返回false的原因?此外,如果您希望使用嵌入数组封送结构,可以在数组上使用UnmanagedType.ByValArray,而不是多个字段。另一个问题:本机函数如何发现数组有多大?您必须停止使用SizeConst。我想不出UInt64只有1个字节的情况。请丢失[MarshalAsAttributeUnmanagedType.U8,SizeConst=1]。考虑到本机的定义,这是错误的。我看不出还有什么不对的,珀斯.我已经试过你说的了。。MainStruct OBJ=MainstructureMarshall.ptrStructurePointerTostruct,MainStruct的类型;指针包含数据,但它只是用零初始化对象!!您知道您没有提供足够的信息来帮助您吗?我知道:以前显示的结构。。这个问题。现在我有一个指向数据的指针,无法将其转换为结构
MainStruct OBJ = (MainStruct)Marshal.PtrToStructure(pointertostruct, typeof(MainStruct));