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

发布于 2024-07-04 16:43:31 字数 360 浏览 8 评论 0原文

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

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半城柳色半声笛 2024-07-11 16:43:31

可能方法摘要的另一个链接:

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

One additional link to a summary of possible ways:

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

没企图 2024-07-11 16:43:31

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

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.

じ违心 2024-07-11 16:43:31

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

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

口干舌燥 2024-07-11 16:43:31

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

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

获取 char* 有点困难,但也不算太糟糕:

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

System::String has a constructor that takes a 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:

 IntPtr p = Marshal::StringToHGlobalAnsi(clistr);
 char *pNewCharStr = static_cast<char*>(p.ToPointer());
 cout << pNewCharStr << endl;
 Marshal::FreeHGlobal(p);
枫以 2024-07-11 16:43:31

我创建了一些辅助方法。 我需要这样做才能从旧的 Qt 库迁移到 CLI String。 如果有人可以补充这一点并告诉我是否有内存泄漏以及我可以采取哪些措施来修复它,我将非常感激。

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;
}

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文