この記事では、Visual C++ でマネージド拡張機能を使用して System::String* から char* に変換するいくつかの方法について説明します。
元の製品バージョン: Visual C++
元の KB 番号: 311259
まとめ
この記事では、次の Microsoft .NET Framework クラス ライブラリ名前空間について説明します。
System::Runtime::InteropServicesMsclr::interop
この記事では、次を使用して System::String* から char* に変換するいくつかの方法について説明します。
- Visual C++ .NET 2002 および Visual C++ .NET 2003 での C++ のマネージド拡張機能
- Visual C++ 2005 および Visual C++ 2008 の C++/CLI
方法 1
PtrToStringChars は、実際の String オブジェクトへの内部ポインターを提供します。 このポインターをアンマネージ関数呼び出しに渡す場合は、まずポインターをピン留めして、非同期ガベージ コレクション プロセス中にオブジェクトが移動しないようにする必要があります。
//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);
方法 2
StringToHGlobalAnsi は、マネージド String オブジェクトの内容をネイティブ ヒープにコピーし、その場で米国国立標準研究所 (ANSI) 形式に変換します。 このメソッドは、必要なネイティブ ヒープ メモリを割り当てます。
//using namespace System::Runtime::InteropServices;
System::String * str = S"Hello world\n";
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
Marshal::FreeHGlobal(str2);
Note
Visual C++ 2005 および Visual C++ 2008 では、前のコード サンプルを正常にコンパイルするには、共通言語ランタイム サポート コンパイラ オプション (/clr:oldSyntax) を追加する必要があります。 共通言語ランタイム サポート コンパイラ オプションを追加するには、次の手順に従います。
[ Project] をクリックし、[ProjectName プロパティ ] をクリック。
Note
ProjectName は、プロジェクトの名前のプレースホルダーです。
Configuration プロパティ展開し、General をクリックします。
右側のウィンドウで、Common 言語ランタイム のサポートプロジェクト設定で、Common 言語ランタイム サポート、古い構文 (/clr:oldSyntax)を選択します。
適用をクリックし、OK をクリックします。
共通言語ランタイム サポート コンパイラ オプションの詳細については、次の Microsoft Developer Network (MSDN) Web サイトを参照してください。
これらの手順は、記事全体に適用されます。
方法 3
VC7 CString クラスには、マネージド文字列ポインターを受け取り、その内容を含む CString を読み込むコンストラクターがあります。
//#include <atlstr.h>
System::String * str = S"Hello world\n";
CString str3(str);
printf(str3);
方法 4
Visual C++ 2008 では、 marshal_as<T> マーシャリング ヘルプ クラスと marshal_context() マーシャリング ヘルパー クラスが導入されています。
//#include <msclr/marshal.h>
//using namespace msclr::interop;
marshal_context ^ context = gcnew marshal_context();
const char* str4 = context->marshal_as<const char*>(str);
puts(str4);
delete context;
Note
このコードは、Visual C++ .NET 2002 または Visual C++ .NET 2003 で C++ 用のマネージド拡張機能を使用してコンパイルされることはありません。 Visual C++ 2005 で導入された新しい C++/CLI 構文と、Visaul C++ 2008 で導入された新しい msclr 名前空間コードを使用します。 このコードを正常にコンパイルするには、Visual C++ 2008 で /clr C++ コンパイラ スイッチを使用する必要があります。
C++ 用マネージド拡張機能のサンプル コード (Visual C++ 2002 または Visual C++ 2003)
//compiler option: cl /clr
#include <vcclr.h>
#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
int _tmain(void)
{
System::String * str = S"Hello world\n";
//method 1
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);
//method 2
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
Marshal::FreeHGlobal(str2);
//method 3
CString str3(str);
wprintf(str3);
return 0;
}
C++/CLI サンプル コード (Visual C++ 2005 および Visual C++ 2008)
//compiler option: cl /clr
#include <atlstr.h>
#include <stdio.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
#if _MSC_VER > 1499 // Visual C++ 2008 only
#include <msclr/marshal.h>
using namespace msclr::interop;
#endif
int _tmain(void)
{
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
return 0;
}