1
votes

I am using a Win32 edit to display debugging information, and I have placed the edit, along with the rest of my basic GUI in a class. But when I output anything to the edit it displays '??????????????????????????'. I think the error lies in my MyGUI::append(LPCSTR) method, although it has always worked perfectly in the past. Any comments/ideas/solutions will be appreciated. If I need to post all the code pertaining to my GUI class please let me know so.

My class lies in the namespace Interface, along with the stand-alone WindowProcedure function, which I call when registering the application with the WNDCLASSEX object.

The win32 edit is not created in the WM_CREATE handle within the WindowProcedure(as it probably should be) as I could not place the function inside my GUI class.

Method that creates the edit:

HWND createEdit( HINSTANCE hInst, HWND hwnd, int appBott, int appTop ){
    return CreateWindowEx(  WS_EX_APPWINDOW,
                            TEXT("EDIT"), TEXT(""),
                            WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_MULTILINE| WS_VSCROLL | WS_HSCROLL,
                            10, 10, appBott-25, appTop-50,
                            hwnd,
                            (HMENU) 102,
                            hInst,
                            NULL );
}

Used in 'guiCreate()' method as: HWND hEdit = createEdit( hInst, hWin, appWidth, appHeight );

Method that displays text in edit:

void Interface::MyGUI::append( LPCSTR text ){
    if( created && !stopAll ){
        int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
        SendMessageW(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
        SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM) text);
    }
}

Used in main program as:

MyGUI form(); //initialize form
form.append( (LPCSTR)"Example text\n" );

Input text: 'Example text.\n' Displayed text: '?????????????? l'

1

1 Answers

2
votes

You are targeting ANSI it would seem. In that case, don't call SendMessageW, call SendMessageA or even SendMessage and let that be expanded to SendMessageA.

You call SendMessageW but pass ANSI encoded text. When you called SendMessageW you promised to send UTF-16 encoded text.

However, you should stop targeting ANSI I think. Target Unicode instead. Stop using the TEXT() macro and use the L prefix for your string literals. And stop casting string types. That (LPCSTR) cast is asking for trouble. When you cast like that you tell the compiler that you know better than it does. And usually that is not the case.