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));
}

留言

這個網誌中的熱門文章

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

🛠【實戰排除教學】從 VS Code 的 _logger 錯誤,到 PowerShell 找不到 npm/serve,再到 Oracle ORA-03135 連線中斷——一次搞懂!

🔎如何在 Oracle PL/SQL 儲存過程中為文字欄位加入換行符號(CHR(10))——以 Updlcmremark 為例