本文介绍了如何将System :: String转换为const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将'String ^'转换为'const char *'?

how to convert 'String^' to 'const char*'?

     String^ cr = ("netsh wlan set hostednetwork mode=allow ssid=" + this->txtSSID->Text + " key=" + this->txtPASS->Text);
system(cr);

错误:

1   IntelliSense: argument of type "System::String ^" is incompatible with parameter of type "const char *"


推荐答案

您可以使用类:

You can do this using the msclr::interop::marshal_context class:

#include <msclr/marshal.h>

然后:

String^ something = "something";

msclr::interop::marshal_context ctx;
const char* converted = ctx.marshal_as<const char*>(something);

system(converted);

转换的缓冲区 ctx 超出范围。

但在你的情况下,更容易调用一个等效的托管API :

But in your case it would be much easier to just call an equivalent managed API:

System::Diagnostics::Process::Start("netsh", "the args");

这篇关于如何将System :: String转换为const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:41