WriteFile(...) changes file encoding from UTF-16 LE to UTF-8

278 views Asked by At

I have UTF-16 LE file. If I write the text to .txt file, it becomes UTF-8. Code:

hFile = CreateFile(TEXT("D:\\clown.txt"), GENERIC_READ | GENERIC_WRITE,  FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
    wsprintf(buffer, TEXT("ERROR CODE: %i"), GetLastError());
    MessageBox(NULL, buffer, TEXT("Error"), MB_OK | MB_ICONERROR);
    return 0;
}
MessageBox(NULL, TEXT("hFile was created"), TEXT("Successfully"), MB_OK);
sBuff = GetWindowText(hEdit, buffer, BUFFER_SIZE);
if (!WriteFile(hFile, buffer, sBuff * 2, &i, NULL))
{
    wsprintf(buffer, TEXT("ERROR CODE: %i"), GetLastError());
    MessageBox(NULL, buffer, TEXT("Error"), MB_OK | MB_ICONERROR); 
    return 0;
}
wsprintf(buffer, TEXT("%i"), GetLastError());
MessageBox(NULL, buffer, TEXT("Successfully"), MB_OK);
CloseHandle(hFile);
return 0;

WriteFileEx have the same problem

1

There are 1 answers

0
IInspectable On

WriteFile (and WriteFileEx) accept raw binary data and pass it to the device identified by the hFile parameter. No attempt is made to transcode or even interpret the data. The device receives a byte-for-byte copy of the input buffer.

When calling WriteFile on a file handle and the file contents aren't what you expected, the issue is with the input to WriteFile rather than an issue with the API itself. The input comes from a call to GetWindowText, and the problem is here, in all likelihood.

GetWindowText is a preprocessor macro that expands to either GetWindowTextW or GetWindowTextA depending on whether the UNICODE preprocessor symbol is defined. Since the code isn't self-sufficient, I'm going to have to guess here:

  • The code is compiled using a C compiler
  • The code author ignores compiler warnings
  • UNICODE is not defined

Since we don't know with certainty what the problem is, the following is an educated guess: To solve the issue, replace GetWindowText with GetWindowTextW.