• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张纯睿
博客园    首页    新随笔    联系   管理    订阅  订阅

CLR 与 C++的常用类型转换笔记

1. System::String 转换到 const wchar_t*

const wchar_t* ToUnmanagedUnicode( System::String^ str )
{
pin_ptr<const WCHAR> nativeString1 = PtrToStringChars( str );
return (const wchar_t*)nativeString1;
}


2. const wchar_t* / const char* 转换到 System::String

const wchar_t* p= L"hello";
System::String( p ).ToString();

 

3. C++数值类型转CLR数值类型

int a;
System::Int32 b = System::Int32( a );


4. HWND 转为IWin32Window

public ref class WindowWrapper : System::Windows::Forms::IWin32Window
{

public: WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}

public:
property IntPtr Handle
{
virtual IntPtr get(void){return _hwnd;};
};

private: IntPtr _hwnd;
};

HWND nativehwnd;
IWin32Window^ w = gcnew Managed::WindowWrapper( System::IntPtr( nativehwnd ) );

posted @ 2012-09-26 11:00  张纯睿  阅读(712)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3