I’m trying to write a universal text editor which can open and display ANSI and Unicode in EditControl. Do I need to repeatedly call ReadFile() if I determine that the text is ANSI? Can’t figure out how to perform this task. My attempt below does not work, it displays ‘?’ characters in EditControl.
LARGE_INTEGER fSize;
GetFileSizeEx(hFile,&fSize);
int bufferLen = fSize.QuadPart/sizeof(TCHAR)+1;
TCHAR* buffer = new TCHAR[bufferLen];
buffer[0] = _T('\0');
DWORD wasRead = 0;
ReadFile(hFile,buffer,fSize.QuadPart,&wasRead,NULL);
buffer[wasRead/sizeof(TCHAR)] = _T('\0');
if(!IsTextUnicode(buffer,bufferLen,NULL))
{
CHAR* ansiBuffer = new CHAR[bufferLen];
ansiBuffer[0] = '\0';
WideCharToMultiByte(CP_ACP,0,buffer,bufferLen,ansiBuffer,bufferLen,NULL,NULL);
SetWindowTextA(edit,ansiBuffer);
delete[]ansiBuffer;
}
else
SetWindowText(edit,buffer);
CloseHandle(hFile);
delete[]buffer;
There are a few buffer length errors and oddities, but here’s your big problem. You call
WideCharToMultiByteincorrectly. That is meant to receive UTF-16 encoded text as input. But whenIsTextUnicodereturns false that means that the buffer is not UTF-16 encoded.The following is basically what you need:
Note that I’ve fixed the length parameter to
IsTextUnicode.For what it is worth, I think I’d read in to a buffer of
char. That would remove the need for thesizeof(TCHAR). In fact I’d stop usingTCHARaltogether. This program should be Unicode all the way –TCHARis what you use when you compile for both NT and 9x variants of Windows. You aren’t compiling for 9x anymore I imagine.So I’d probably code it like this:
Note also that this code makes no allowance for the possibility of receiving UTF-8 encoded text. If you want to handle that you’d need to take your
charbuffer and send to throughMultiByteToWideCharusingCP_UTF8.