使用和扩展marshal_as库

一.简介

    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); } }; } }

转载于:https://www.cnblogs.com/neking/archive/2011/06/23/2428169.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值