marshal_context 類別

這個類別會在原生和 Managed 環境之間轉換。

語法

class marshal_context

備註

為需要內容的資料轉換使用 marshal_context 類別。 如需哪些轉換需要內容以及必須包含哪些封送處理檔案的詳細資訊,請參閱 C++ 封送處理概觀。 在您使用內容時的封送處理結果的有效性只到 marshal_context 物件終結時。 若要保存結果,您必須複製資料。

這同樣 marshal_context 可用於許多資料轉換。 以這種方式重複使用內容不會影響先前封送處理呼叫的結果。

成員

公用建構函式

名稱 描述
marshal_context::marshal_context marshal_context建構 物件,以用於 Managed 和原生資料類型之間的資料轉換。
marshal_context::~marshal_context 終結 marshal_context 物件。

公用方法

名稱 描述
marshal_context::marshal_as 在特定資料物件上執行封送處理,以便在 Managed 資料類型和原生資料類型之間進行轉換。

需求

標頭檔: < msclr\marshal.h > 、 < msclr\marshal_windows.h、 < msclr\marshal_cppstd.h > 或 < msclr\marshal_atl.h >>

命名空間: msclr::interop

marshal_context::marshal_context

marshal_context建構 物件,以用於 Managed 和原生資料類型之間的資料轉換。

marshal_context();

備註

某些資料轉換需要封送處理內容。 如需哪些翻譯需要內容以及您必須包含在應用程式中之封送處理檔案的詳細資訊,請參閱 C++ 封送處理概觀。

範例

如需 marshal_coNtext::marshal_as ,請參閱範例

marshal_context::~marshal_context

終結 marshal_context 物件。

~marshal_context();

備註

某些資料轉換需要封送處理內容。 如需哪些翻譯需要內容以及哪些封送處理檔案必須包含在應用程式中的詳細資訊,請參閱 C++ 封送處理概觀。

刪除 marshal_context 物件將會使要由該內容轉換的資料失效。 如果想要在終結 marshal_context 物件之後保留資料,您必須手動將資料複製至會保存的變數。

marshal_context::marshal_as

在特定資料物件上執行封送處理,以便在 Managed 資料類型和原生資料類型之間進行轉換。

To_Type marshal_as<To_Type>(
   From_Type input
);

參數

input
[in]您想要封送處理至 To_Type 變數的值。

傳回值

To_Type 別的變數,其為 的轉換值 input

備註

這個函式會在特定資料物件上執行封送處理。 只有在 C++ 封送處理概觀中資料表所指示的轉換時 ,才使用此函式。

如果您嘗試封送處理不支援的一組資料類型, marshal_as 在編譯時期會產生錯誤 C4996 。 如需詳細資訊,請閱讀此錯誤所提供的訊息。 C4996錯誤可能只針對已被取代的函式產生。 產生此錯誤的兩個條件正嘗試封送處理不支援的資料類型組,並嘗試用於 marshal_as 需要內容的轉換。

封送處理程式庫包含數個標頭檔。 任何轉換只需要一個檔案,但如果您需要進行其他轉換,則可以包含其他檔案。 Marshaling Overview in C++ 中的表格所指出的封送處理檔案應該包含在每次轉換中。

範例

這個範例會建立從 System::Stringconst char * 變數類型的封送處理的內容。 在刪除內容行之後,轉換的資料將無法有效。

// marshal_context_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main() {
   marshal_context^ context = gcnew marshal_context();
   String^ message = gcnew String("Test String to Marshal");
   const char* result;
   result = context->marshal_as<const char*>( message );
   delete context;
   return 0;
}