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
WriteFile(andWriteFileEx) accept raw binary data and pass it to the device identified by thehFileparameter. 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
WriteFileon a file handle and the file contents aren't what you expected, the issue is with the input toWriteFilerather than an issue with the API itself. The input comes from a call toGetWindowText, and the problem is here, in all likelihood.GetWindowTextis a preprocessor macro that expands to eitherGetWindowTextWorGetWindowTextAdepending on whether theUNICODEpreprocessor symbol is defined. Since the code isn't self-sufficient, I'm going to have to guess here:UNICODEis not definedSince we don't know with certainty what the problem is, the following is an educated guess: To solve the issue, replace
GetWindowTextwithGetWindowTextW.