Geore Shg
Geore Shg

Reputation: 1329

Get text from an edit control

I tried this:

int editlength;
int buttonid = 3324; // id to button, the numbers dont mean anything
int editid = 5652; // id to edit

LPTSTR  edittxt;

HWND button; // created in wWinmain as a button
HWND edit; // created in wWinMain as an edit control

// LRESULT CALLBACK WindowProc

switch(uMsg)
{
    case WM_COMMAND:
        if(wParam == buttonid)
        {
            filedit = GetDlgItem(hwnd, editid); // I tried with and without this
            editlength = GetWindowTextLength(filedit);
            GetWindowText(filedit, edittxt, editlength);

            MessageBox(hwnd, edittxt, L"edit text", 0);
        }
        break;
}

But I get don't see any text in the message box.

Upvotes: 8

Views: 33287

Answers (2)

Jonathan Wood
Jonathan Wood

Reputation: 67175

The last argument to GetWindowText() is the size of your buffer. Since you set it to the length of the string, you are telling the function that your buffer is too small because there's no room for the null terminator. And nothing gets copied.

In addition, you must already allocate the buffer to hold the copy of the text. What does edittxt point to? I don't even see where you initialize it.

Correct usage would look something like this:

TCHAR buff[1024];
GetWindowText(hWndCtrl, buff, 1024);

Upvotes: 19

Jim Morris
Jim Morris

Reputation: 2890

edittxt needs to be a pointer to a buffer that gets the text.. so try this...

char txt[1024];
....
GetWindowText(filedit, txt, sizeof(txt));

You may have to adjust for unicode.. sorry its been a while since I did raw win32.

Upvotes: 5

Related Questions