How to get control text?

I want to get control text. but this code return parent class name.

const wchar_t* textInput = L"Login";   

HWND btnHandle = CreateWindowEx(0, L"BUTTON", textInput, WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                 mCoordinate.x, mCoordinate.y,,, parentHandle, NULL, 
                 (HINSTANCE)GetWindowLongPtr(parentHandle, GWLP_HINSTANCE), NULL);

wchar_t* textOutput;
int length = GetWindowTextLengthW(btnHandle);
GetWindowText(btnHandle, textOutput, length);

MessageBox(NULL, textOutput, L"Window Text", MB_OK);

1 answer

  • answered 2021-10-12 18:16 IInspectable

    As documented, the lpString argument to GetWindowTextW is:

    The buffer that will receive the text.

    The API does not provide that buffer for you. Instead, you will have to pass it in, as illustrated here:

    size_t length{ GetWindowTextLengthW(btnHandle) };
    // Allocates a buffer for `length` characters plus a NUL terminator
    std::wstring text(length, L'\0');
    // The API promises to write a NUL terminator into the final character
    // so it is safe to lie about the length
    length = GetWindowTextW(btnHandle,, text.size() + 1);
    // Resize in case we got less than promised
    MessageBoxW(NULL, text.c_str(), L"Window Text", MB_OK);

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum