How to open a file with UNICODE filename on Window

2020-05-29 04:30发布

问题:

There is a 3rd lib only accept char* filename e.g. 3rdlib_func_name(char* file_name). Every things get wrong when I provide a filename in Chinese or Japanese.

Is there any way to make this lib open UNICODE filename? The program is running on Windows.

Thanks for your reply.

回答1:

We has a similar problem too. Luckily there's a solution, though it's kinda tricky.

If the file/directory already exists - you may use the GetShortPathName function. The resulting "short" path name is guaranteed not to contain non-latin characters.

  1. Call GetShortPathNameW (unicode version) to get the "short" path string.
  2. Convert the short path into the ANSI string (use WideCharToMultiByte).
  3. Give the resulting ANSI string to the stupid 3rd-party lib.

Now, if the file/directory doesn't exist yet - you may not obtain its short pathname. In such a case you should create it first.



回答2:

No, there isn't unless you can recompile it from modified source (a major undertaking). You might have better luck feeding the 3rd party library short filenames, like AHDF76~4.DOC; these filenames use ASCII. See GetShortPathName.



回答3:

You may try to convert the string to local code page:

setlocale(LC_ALL,"Japanese_Japan.932");
std::string file_name = convert_to_codepage_932(utf16_file_name);
3rdlib_func_name(file_name.c_str());

Otherwise?

Blame windows for not supporting UTF-8 ;-)



标签: unicode fopen