.net 在 C++/CLI 中在 char* 和 System::String 之间转换的最佳方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:47:49  来源:igfitidea点击:

What is the best way to convert between char* and System::String in C++/CLI

.netstringc++-cli

提问by Brian Stewart

What is the approved way to convert from char* to System::string and back in C++/CLI? I found a few references to marshal_to<> templated functions on Google, but it appears that this feature never made the cut for Visual Studio 2005 (and isn't in Visual Studio 2008 either, AFAIK). I have also seen some code on Stan Lippman's blog, but it's from 2004. I have also seen Marshal::StringToHGlobalAnsi(). Is there a method that is considered "best practice"?

在 C++/CLI 中从 char* 转换为 System::string 并返回的认可方法是什么?我在 Google 上发现了一些对 marshal_to<> 模板化函数的引用,但似乎此功能从未适用于 Visual Studio 2005(并且在 Visual Studio 2008 中也没有,AFAIK)。我也在Stan Lippman 的博客上看到了一些代码,但它是 2004 年的。我也看到了 Marshal::StringToHGlobalAnsi()。有没有被认为是“最佳实践”的方法?

采纳答案by Matthew Clendening

There's a good overview here (this marshaling support added for VS2008): http://www.codeproject.com/KB/mcpp/OrcasMarshalAs.aspx

这里有一个很好的概述(为 VS2008 添加了这种编组支持):http: //www.codeproject.com/KB/mcpp/OrcasMarshalAs.aspx

回答by Ben Straub

System::String has a constructor that takes a char*:

System::String 有一个带有 char* 的构造函数:

 using namespace system;
 const char* charstr = "Hello, world!";
 String^ clistr = gcnew String(charstr);
 Console::WriteLine(clistr);

Getting a char* back is a bit harder, but not too bad:

取回 char* 有点困难,但还不错:

 IntPtr p = Marshal::StringToHGlobalAnsi(clistr);
 char *pNewCharStr = static_cast<char*>(p.ToPointer());
 cout << pNewCharStr << endl;
 Marshal::FreeHGlobal(p);

回答by Dan Blair

What we did is made a C++\CLI object that held the string in unmangaed code and would give out manged copies of the item. The conversion code looks very much like what Stan has on his blog (I can't remember it exactly)(If you use his code make sure you update it to use delete[]) but we made sure that the destructor would handle releasing all the unmanged elements of the object. This is a little overblown but we didn't have leaks when we tied into legacy C++ code modules.

我们所做的是创建一个 C++\CLI 对象,该对象将字符串保存在非托管代码中,并提供该项目的托管副本。转换代码看起来很像 Stan 在他的博客上写的(我记不清了)(如果您使用他的代码,请确保将其更新为使用 delete[]),但我们确保析构函数可以处理释放所有对象的非托管元素。这有点夸大其词,但是当我们绑定到遗留 C++ 代码模块时,我们没有泄漏。

回答by dko

I created a few helper methods. I needed to do this to move from an old Qt library to CLI String. If anyone can add onto this and tell me if it seems like I have a memory leak and what I can do to fix it, I would be most appreciative.

我创建了一些辅助方法。我需要这样做才能从旧的 Qt 库移动到 CLI 字符串。如果有人可以添加此内容并告诉我我是否有内存泄漏以及我可以做些什么来修复它,我将不胜感激。

void MarshalString (  String ^ s, wstring& os ) {
    using namespace Runtime::InteropServices;
    const wchar_t* char = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
    os = char;
}
QString SystemStringToQt( System::String^ str)
{
    wstring t;
    MarshalString(str, t);
    QString r = QString::fromUcs2((const ushort*)t.c_str());
    return r;
}

回答by Tadej Mali

One additional link to a summary of possible ways:

一个额外的链接到可能的方法摘要:

http://support.microsoft.com/?kbid=311259

http://support.microsoft.com/?kbid=311259