托管cstring^ 转换为 std::string
来源:互联网 发布:彩虹秒赞源码7.8 编辑:程序博客网 时间:2024/05/22 09:00
#include <stdlib.h>
#include <vcclr.h>
#include <string>
using namespace System;
bool To_CharStar( String^ source, char*& target )
{
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
int len = (( source->Length+1) * 2);
target = new char[ len ];
return wcstombs( target, wch, len ) != -1;
}
bool To_string( String^ source, string &target )
{
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
int len = (( source->Length+1) * 2);
char *ch = new char[ len ];
bool result = wcstombs( ch, wch, len ) != -1;
target = ch;
delete ch;
return result;
}
- 托管cstring^ 转换为 std::string
- CString转换为std::string
- CString与std::string转换
- CString 转化为 std::string
- string转换为CString
- CString与std::string相互转换
- 实现CString到std::string的转换
- CString,std::string,char*之间转换
- 转换MFC CString 到std::string
- CString与std::string相互转换
- std::string 转换为std::wstring
- wchar_t,char,std::string,std::wstring,CString转换
- wchar_t,char,std::string,std::wstring,CString转换
- wchar_t char std::string std::wstring CString 转换
- [转]wchar_t char std::string std::wstring CString 转换
- [转]wchar_t char std::string std::wstring CString 转换
- CString,string转换为char*
- System::String转换为std::string
- asp.net 编码问题
- 测试人员的培养过程-2
- 51单片机之数码管显示
- C++ 虚函数、多态性的演示 -- 虚拟析构函数
- 安装SQL Server 2005 Performance Dashboard Reports的技巧
- 托管cstring^ 转换为 std::string
- PHP大赛
- MCS-51中断系统
- Spring配置iBatis多个SqlMapConfig.xml
- String.Format的多格式定义
- 一个清华学生留学香港后对人生的思考
- 51单片机的定时/计数器
- 破解博思特软件
- 荣耀与梦想
As a sidenote, this is not the first time I find myself wanting one or two features from CString in std::string, to make the decision for which of the two classes to use for a native Windows C++ application a bit easier...
Either way, I'm really looking forward to the new C++/CLI. Actually, the /clr switch works amazingly well even today.
void F( String^ s )
{
string s1 = marshal_to<string>( s );
String^ s2 = marshal_to<String^>( s1 );
}
If you already have a char*, and want to get a System::String from it, all you have to do is this:
void F( const char* s )
{
String^ s1 = gcnew String( s );
}
bool To_CharStar( String^ source, char*& target )
{
int len = (( source->Length+1) * 2);
target = new char[ len ];
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
return wcstombs( target, wch, len ) != -1;
}
bool To_string( String^ source, string &target )
{
int len = (( source->Length+1) * 2);
char *ch = new char[ len ];
bool result ;
{
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
result = wcstombs( ch, wch, len ) != -1;
}
target = ch;
delete ch;
return result;
}
delete ch;
actually be
delete[] ch;
Since you're allocating an array, you really should use the array deletion expression...