我正在嘗試除錯一個值為System::String. 當我使用 Visual Studio 時,我通常會使用OutputDebugString或其變體來查看字串,但System::String與OuputDebugString.
如何將 a 轉換為可以列印System::String的值?OuputDebugString或者查看 System::string 值的替代工具?
萬一這很重要,這特別是一個System::String^變數。
uj5u.com熱心網友回復:
System::Diagnostics::Trace::WriteLine(L"Your message");
或者:
String^ s = gcnew String(L"Your message");
System::Diagnostics::Trace::WriteLine(s);
Trace類在內部使用OutputDebugString. 如果您需要OutputDebugString直接使用,您可以使用PtrToStringChars另一個答案中已經提到的。
附加方式:
String^ s = gcnew String(L"Your message\n");
std::wstring ws = msclr::interop::marshal_as<std::wstring>(s);
OutputDebugStringW(ws.c_str();
uj5u.com熱心網友回復:
詳細說明我上面的評論:
您可以使用PtrToStringChars來獲取可以輸入的字串字符OutputDebugString。
從檔案中:
PtrToStringChars 為您提供了一個指向實際 String 物件的內部指標。如果將此指標傳遞給非托管函式呼叫,則必須首先固定指標以確保物件在異步垃圾回收程序中不會移動
在您的情況下使用示例:
#include <vcclr.h>
System::String ^ system_str = gcnew System::String("abcd");
pin_ptr<const wchar_t> wchar_str = PtrToStringChars(system_str);
OutputDebugString(wchar_str);
注意:固定的指標wchar_str在超出范圍時將被釋放。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496373.html
