一.简介
marshal_as库是一个托管与非托管代码间数据类型的转换模板库.
非托管即本地 C++或C代码
二.基本用法
1.非托管到托管的字符串转换
String^ clrString; const char* pcszHello = "hello world"; clrString = marshal_as<String^>(pcszHello); wchar_t* pwszHello = L"hello wide world"; clrString = marshal_as<String^>(pwszHello); bstr_t bstrtHello("hello bstr_t world"); clrString = marshal_as<String^>(bstrtHello); std::string stdHello = "hello from std::string"; clrString = marshal_as<String^>(stdHello); CString mfcString("hello from CString"); clrString = marshal_as<String^>(mfcString); CComBSTR atrBSTR(L"hello from CComBSTR"); clrString = marshal_as<String^>(atrBST
2.托管到非非托管的字符串转换
String^ clrString = "Original System::String"; std::string stdHello = marshal_as<std::string>(clrString); CString mfcString = marshal_as<CString>(clrString); CComBSTR atrBSTR = marshal_as<CComBSTR>(clrString);
不能直接使用marshal_as转化String^类型到const char*, const wchar_t * 或者 BSTR,因为那些不受托管的资源需要在使用后被释放。为了实现这些,你需要使用一个context对象如下表所列。
marshal_context context; const char* pcszHello = context.marshal_as<const char*>(clrString); const wchar_t* pcwszHello = context.marshal_as<const wchar_t*>(clrString); BSTR bstrString = context.marshal_as<BSTR>(clrString); Console::WriteLine(context._clean_up_list.Count);
三.扩展用法
1.扩展函数, 举例:
System::Drawing::Rectangle, RECT, CRect
namespace msclr { namespace interop { template<> System::Drawing::Rectangle marshal_as<System::Drawing::Rectangle, RECT> ( const RECT& from) { return System::Drawing::Rectangle(from.left, from.top, from.right - from.left, from.bottom - from.top); } template<> System::Drawing::Rectangle marshal_as< System::Drawing::Rectangle, CRect> ( const CRect& from) { return System::Drawing::Rectangle(from.left, from.top, from.Width(), from.Height()); } template<> RECT marshal_as<RECT, System::Drawing::Rectangle>( const System::Drawing::Rectangle& from) { System::Drawing::Rectangle rectangle = from; //remove const RECT rect = {rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom}; return rect; } template<> CRect marshal_as<CRect, System::Drawing::Rectangle>( const System::Drawing::Rectangle& from) { System::Drawing::Rectangle rectangle = from; //remove const return CRect (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom); } } }
2.扩展objects需要使用context, 举例:
namespace msclr { namespace interop { template<> ref class context_node<System::Drawing::Font^, HFONT> : public context_node_base { private: System::Drawing::Font^ _font; public: context_node(System::Drawing::Font^% to, HFONT from) { to = _font = System::Drawing::Font::FromHfont((IntPtr)from); } ~context_node() { this->!context_node(); } protected: !context_node() { delete _font; } }; template<> ref class context_node<HFONT, System::Drawing::Font^> : public context_node_base { private: HFONT _hFont; public: context_node(HFONT& to, System::Drawing::Font^ from) { to = _hFont = (HFONT)from->ToHfont().ToPointer(); } ~context_node() { this->!context_node(); } protected: !context_node() { DeleteObject(_hFont); } }; } }