我只想在C ++中将一个wostringstream(tmp)项添加到我的listBox中。这是我尝试的方式:
for(int i=0; i<6; i++){
tmp<<hex<<m_device_info.Adress.rgBytes[i];
if (i<5)
tmp<<L":";
}
listBox2->Items->Add(tmp.str());
我得到的错误是:
“错误C2664:'System :: Windows :: Forms :: ListBox :: ObjectCollection :: Add' 在'system :: object ^'中转换'wchar_t'是不可能的“
有人有线索吗?
答案 0 :(得分:0)
listBox2->Items->Add( System::Runtime::InteropServices:: Marshal::PtrToStringUni( IntPtr( tmp.str().c_str() ) ) );
最好在.NET应用程序中使用托管的System :: String类,而不是非托管的字符串和流。如果不是绝对必要,请不要混用托管和非托管类型。
这是我测试项目中的代码:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
std::wostringstream s;
s << L"test";
listBox1->Items->Add( System::Runtime::InteropServices::
Marshal::PtrToStringUni( IntPtr( (void*)s.str().c_str() ) ) );
}