Need help with edit box

I can't display unicode characters properly in an edit control. They get displayed as square boxes and sometimes question marks. I'm using msvc++ 2008. The project Character Set in Project->Properties is set to Use Unicode Character Set. This is the code for creating the edit box
 
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE, 0, 0, 100, 100, hwnd, (HMENU)MAIN_EDIT, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

This is the code for determining file size, allocating memory, reading file in memory and setting the edit box text
1
2
3
4
5
6
7
8
9
10
11
DWORD dwFileSize = GetFileSize(hFile, NULL);  //File size
LPTSTR pszFileText = (LPTSTR)GlobalAlloc(GPTR, dwFileSize+1);  //Allocate memory
if(pszFileText != NULL)
{
    DWORD dwRead;  //Number of characters read by ReadFile function
    if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))  //Read text file to memory
    {
        pszFileText[dwFileSize] = 0;  //Add null terminating character to end of text read in memory
        SetWindowText(hWndEdit, pszFileText);
    }
}

I'm trying to open a file in the edit control, but the text gets displayed as gibberish. When i paste text everything gets displayed correctly. If i change the Characet Set in Project->Properties to Not Set and change the text in the source file from L"..." to "..." everything gets displayed correctly but i want the project to be unicode. Please help

Last edited on
You need to use MultiByteToWideChar() to convert between already known file encoding to UTF-16 and then pass the reult to SetWindowsTextW().

Do not use GlobalAlloc and ReadFile that way, it will only work if the file encoding is already UTF16 and you should use BYTE (aka unsigned char, not LPTSTR there).
Topic archived. No new replies allowed.