将C#struct传递给C++/C+的CLI+;包装纸 昨天发布一个问题后,我想我已经解决了这个问题,但我仍然有问题,我有C++类的C++/CLI包装器,C++类的一些函数以ReV作为参数缓冲区,包结构被定义为C++结构,这是一个参数。p> 在C++中,我使用StuttDebug复制了这些C++结构,这样我就有了C结构中的等价结构,它们在内存中与我的C++结构一样。在我的C++/CLI代码中,我尝试了以下操作 UINT GetValues(value class^ JPVals) // value class, as C# structs are value types { IntPtr ptr; Marshal::StructureToPtr(JPVals,ptr,false); return m_pComms->GetValues(ptr,0); // m_pComms is a wrapped unmanaged class //GetValues takes a pointer to a C++ struct } 我所得到的错误是不能将参数1从“St::ItpTR”转换成“SjpVal*”,为什么不可能将MaReLe从值类转换为C++结构指针?在这种情况下,我应该传递什么?我应该如何组织它

将C#struct传递给C++/C+的CLI+;包装纸 昨天发布一个问题后,我想我已经解决了这个问题,但我仍然有问题,我有C++类的C++/CLI包装器,C++类的一些函数以ReV作为参数缓冲区,包结构被定义为C++结构,这是一个参数。p> 在C++中,我使用StuttDebug复制了这些C++结构,这样我就有了C结构中的等价结构,它们在内存中与我的C++结构一样。在我的C++/CLI代码中,我尝试了以下操作 UINT GetValues(value class^ JPVals) // value class, as C# structs are value types { IntPtr ptr; Marshal::StructureToPtr(JPVals,ptr,false); return m_pComms->GetValues(ptr,0); // m_pComms is a wrapped unmanaged class //GetValues takes a pointer to a C++ struct } 我所得到的错误是不能将参数1从“St::ItpTR”转换成“SjpVal*”,为什么不可能将MaReLe从值类转换为C++结构指针?在这种情况下,我应该传递什么?我应该如何组织它,c#,c++,interop,c++-cli,C#,C++,Interop,C++ Cli,您没有获得序列化过程: // !! Note the % !! UINT GetValues(value class% JPVals) // value class, as C# structs are value types { // Allocate a buffer for serialization, pointer to NULL otherwise IntPtr ptr = Marshal::AllocHGlobal(Marshal::SizeOf(JPVals)

您没有获得序列化过程:

// !! Note the % !!
UINT GetValues(value class% JPVals) // value class, as C# structs are value types 
{ 
    // Allocate a buffer for serialization, pointer to NULL otherwise
    IntPtr ptr = Marshal::AllocHGlobal(Marshal::SizeOf(JPVals));

    try {
        // Serialize the managed object to "static" memory (not managed by the GC)
        Marshal::StructureToPtr(JPVals, ptr, false); 

        // Pass it to unmanaged code that will modify it.
        auto ret = m_pComms->GetValues(reinterpret_cast<SJPVal*>(ptr.ToPointer()), 0);

        // Copies the modifications back
        Marshal::PtrToStructure(ptr, JPVals);

        // Free resources
        Marshal::FreeHGlobal(ptr);

        return ret;
    } catch (...) {
        // Make sure we free the memory
        Marshal.FreeHGlobal(ptr);
        throw; 
    }
} 
/!!请注意%!!
UINT GetValues(值类%JPVals)//值类,因为C#结构是值类型
{ 
//为序列化分配缓冲区,否则指针指向NULL
IntPtr ptr=Marshal::AllocHGlobal(Marshal::SizeOf(JPVals));
试一试{
//将托管对象序列化为“静态”内存(不由GC管理)
Marshal::StructureToPtr(JPVals、ptr、false);
//将其传递给将修改它的非托管代码。
auto ret=m_pComms->GetValues(reinterpret_cast(ptr.ToPointer()),0);
//将修改内容复制回来
Marshal::ptr结构(ptr,JPVals);
//免费资源
元帅:自由全球(ptr);
返回ret;
}捕获(…){
//确保释放内存
弗里赫全球元帅(ptr);
投掷;
}
} 
编辑:显示了如何复制回该值

在使用C#
结构时
需要通过引用传递它,以确保将更改复制回来。或者,代码将与C#
的工作方式相同。第一步(
StructureToPtr
)现在可能没用了,因为在调用
GetValues
之前,您可能不关心其中的内容


顺便说一下,你的命名习惯有点不好。在C++中,你应该用一个大写字母来写<强> >不>强> .< /p> < p>你没有得到序列化过程:

// !! Note the % !!
UINT GetValues(value class% JPVals) // value class, as C# structs are value types 
{ 
    // Allocate a buffer for serialization, pointer to NULL otherwise
    IntPtr ptr = Marshal::AllocHGlobal(Marshal::SizeOf(JPVals));

    try {
        // Serialize the managed object to "static" memory (not managed by the GC)
        Marshal::StructureToPtr(JPVals, ptr, false); 

        // Pass it to unmanaged code that will modify it.
        auto ret = m_pComms->GetValues(reinterpret_cast<SJPVal*>(ptr.ToPointer()), 0);

        // Copies the modifications back
        Marshal::PtrToStructure(ptr, JPVals);

        // Free resources
        Marshal::FreeHGlobal(ptr);

        return ret;
    } catch (...) {
        // Make sure we free the memory
        Marshal.FreeHGlobal(ptr);
        throw; 
    }
} 
/!!请注意%!!
UINT GetValues(值类%JPVals)//值类,因为C#结构是值类型
{ 
//为序列化分配缓冲区,否则指针指向NULL
IntPtr ptr=Marshal::AllocHGlobal(Marshal::SizeOf(JPVals));
试一试{
//将托管对象序列化为“静态”内存(不由GC管理)
Marshal::StructureToPtr(JPVals、ptr、false);
//将其传递给将修改它的非托管代码。
auto ret=m_pComms->GetValues(reinterpret_cast(ptr.ToPointer()),0);
//将修改内容复制回来
Marshal::ptr结构(ptr,JPVals);
//免费资源
元帅:自由全球(ptr);
返回ret;
}捕获(…){
//确保释放内存
弗里赫全球元帅(ptr);
投掷;
}
} 
编辑:显示了如何复制回该值

在使用C#
结构时
需要通过引用传递它,以确保将更改复制回来。或者,代码将与C#
的工作方式相同。第一步(
StructureToPtr
)现在可能没用了,因为在调用
GetValues
之前,您可能不关心其中的内容


顺便说一下,你的命名习惯有点不好。你应该在C++中用大写字母写<强> >不>强。

你应该接受昨天的问题的答案。然后你应该接受昨天的问题的答案。愚蠢的错误:我的意思是不要用大写字母来开始可变的名字,对不起,我已经编辑了答案。我还有一个问题,传入的结构是调用winsock recv()的缓冲区,在调用函数中,用户需要能够传入结构,然后在函数调用后,结构将被数据填充,因为IntPtr在函数中分配了内存,现在还会发生这种情况吗,IntPtr可以指向与传入的结构相同的内存吗?不可以,但可以使用反向函数
Marshal::PtrToStructure
将其复制回来。我将编辑并向您展示如何。请注意参数列表中的 %。“第一步(StutoTopTR)可能是无用的,现在……”如果我不再做结构TopTr.愚蠢的错误,我如何把它传递给C++函数:我的意思是不要用大写字母来开始变量名,对不起,我已经编辑了答案。我还有一个问题,实际上,传入的结构是调用winsock recv()的缓冲区,在调用函数中,用户需要能够传入结构,然后在函数调用后,结构将填充数据,因为IntPtr在函数中分配了内存,现在还会发生这种情况吗,IntPtr可以指向与传入的结构相同的内存吗?不可以,但可以使用反向函数
Marshal::PtrToStructure
将其复制回来。我会编辑并展示给你看。请在参数列表中注意<代码> %。“第一步(StutoStRTR)可能是无用的,现在…………如果我不再做StuttoTopr,我怎么把它传递给C++函数?”