String 将字符串^转换为标准C++;一串
我遇到了一个问题String 将字符串^转换为标准C++;一串,string,visual-c++,String,Visual C++,我遇到了一个问题无法将参数从'System::String^'转换为'std::String'。在VisualC++环境中,如何将String::String ^转换为标准C++字符串?< /P> < P>:< /P> void封送处理字符串(字符串^s、字符串&os){ 使用命名空间运行时::InteropServices; 常量字符*字符= (常量字符*)(封送:StringToHGlobalAnsi(s)).ToPointer(); os=字符; marshall::FreeHGlobal
无法将参数从'System::String^'转换为'std::String'
。在VisualC++环境中,如何将String::String ^转换为标准C++字符串?< /P> < P>:< /P>
void封送处理字符串(字符串^s、字符串&os){
使用命名空间运行时::InteropServices;
常量字符*字符=
(常量字符*)(封送:StringToHGlobalAnsi(s)).ToPointer();
os=字符;
marshall::FreeHGlobal(IntPtr((void*)chars));
}
int main(){
字符串a=“测试”;
字符串^c=gc新字符串(“abcd”);
库特
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
int main() {
string a = "test";
String ^ c = gcnew String("abcd");
cout << a << endl;
MarshalString(c, a);
cout << a << endl;
}
//*** convert String^ to standard C++ string
` String ^SyStr = "abc"; // system string
string StStr =""; // standard string
for each (char c in SyStr )
{
StStr .push_back( c);
} `
//**** End convert String^ to standard C++ string
//**** convert standard C++ string to String^
` String ^SyStr = ""; // system string
string StStr ="xyz"; // standard string
SyStr = gcnew String(StStr.c_str());`
//**** End convert standard C++ string to String^