简体   繁体   中英

.NET System::String to std::string

all, I'm using C++/CLI to write 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.

However, if I open the Chinese named video,and feed it to ffmpeg, ffmpeg can open video correctly. But when I open Korea named video, ffmepg can't open 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:

OpenVideo(char* video_path);

My code in 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM