在 Visual C++中從 System::String 轉換為 Char

本文說明在 Visual C++ 中使用 Managed 擴充功能從 轉換成 System::String* char* 的數種方式。

原始產品版本: Visual C++
原始 KB 編號: 311259

摘要

本文參考下列Microsoft .NET Framework 類別庫命名空間:

  • System::Runtime::InteropServices
  • Msclr::interop

本文討論使用下列幾種方式從 轉換為 System::String* char*

  • Visual C++ .NET 2002 和 Visual C++ .NET 2003 中C++的 Managed 擴充功能
  • Visual C++ 2005 和 Visual C++ 2008 中的 C++/CLI

方法 1

PtrToStringChars 提供您實際 String 物件的內部指標。 如果您將此指標傳遞至 Unmanaged 函式呼叫,您必須先釘選指標,以確保物件不會在異步垃圾收集程式期間移動:

//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);

方法 2

StringToHGlobalAnsi 將 Managed 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);

注意

在 Visual C++ 2005 和 Visual C++ 2008 中,您必須新增 Common Language Runtime 支援編譯程式選項 (/clr:oldSyntax) 才能成功編譯先前的程式代碼範例。 若要新增 Common Language Runtime 支援編譯程式選項,請遵循下列步驟:

  1. 按兩下 [專案],然後按兩下 [ ProjectName 屬性]。

    注意

    ProjectName 是專案名稱的佔位元。

  2. 展開 [ 組態屬性],然後按兩下 [ 一般]。

  3. 在右窗格中,按兩下即可在 Common Language Runtime 支援項目設定中選取 [Common Language Runtime 支援]、[舊語法] (/clr:oldSyntax)。

  4. 按一下套用,再按一下確定

如需 Common Language Runtime 支援編譯程式選項的詳細資訊,請流覽下列Microsoft開發人員網路 (MSDN) 網站:

/clr (通用語言執行平台編譯)

這些步驟適用於整個文章。

方法 3

VC7 CString 類別具有採用 Managed String 指標的建構函式,並使用其內容載入 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;

注意

此程式代碼不會使用 Visual C++ .NET 2002 或 Visual C++ .NET 2003 中C++的 Managed 延伸模組進行編譯。 它會使用 Visual C++ 2005 中引進的新 C++/CLI 語法,以及 2008 年 Visaul C++ 中引進的新 msclr 命名空間程式代碼。 若要成功編譯此程序代碼,您必須在Visual C++ 2008 中使用 /clr C++ 編譯程序參數。

適用於 C++ 範例程式代碼的 Managed 延伸模組 (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;
}