std :: string< - >系统::字符串* [英] std::string <--> System::String*

查看:58
本文介绍了std :: string< - >系统::字符串*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在努力清理一些我继承的代码,并且

想知道我的功能是否有问题。我相当擅长标准C ++,但我对.NET托管C ++很陌生。

似乎工作正常,但是每个人都知道有错误的程序可以

似乎仍然工作正常。 :)


我正在使用VS .NET 2003;我无法在这个

时间升级到2005,所以我无法使用更新的语法或功能。


此函数采用std :: string和将它转换为System :: String *。

这是原始的:

System :: String *

MarshalStdToNetString( std :: string& ss)

{

if(ss.empty())

返回新的System :: String(S"" ;);


System :: IntPtr ptr(static_cast< System :: IntPtr>(static_cast< void *>(const_cast< char *>(ss.c_str())) ));


System :: String * ret(System :: Runtime :: InteropServices :: Marshal :: Ptr ToStringAnsi(ptr));


返回ret;

}

这是我的更新版本:

//函数名称只是我最终会调用的简写它


系统::字符串*

std2gc(const std :: string& s)

{

返回新的System :: String(s.c_str());

}

我是否过度简化?或者是否真的需要所有的铸造业务?

另外,从另一个方向来看,这是将

管理的System :: String *转换为std的正确方法::串?我还没有开始尝试

改变这个:


void

MarshalNetToStdString(System :: String * s ,std :: string& os)

{

使用System :: IntPtr;

使用System :: Runtime :: InteropServices :: Marshal ;


const char * chars =(const char *)(Marshal :: StringToHGlobalAnsi(s))。ToPointer();

os = chars;

Marshal :: FreeHGlobal(IntPtr((void *)chars));

}


-

Marcus Kwok

Hello, I am working on cleaning up some code that I inherited and was
wondering if there is anything wrong with my function. I am fairly
proficient in standard C++ but I am pretty new to the .NET managed C++.
It seems to work fine, but everyone knows that programs with errors can
still appear to "work fine" :)

I am working with VS .NET 2003; I am unable to upgrade to 2005 at this
time, so I cannot use the newer syntax or features.

This function takes a std::string and converts it to a System::String*.
Here is the original:

System::String*
MarshalStdToNetString(std::string& ss)
{
if (ss.empty())
return new System::String(S"");

System::IntPtr ptr(static_cast<System::IntPtr>(static_cast<void*> (const_cast<char*>(ss.c_str()))));

System::String* ret(System::Runtime::InteropServices::Marshal::Ptr ToStringAnsi(ptr));

return ret;
}
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String(s.c_str());
}
Have I oversimplified? Or is all that casting business really needed?
Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven''t started trying to
change this one yet:

void
MarshalNetToStdString(System::String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;

const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer( );
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}

--
Marcus Kwok

推荐答案

嗨马库斯!
Hi Marcus!
这是我的更新版本:
//函数名称只是我最终称之为的简写

System :: String *
std2gc(const std :: string& s)
{
return new System :: String(s.c_str());
}

我是否过度简化了?


不,那看起来真的很棒!


另外,朝另一个方向走,这是转换a的正确方法
管理System :: String *到std :: string?我还没有开始尝试改变这个:

void
MarshalNetToStdString(System :: String * s,std :: string& os)
{
使用System :: IntPtr;
使用System :: Runtime :: InteropServices :: Marshal;

const char * chars =(const char *)(Marshal :: StringToHGlobalAnsi (s))。ToPointer();
os = chars;
Marshal :: FreeHGlobal(IntPtr((void *)chars));
}
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String(s.c_str());
}
Have I oversimplified?
No. That look?′s really good!

Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven''t started trying to
change this one yet:

void
MarshalNetToStdString(System::String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;

const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer( );
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}




看起来还不错......


-

问候

Jochen


关于Win32和.NET的博客
http:// blog .kalmbachnet.de /


Jochen Kalmbach [MVP]< no ****************** **@holzma.de>写道:
Jochen Kalmbach [MVP] <no********************@holzma.de> wrote:
嗨马库斯!
Hi Marcus!
这是我的更新版本:
//函数名称只是我最终称之为的简写<系统::字符串*
std2gc(const std :: string& s)
{
返回新的System :: String(s.c_str());
}

我过度简化了吗?
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String(s.c_str());
}
Have I oversimplified?



不。那看起来真的很棒!



No. That look?s really good!




好​​的,谢谢!



OK, thanks!

另外,从另一个方向来看,这是转换
托管系统的正确方法: :String *转换为std :: string?我还没有开始尝试改变这个:

void
MarshalNetToStdString(System :: String * s,std :: string& os)
{
使用System :: IntPtr;
使用System :: Runtime :: InteropServices :: Marshal;

const char * chars =(const char *)(Marshal :: StringToHGlobalAnsi (s))。ToPointer();
os = chars;
Marshal :: FreeHGlobal(IntPtr((void *)chars));
}
Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven''t started trying to
change this one yet:

void
MarshalNetToStdString(System::String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;

const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer( );
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}



它看起来还不错......



It looks ok...




谢谢,看了一下后我发现了几乎相同的代码在

MSDN中,所以我认为这是正确的这样做的方法,虽然也许我会

更改它以返回std :: string而不是将out-parameter

传递给函数。


-

Marcus Kwok



Thanks, after looking around some more I found almost identical code in
MSDN, so I think this is the "right" way to do it, though maybe I''ll
change it to return the std::string instead of passing an out-parameter
to the function.

--
Marcus Kwok


Marcus Kwok< ri ****** @ gehennom。净>写道:
Marcus Kwok <ri******@gehennom.net> wrote:
此函数采用std :: string并将其转换为
System :: String *。
This function takes a std::string and converts it to a
System::String*.




you还应该考虑std :: wstring转换;可能这些

可能会更快,因为CLI String是unicode one,这意味着在String构造函数和marshaller中不会转换
,你就不会丢失

数据(超出ASCII的数据)

B.



you should also think about std::wstring conversions; potentially these
could be faster, as CLI String is unicode one which means there would''t
be conversion in String constructor and marshaller, and you would''t lose
data (anything beyond ASCII)
B.


这篇关于std :: string&lt; - &gt;系统::字符串*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆