C++ cli 转换为C++/CLI用户定义对象(%)到本机C++;对象(&;)

C++ cli 转换为C++/CLI用户定义对象(%)到本机C++;对象(&;),c++-cli,pin-ptr,C++ Cli,Pin Ptr,如何将通过(%)传递的用户定义对象转换为本机对象(&)。比如说 void managed_func(user_defined_managed_obj% m_obj_) { // i need this obj_ptr->unmanaged_func(&m_obj_); } 因此,在上面的示例中,我希望将托管对象%m_obj\u传递给非托管函数的参数。这里UsReDealDeMyMaultEdgObj.Objc是C++中的.NET包装器,它是一个用于本地C++的桥梁

如何将通过(%)传递的用户定义对象转换为本机对象(&)。比如说

void managed_func(user_defined_managed_obj% m_obj_)
{
    // i need this
    obj_ptr->unmanaged_func(&m_obj_);
}
因此,在上面的示例中,我希望将托管对象
%m_obj\u
传递给
非托管函数的参数。这里UsReDealDeMyMaultEdgObj.Objc是C++中的.NET包装器,它是一个用于本地C++的桥梁。
我尝试了以下方法:

pin_ptr<user_defined_unmanaged_obj> tmp_ptr = &m_obj_;
obj_ptr->unmanaged_func(*tmp_ptr);
<本地> C++,本地上传数据,H./P>
#include "native_sql.h"
#include <string>
class native_upload_specific_data
{
public:
    native_upload_specific_data(string arugments) {...some codes}
    ~native_upload_specific_data(void)
    void upload(native_sql& sql_obj_) {...some codes}
}
#包括“native_sql.h”
#包括
类本机\u上载\u特定\u数据
{
公众:
本机\上传\特定\数据(字符串片段){…某些代码}
~native_上传_特定_数据(无效)
无效上载(本机sql和sql对象){…一些代码}
}
在C++/CLI中,sql_wrapper.h

#include <msclr\marshal_cppstd.h>
#include "../native_sql.h"
#include "../native_sql.cpp"
using namespace System;
public ref class sql_wrapper
{
public:
    sql_wrapper(String^ arguments) 
    {...codes to convert String^ to std::string and pass args to
    new native_sql}
    ~sql_wrapper(void) { delete sql_ptr; }
    void connect_wrapper(void) { sql_ptr->connect(); }
    void clean_wrapper(void) { sql_ptr->clean(); }

    native_sql* sql_ptr;
}
#include "../native_upload_specific_data.h"
#include "../native_upload_specific_data.cpp"
using namespace System;
public ref class upload_specific_data_wrapper
{
public:
    upload_specific_data_wrapper(String^ arguments)
    {...convert String^ args into std::strings and pass to 
    native_upload_specific_data ctor}
    ~upload_specific_data_wrapper(void) { delete data_ptr; }
    void upload(sql_wrapper% sql_obj_)
    {
        // here is where I have the problem
        pin_ptr<native_sql*> ptr = &(sql_obj_.sql_ptr);
        data_ptr->upload(ptr);
    }
    native_upload_specific_data* data_ptr;
}
#包括
#包括“./native_sql.h”
#包括“./native_sql.cpp”
使用名称空间系统;
公共引用类sql\U包装器
{
公众:
sql_包装器(字符串^arguments)
{…将字符串^转换为std::String并将参数传递给
新的本机sql}
~sql_包装器(void){删除sql_ptr;}
void connect_包装(void){sql_ptr->connect();}
void clean_包装(void){sql_ptr->clean();}
本机_sql*sql_ptr;
}
在C++/CLI wrapper中,上载特定的\u数据\u wrapper.h

#include <msclr\marshal_cppstd.h>
#include "../native_sql.h"
#include "../native_sql.cpp"
using namespace System;
public ref class sql_wrapper
{
public:
    sql_wrapper(String^ arguments) 
    {...codes to convert String^ to std::string and pass args to
    new native_sql}
    ~sql_wrapper(void) { delete sql_ptr; }
    void connect_wrapper(void) { sql_ptr->connect(); }
    void clean_wrapper(void) { sql_ptr->clean(); }

    native_sql* sql_ptr;
}
#include "../native_upload_specific_data.h"
#include "../native_upload_specific_data.cpp"
using namespace System;
public ref class upload_specific_data_wrapper
{
public:
    upload_specific_data_wrapper(String^ arguments)
    {...convert String^ args into std::strings and pass to 
    native_upload_specific_data ctor}
    ~upload_specific_data_wrapper(void) { delete data_ptr; }
    void upload(sql_wrapper% sql_obj_)
    {
        // here is where I have the problem
        pin_ptr<native_sql*> ptr = &(sql_obj_.sql_ptr);
        data_ptr->upload(ptr);
    }
    native_upload_specific_data* data_ptr;
}
#包括“./native_upload_specific_data.h”
#包括“./native\u upload\u specific\u data.cpp”
使用名称空间系统;
公共引用类上载\u特定\u数据\u包装器
{
公众:
上载特定的数据包装(字符串^arguments)
{…将字符串^args转换为std::strings并传递给
本机\u上载\u特定\u数据选择器}
~upload_specific_data_wrapper(void){delete data_ptr;}
无效上载(sql\u包装%sql\u对象)
{
//这就是我的问题所在
pin_ptr ptr=&(sql_obj_uj.sql_ptr);
数据上传(ptr);
}
本机上传特定数据*数据ptr;
}
我收到的错误是

C2664: 'void native_upload_specific_data(native_sql&)': cannot convert arugment 1 from cli::pin_ptr<native_sql*> to native_sql&
C2664:“无效本机\u上载\u特定的\u数据(本机\u sql&”):无法将片段1从cli::pin\u ptr转换为本机\u sql&

…谢谢。

您不能直接转换。当然,包装器可以公开它包装的用户定义的非托管对象。@HansPassant是的。我引用了,但在我的情况下没有用。嗯,那有点没用。发布包装器类的代码,我们不必看到每个方法。@HansPassant我已经发布了相关代码。谢谢