We use the Windows function MultiByteToWideChar to convert multibyte strings to wide strings. As shown below:
int MultiByteToWideChar ( UINT ucodepage, //identifies a code page value associated with a multibyte character DWORD dwFlags, // Additional control is allowed, but generally passed in 0 pcstr pmultibytestr,// string to convert int cbmultibyte, / The length of the string, if passed-1, automatically determines the length pwstr pwidecharstr, //The resulting Unicode string is passed into the specified memory buffer int Cchwidechar); Specifies the maximum length of the buffer
Specific steps to convert:
1. Call MultiByteToWideChar,
Pass NULL to the PWIDECHARSTR parameter,
Pass in 0 for the Cchwidechar parameter,
The Cbmultibyte parameter is passed in-1.
2. Allocate a block of memory sufficient to accommodate the converted Unicode string.
Its size is the return value of the previous MultiByteToWideChar call multiplied by sizeof (wchar_t).
3. Call MultiByteToWideChar again, this time
The buffer address is passed in as the value of the PWIDECHARSTR parameter.
The return value of the first MultiByteToWideChar call is multiplied by sizeof (wchar_t) to get the size passed in as the value of the Cchwidechar parameter.
4. Use the converted string.
5. Releases the block of memory occupied by the Unicode string.
Convert a multi-byte string to Unicode on Windows