gpt4 book ai didi

string - 在vc++中将 'System::String ^'转换为 'const char *'

转载 作者:行者123 更新时间:2023-12-04 13:24:33 25 4
gpt4 key购买 nike

如何在vc++中将'System::String ^'转换为'const char *'?

我的代码:
String ^Result1= "C:/Users/Dev/Desktop/imag.jpg";IplImage *img1 = cvLoadImage(Result1, 1);
如果我喜欢上面的方法,它将产生以下错误。
error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
请帮我。

最佳答案

就像这样:How to convert from System::String* to Char* in Visual C++

System::String ^ str = "Hello world\n";

//method 1
pin_ptr<const wchar_t> str1 = PtrToStringChars(str);
wprintf(str1);

//method 2
char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
printf(str2);
Marshal::FreeHGlobal((IntPtr)str2);

//method 3
CString str3(str);
wprintf(str3);

//method 4
#if _MSC_VER > 1499 // Visual C++ 2008 only
marshal_context ^ context = gcnew marshal_context();
const char* str4 = context->marshal_as<const char*>(str);
puts(str4);
delete context;
#endif

关于string - 在vc++中将 'System::String ^'转换为 'const char *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com