평가 및 의견을 보내려면 클릭하십시오.
MSDN
MSDN Library
개발 도구 및 언어
Visual Studio 2008
Visual Studio
Visual C++
일반적인 프로그래밍 방법론
네이티브 및 .NET 상호 운용성
관리 코드에서 네이티브 함수 호출
C++ Interop 사용(암시적 PInvoke)
 방법: System::String을 wchar_t* 또는 cha...

  저대역폭 보기 설정
이 페이지에서 다루는 특정 제품:.
Microsoft Visual Studio 2008/.NET Framework 3.5

다음 제품들은 다른 버전에서 다루어 집니다.
Visual C++
방법: System::String을 wchar_t* 또는 char*로 변환

업데이트: 2007년 11월

Vcclr.h에서 PtrToStringChars를 사용하여 String을 네이티브 wchar_t * 또는 char *로 변환할 수 있습니다. 이렇게 하면 항상 와이드 유니코드 문자열 포인터가 반환됩니다. CLR 문자열은 내부적으로 유니코드이기 때문입니다. 그런 다음 아래 예제에서와 같이 와이드 문자열을 변환할 수 있습니다.

// convert_string_to_wchar.cpp
// compile with: /clr
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >

using namespace System;

int main() {
   String ^str = "Hello";

   // Pin memory so GC can't move it while native function is called
   pin_ptr<const wchar_t> wch = PtrToStringChars(str);
   printf_s("%S\n", wch);

   // Conversion to char* :
   // Can just convert wchar_t* to char* using one of the 
   // conversion functions such as: 
   // WideCharToMultiByte()
   // wcstombs_s()
   // ... etc
   size_t convertedChars = 0;
   size_t  sizeInBytes = ((str->Length + 1) * 2);
   errno_t err = 0;
   char    *ch = (char *)malloc(sizeInBytes);

   err = wcstombs_s(&convertedChars, 
                    ch, sizeInBytes,
                    wch, sizeInBytes);
   if (err != 0)
      printf_s("wcstombs_s  failed!\n");

    printf_s("%s\n", ch);
}
Hello
Hello
커뮤니티 콘텐츠   커뮤니티 콘텐츠란?
새 콘텐츠 추가 RSS  주석
Processing
© 2009 Microsoft Corporation. All rights reserved. 사용약관  |  상표  |  개인정보보호
Page view tracker