簡體   English   中英

.NET System :: String到std :: string

[英].NET System::String to std::string

所有,我正在使用C ++ / CLI編寫Winform。 我的操作系統語言是中文。 我從openfiledialog獲取System :: String,並使用.NET將System :: String轉換為UTF8編碼,最后我使用StringToHGlobalAnsi將其轉換為std :: string。

但是,如果我打開中文命名的視頻並將其提供給ffmpeg,則ffmpeg可以正確打開視頻。 但是,當我打開名為video的Korea時,ffmepg無法打開video。 有人知道如何打開與操作系統語言不同的不同語言的視頻嗎?

感謝大伙們 ! 我遵循盧卡斯的建議。 這是可以打開不同語言的視頻文件名的代碼。

我的本機dll API:

OpenVideo(char* video_path);

我在C ++ / CLI中的代碼:

System::String^ multi_language_str = str_from_openfile_dialog;

const wchar_t* wstr_file_name = (const wchar_t*)(Marshal::StringToHGlobalUni(multi_language_str)).ToPointer();

std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;

std::string str_file_name = converter.to_bytes(wstr_file_name);

OpenVideo(str_file_name.c_str());

Marshal::FreeHGlobal((IntPtr)(void*)wstr_file_name);

我認為您最好使用此方法,它減少了資源管理的麻煩,並且由於使用了pin_ptr的析構函數,因此更加安全異常:

array<unsigned char>^ bytes = System::Text::Encoding::UTF8::GetBytes(str_from_openfile_dialog + L'\0');
cli::pin_ptr<unsigned char> pinned = &bytes[0];
const unsigned char * puString = pinned;

OpenVideo(static_cast<const char *>(puString));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM