Aaron K
Aaron K

Reputation: 437

GetWindowText() in win32 c++ not working

Doing a project where I am retrieving the text from an edittext control in my window, and the GetWindowText() function is failing to read into the buffer and returning 0, however calling a SetWindowText() to the same control is working perfectly, both when the window is initialized and when other events occur (checkboxes being clicked etc.) Not sure what is causing the failure to read the data, relevant code posted below:

#define IDC_SOPRANO_H 1788
INT_PTR CALLBACK    Rules(HWND, UINT, WPARAM, LPARAM);
LPSTR SOP_HIGH_NOTE = "A#4";
INT_PTR CALLBACK Rules(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND SOP_HIGH_NOTE_HWND = GetDlgItem(hDlg, IDC_SOPRANO_H);
    switch (message)
    {
    case WM_INITDIALOG:
        SetWindowText(SOP_HIGH_NOTE_HWND, SOP_HIGH_NOTE);
        return (INT_PTR)TRUE;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDOK:
            GetWindowText(SOP_HIGH_NOTE_HWND, SOP_HIGH_NOTE, 4);
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
}

Upvotes: 0

Views: 1948

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

SOP_HIGH_NOTE is a pointer, not an allocated buffer. You are declaring it to initially point at a string literal, which exists in the process's read-only data. Passing that pointer to GetWindowText() will try to write to that block of read-only memory.

Try this instead:

TCHAR SOP_HIGH_NOTE[] = TEXT("A#4");

That declares an actual read/write buffer in memory and pre-fills it with the contents of the string literal, rather than pointing at the memory address of the literal itself.

Upvotes: 2

Related Questions