marshal_as
此方法會在原生與受控環境之間轉換資料。
語法
To_Type marshal_as<To_Type>(
From_Type input
);
參數
input
[in]您想要封送處理至 To_Type
變數的值。
傳回值
型 To_Type
別的變數,其為 的轉換值 input
。
備註
這個方法是一種簡化的方式,可將資料在原生和 Managed 類型之間轉換。 若要判斷支援哪些資料類型,請參閱 C++ 封送處理概觀。 某些資料轉換需要內容。 您可以使用 marshal_coNtext 類別 來轉換這些資料類型 。
如果您嘗試封送處理不支援的一組資料類型, marshal_as
在編譯時期會產生錯誤 C4996 。 如需詳細資訊,請閱讀此錯誤所提供的訊息。 C4996
錯誤可能只針對已被取代的函式產生。 其中一個範例是嘗試封送處理不支援的一組資料類型。
封送處理程式庫包含數個標頭檔。 任何轉換只需要一個檔案,但如果您需要進行其他轉換,則可以包含其他檔案。 若要查看哪些轉換與哪些檔案相關聯,請在 中的 Marshaling Overview
資料表中尋找 。 不論您想要執行的轉換為何,命名空間需求一律有效。
System::ArgumentNullException(_EXCEPTION_NULLPTR)
如果輸入參數為 null,則擲回 。
範例
這個範例會將 從 const char*
封送處理到 System::String
變數類型。
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
int main() {
const char* message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
需求
標頭檔: < msclr\marshal.h > 、 < msclr\marshal_windows.h、 < msclr\marshal_cppstd.h > 或 < msclr\marshal_atl.h >>
命名空間: msclr::interop
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應