2011-10-06 4 views
6

'System :: String ^'을 'const char *'로 변환 할 수 있습니까?'System :: String ^'을 'const char *'vC++로 변환

내 코드 :

IplImage *img1 = cvLoadImage(Result1, 1);

나는 오류 다음 생성이 위와 같이 할 경우

String ^Result1= "C:/Users/Dev/Desktop/imag.jpg";

.

error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'

제발 도와주세요.

+0

가능한 중복 ([I이 시스템 :: 문자열 ^에서 const 문자 *?을 변환하려면 어떻게] http://stackoverflow.com/questions/1098431/how-do-i-convert-a-systemstring -to-const-char) – shf301

+0

가능한 [System :: String을 const char \ *로 변환하는 방법] (http://stackoverflow.com/questions/29335426/how-to-convert-systemstring-to-const) -숯) –

답변

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 
관련 문제