简体   繁体   English

.NET System :: String到std :: string

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

all, I'm using C++/CLI to write Winform. 所有,我正在使用C ++ / CLI编写Winform。 My operating system language is Chinese. 我的操作系统语言是中文。 I get the System::String from openfiledialog ,and use .NET transform the System::String to UTF8 encoding, finally I use StringToHGlobalAnsi to convert it to std::string. 我从openfiledialog获取System :: String,并使用.NET将System :: String转换为UTF8编码,最后我使用StringToHGlobalAnsi将其转换为std :: string。

However, if I open the Chinese named video,and feed it to ffmpeg, ffmpeg can open video correctly. 但是,如果我打开中文命名的视频并将其提供给ffmpeg,则ffmpeg可以正确打开视频。 But when I open Korea named video, ffmepg can't open video. 但是,当我打开名为video的Korea时,ffmepg无法打开video。 Does anybody know how to open different language video which is different from operating system language? 有人知道如何打开与操作系统语言不同的不同语言的视频吗?

Thank you guys ! 感谢大伙们 ! I follow Lucas's suggestion. 我遵循卢卡斯的建议。 Here is the code which can open different language video file name. 这是可以打开不同语言的视频文件名的代码。

My native dll API: 我的本机dll API:

OpenVideo(char* video_path);

My code in C++/CLI: 我在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);

I think you'd be better off using this, which has less hassle of managing resources, and is more exception-safe thanks to pin_ptr's destructor: 我认为您最好使用此方法,它减少了资源管理的麻烦,并且由于使用了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