Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from an edit control

Tags:

winapi

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.

like image 218
Geore Shg Avatar asked Apr 16 '11 23:04

Geore Shg


2 Answers

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);
like image 135
Jonathan Wood Avatar answered Sep 27 '22 19:09

Jonathan Wood


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.

like image 41
Jim Morris Avatar answered Sep 27 '22 20:09

Jim Morris