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 do not see any text in the message box.

+5
source share
2 answers

The final argument GetWindowText()is the size of your buffer. Since you set it to the length of the string, you tell the function that your buffer is too small because there is no room for a null terminator. And nothing is copied.

In addition, you should already allocate a buffer for storing copies of the text. What does it point to edittxt? I don’t even see where you initialized it.

Proper use will look something like this:

TCHAR buff[1024];
GetWindowText(hWndCtrl, buff, 1024);
+14
source

edittxt should be a pointer to a buffer that receives text .. so try this ...

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

, unicode.. , , raw win32.

+4

All Articles