String^ to string 與 string to String^

String^ to string

//Convert std::string to System::String^,os is std::string
String^ str = gcnew String(os.c_str());


string to String^

第一種方式

//In here source is System::String^
const char* chars = (const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(source)).ToPointer();
string dest = chars;

第二種方式:若出現'^' 不能在類型'std::array'上使用這個間接取值之類的錯誤時,可以寫成函式來轉換

//In here source is System::String^
void MarshalString(System::String^ s, std::string& os)
{
	using namespace System::Runtime::InteropServices;
	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
	os = chars;
	Marshal::FreeHGlobal(System::IntPtr((void*)chars));
}

留言

這個網誌中的熱門文章

🔍Vue.js 專案錯誤排查:解決 numericFields is not defined 與合併儲存格邏輯最佳化

🛠【ASP.NET Core + Oracle】解決 ORA-00904 "FALSE": 無效的 ID 錯誤與資料欄位動態插入顯示問題

🔎EF Core 連 Oracle 出現 ORA-00600 [kpp_concatq:2] 的完整排錯指南(含 EF Core ToString/CultureInfo 錯誤)