方法 : System::String を wchar_t* または char* に変換する

更新 : 2007 年 11 月

Vcclr.h の PtrToStringChars を使用して、String をネイティブな wchar_t * または char * に変換できます。CLR 文字列は内部的には Unicode であるため、変換を行うと、常にワイド文字列の Unicode ポインタが返されます。したがって、次の例で示すように、ワイド文字列からの変換を行うことができます。

使用例

// 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

参照

参照

C++ Interop (暗黙の PInvoke) の使用