C++/CLI中托管-非托管字符串转换的通用方法
acmilan2016/07/04软件综合 IP:四川

C++/CLI的好处:

  1. 调用WinAPI方便
  2. 被原生编译为程序集,C#调用方便

缺点:

  1. 无法实现平台无关(Any CPU)
  2. 编译平台默认不能联动,需要自己在生成->配置管理器中调整
  3. 语法比较晦涩
  4. 对编译器版本要求比较高(托管运行库只能随.NET安装)

第一个方法是使用PtrToStringChars和pin_ptr

这个方式的好处:

  1. 使用简单
  2. 兼容VS2005

这个方式的缺点:

  1. 只能先转换为Unicode宽字符串
  2. 字符串是只读的

String^转换为const wchar_t *方法如下:

  1. 引用<vcclr.h>
  2. 使用PtrToStringChars函数获取String^内部字符串指针
  3. 赋值给pin_ptr<const wchar_t>钉住指针
  4. 将这个指针当作const wchar_t *使用即可

const wchar_t *转换为String^是最简单的,直接用String^构造函数就行了。

代码如下:

<code class="language-cpp">// stringclr.cpp: 主项目文件。

#include "stdafx.h"

#include <vcclr.h>
#include <windows.h>

#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")

using namespace System;

int main(array<system::string ^> ^args)
{
	
	String ^str = L"Hello World";

	// 获取String ^内部指针并钉住
    pin_ptr<const wchar_t> strptr = PtrToStringChars(L"Hello World");
	
	// 这个指针可作为const wchar_t *使用
	MessageBox(NULL, strptr, L"String from .NET", MB_OK);

	wchar_t buf[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, buf);

	// 直接用构造函数就可以将const wchar_t *转换为String ^
	String ^dir = gcnew String(buf);
	Console::WriteLine(dir);

    return 0;
}
</const></system::string></windows.h></vcclr.h></code>

另一种方式是使用Marshal::StringToHGlobalAnsi或Marshal::StringToHGlobalUni。

这种方法的好处:

  1. 可直接获得ANSI或Unicode字符串
  2. 字符串是可读写的

这种方法的坏处:

  1. 需要手动调用Marshal::FreeHGlobal释放内存,容易内存泄漏

使用这种方法要注意:不要使用Marshal::StringToHGlobalAuto,这是因为WinSDK是在编译时决定ANSI或Unicode的,而Marshal::StringToHGlobalAuto是在运行时,这样会导致出错,并且很难发现。

正确的做法是使用条件编译,如下所示:

<code class="language-cpp">#ifdef UNICODE
TCHAR *tstr = (TCHAR*)Marshal::StringToHGlobalUni(str).ToPointer();
#else
TCHAR *tstr = (TCHAR*)Marshal::StringToHGlobalAnsi(str).ToPointer();
#endif
</code>

2、使用后要使用Marshal::FreeHGlobal释放内存,如下所示:

<code class="language-cpp">Marshal::FreeHGlobal(IntPtr(tstr));
</code>

[修改于 7年11个月前 - 2016/07/05 00:29:31]

来自:计算机科学 / 软件综合
4
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
7年11个月前 修改于 7年11个月前 IP:四川
822279

看来最不方便的就是对编译器版本要求比较高了。。。

引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
7年11个月前 修改于 7年11个月前 IP:四川
822426

关于为什么VC++2010以后编译的C++/CLI不兼容.NET2.0/3.x:

VC++2005和VC++2008编译的是v2.0程序

VC++2010以后版本编译的是v4.0程序,与.NET2.0/3.x不兼容

C#兼容是因为.NET自带C#编译器,但是.NET不可能自带C++编译器

主楼说不可再分发,但是经过测试发现,VC++2010以上版本已经取消了托管运行库,C++/CLI程序是直接链接到非托管运行库上的,所以是个误解。

所以,C++/CLI可以使用,只是要注意3.5/4.0这个版本断层。

引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
7年11个月前 修改于 7年11个月前 IP:四川
822484

示例程序:

<code class="language-cpp">// marshaltest.cpp: 主项目文件。

#include "stdafx.h"

#include <vcclr.h>
#include <msclr marshal.h>
#include <msclr marshal_atl.h>
#include <string>
#include <stdlib.h>
#pragma comment(lib, "user32.lib")

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;

int main(array<system::string ^> ^args)
{
	String ^str = L"Hello, world!";
    
	Console::WriteLine(str);
	
	///////////////////////////////////////////////
	// VC++2005的字符串转换方法
	// vcclr.h
	// 通过pin_ptr<const wchar_t>固定托管内存,即可获得内部const wchar_t *指针
	// 再次用WideCharToMultiByte转换,可获得char *字符串
	// const char *(或const wchar_t *)转换到String ^赋值即可

	printf("[VS2005]\n");

	// String ^到const wchar_t *
	pin_ptr<const wchar_t> pinned_str = PtrToStringChars(str);
	MessageBox(NULL, pinned_str, L"pin_ptr<const wchar_t> & PtrToStringChars", MB_ICONINFORMATION);

	// const wchar_t *到char *
	if (int mbsize = WideCharToMultiByte(CP_ACP, 0, pinned_str, -1, NULL, 0, NULL, NULL))
	{
		char *mbbuf = (char *)malloc(mbsize);
		if (mbbuf)
		{
			if (WideCharToMultiByte(CP_ACP, 0, pinned_str, -1, mbbuf, mbsize, NULL, NULL))
			{
				printf("%s\n", mbbuf);
			}
			free(mbbuf);
		}
	}

	// const char *到String ^
	String ^stransi8 = "this is an ANSI string";
	Console::WriteLine(stransi8);
	
	// const wchar_t *到String ^
	String ^struni8 = L"this is a Unicode string";
	Console::WriteLine(struni8);

	///////////////////////////////////////////////
	// VC++2008新增的字符串转换方法
	// msclr/marshal.h
	// 使用marshal_context::marshal_as<const char *>实现String ^到const char *的转换
	// 使用marshal_context::marshal_as<const wchar_t *>实现String ^到const wchar_t *的转换
    // 使用marshal_as<string ^>的重载形式实现const char *到String ^的转换
    // 使用marshal_as<string ^>的重载形式实现const wchar_t *到String ^的转换

	// 除了这些以外,还定义了以下头文件
	// msclr/marshal_cppstd.h -- 支持string和wstring
	// msclr/marshal_atl.h -- 支持CStringA和CStringW
	// msclr/marshal_windows.h -- 支持BSTR和_bstr_t
	
	printf("[VS2008]\n");

	marshal_context mc; // 管理字符串生命周期的帮助类

	// String ^到const char *
	const char *mystrA = mc.marshal_as<const char *>(str);
	printf("%s\n", mystrA);
	
	// String ^到const wchar_t *
	const wchar_t *mystrW = mc.marshal_as<const wchar_t *>(str);
	MessageBox(NULL, mystrW, L"marshal_context::marshal_as<const wchar_t *>", MB_ICONINFORMATION);

	// const char *到String ^
	// 其实根本不用这么麻烦,直接赋值就可以了
	String ^stransi9 = marshal_as<string ^>("this is an ANSI string");
	Console::WriteLine(stransi9);
	
	// const char *到String ^
	// 其实根本不用这么麻烦,同上,直接赋值就可以了
	String ^struni9 = marshal_as<string ^>(L"this is a Unicode string");
	Console::WriteLine(struni9);

	///////////////////////////////////////////////
	// 使用System::Runtime::InteropServices::Marshal类的转换方法
	// 使用Marshal::StringToHGlobalAnsi实现从String ^到char *的转换方法
	// 使用Marshal::StringToHGlobalUni实现从String ^到wchar_t *的转换方法
	// 使用Marshal::PtrToStringAnsi实现从const char *到String ^的转换方法
	// 使用Marshal::PtrToStringUni实现从const wchar_t *到String ^的转换方法

	// 注意:
	// 最后要注意Marshal::FreeHGlobal释放内存(当然也可以用GlobalFree函数)
	// 不要使用StringToHGlobalAuto和PtrToStringAuto方法,因为C++不具备动态决定字符集功能

	// 除了这些以外,还支持两种非托管字符串类型:
	// 1、BSTR(COM自动化字符串,仅wchar_t *)
	// Marshal::StringToBSTR
	// Marshal::FreeBSTR
	// 2、COM任务内存块字符串
	// Marshal::StringToCoTaskMemAnsi
	// Marshal::StringToCoTaskMemUni
	// Marshal::FreeCoTaskMem

	printf("[.NET]\n");

	// String ^到char *
	char *ansi_str = (char *)Marshal::StringToHGlobalAnsi(str).ToPointer();
	printf("%s\n", ansi_str);
	Marshal::FreeHGlobal(IntPtr(ansi_str));

	// String ^到wchar_t *
	wchar_t *uni_str = (wchar_t *)Marshal::StringToHGlobalUni(str).ToPointer();
	MessageBox(NULL, uni_str, L"StringToHGlobalUni", MB_ICONINFORMATION);
	Marshal::FreeHGlobal(IntPtr(uni_str));

	// const char *到String ^
	// 其实根本不用这么麻烦,直接赋值就可以了
	String ^stransifx = Marshal::PtrToStringAnsi(IntPtr("this is an ANSI string"));
	Console::WriteLine(stransifx);

	// const char *到String ^
	// 其实根本不用这么麻烦,同上,直接赋值就可以了
	String ^strunifx = Marshal::PtrToStringUni(IntPtr(L"this is a Unicode string"));
	Console::WriteLine(strunifx);


    return 0;
}
</string></string></const></const></const></string></string></const></const></const></const></const></system::string></stdlib.h></string></msclr></msclr></vcclr.h></code>
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
7年11个月前 修改于 7年11个月前 IP:四川
822544
说明

新版Visual Studio会根据引用的程序集有没有32位来决定C#程序的执行位数,也就是说,如果Any CPU引用了32位程序集,那么就会按32位编译主程序。不过,为了兼容性起见,应该总是选择x86架构。

引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年4个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}