Frage

I want to read the text which the user has typed in a Edit control. after entering the text and pressing the button, I want to get the text and add it as an item into a comboBox. this is what I am doing in WM_COMMAND of the parent dialog:

case WM_COMMAND:

    if(HIWORD(wParam) == BN_CLICKED)
    {
        if ((HWND)lParam == Button[0])
        {



                int len = GetWindowTextLengthW(Button[2]) + 1;
                GetWindowTextW(Button[2], text, len);
                SendMessage(Button[1],(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) text);

                }
        }


    return 0;

but things goes wrong, sometime I get NULL in the "text" variable, sometimes just the first character of the string the user has entered and sometime weird ASCII like characters. what am I doing wron? any ideas ?

War es hilfreich?

Lösung

You need to allocate memory for the string. Here's how one would expect to do it in C++03:

std::vector<wchar_t> str(len);
GetWindowTextW(Button[2], &str[0], str.size());
SendMessageW(Button[1], CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(&str[0]));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top