How to save an UTF-8 string as file name with CreateFileW API Function
Posted on 2006-11-02
I am currently developing a upload component for web page, the html page uses UTF-8 encoding and uploads a file that named with simplified chinese characters. The vb6 component uses the MultiByteToWideChar API function to convert the data requested from Request object with UTF-8 (65001), then uses CreateTextW function to save file onto disk.
lngFileHandle = CreateFile(strFileName, GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS, 0, 0)
I've tried to use CreateFileA() function, but strFileName contained unicode chars which CreateFileA() did not recognise (shown as ?) and returned a "Bad file name or number" error.
Using CreateFileW(), it accepts the file name, but it does not recognise the encoding of strFileName and saves the file under the folder where the vb project is running, this time the file name is made up of unknown characters.
I then use:
and got some of the simplified chinese characters right, and others became "?" or square blocks.
What I don't understand is that the byte array or string which passed into CreateFileW() is already in UTF-8 format, but still requires the StrConv() function to convert it into Unicode format to create a partially correct filename. Does Windows XP/2003 store file names in different format other than UTF-8?
Even if I pass a string without any encoding (e.g. "aaa"), CreateFileW() still creates the file with unreadable file name.
How can I save the file name in Unicode/UTF-8 format correctly without the need of StrConv()?
Thank you very much!
P.S. Please forgive me for my bad english.