2011-10-06 17 views
6

'System :: String ^'を 'const char *'に変換する方法はありますか?'System :: String ^'を 'const char *'に変換するvC++

マイコード:

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 *'

私を助けてください。

+0

[*私は^文字constのためにシステム::文字列を変換するにはどうすればよい?]の可能複製(http://stackoverflow.com/questions/1098431/how-do-i-convert-a-systemstring -to-const-char) – shf301

+0

[システム::文字列をconst char *に変換する方法は?](http://stackoverflow.com/questions/29335426/how-to-convert-systemstring-to-const) -char) –

答えて

3

これは私のための仕事です。

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

それはこのようなものだ: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 
関連する問題