将托管String转换为标准C++string

/************************************************************************/
/*
 * @brief   将托管String转换为标准C++string
 */
/************************************************************************/
ref class GCStringToStdString
{
public:
    //************************************
    // Method:    operator()
    // FullName:  GCStringToStdString::operator()
    // Access:    public
    // Returns:   std::string & 转换后的标准C++string
    // Qualifier:
    // Parameter: System::String ^ gcstr 托管字符串
    // Parameter: std::string & ret 接收标准C++字符串
    // @brief     执行转换的运算符重载
    //************************************
    std::string & operator() (System::String ^ gcstr, std::string & ret)
    {
        char * p = (char *)(int)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(gcstr);
        ret = p;
        System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)p);
        return ret;
    }

    static GCStringToStdString s_globalInstance; //! 公开全局实例
};

//使用方法
ev.ori_message_  = GCStringToStdString::s_globalInstance(str_clr, std_str).c_str();

 

将托管String转换为标准C++string

posted @ 2013-09-25 09:50  calabashdad  阅读(595)  评论(0编辑  收藏  举报