Problems getting text from control on another wind

2019-07-20 06:56发布

I am trying to get the text on the textbox of a notepad window using the SendMessage from the win32 api. I find the window handle first, and I grab the text with SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer). For some reason, even though it can tell me the correct length of the text, the program only returns 1 character of the notepad text, even when I have 1024 as my buffersize which it should be returning. I looked at examples I found, and the way I do it is the same as the examples. I have no clue why this is happening, can someone help me out or point out my error?

#include <Windows.h>
#include <iostream>

int main()
{
    printf("finding notepad window\n");
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if(NULL != hwndNotepad)
    {
        printf("Find edit control window\n");
        HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL);

        if(NULL != hwndEdit)
        {
            printf("- get text length\n");
            int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);
            printf("textlength: %d\n", textLen);
            if(0 < textLen)
            {
                const int bufferSize = 1024;
                char textBuffer[bufferSize] = "";
                SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
                printf("getting text:\n");
                printf("%s\n", textBuffer);
            }
            else
            {
                printf("its empty\n");
            }
        }
        else
        {
            printf("I cant find this control\n");
        }
    }
    else
    {
        printf("I cant find notepad window. \n");
    }
    return 0;
}

Screenshot: http://i.imgur.com/bUhVqlq.png

2条回答
看我几分像从前
2楼-- · 2019-07-20 07:53

This is probably due to the fact that NotePad is using UNICODE. Try this

int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
printf("Copied %d chars.\n", copied);

to find out how many character your callee thinks it copied. Try the following to print UNICODE text:

const int bufferSize = 1024;
wchar_t textBuffer[bufferSize] = "";
int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
printf("Copied %d chars.\n", copied);
printf("getting text:\n");
wprintf(L"%ls \n", textBuffer);

If you are using Visual Studio, it might be worth a try to go into your project options (Right click project -> Configuration Properties -> General -> Character Set) and set it to ANSI ("Not Set").

查看更多
姐就是有狂的资本
3楼-- · 2019-07-20 07:54

No need to change the project settings. Instead of using char you can use TCHAR.

#include <Windows.h>
#include <iostream>

int main()
{
    printf("finding notepad window\n");
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if(NULL != hwndNotepad)
    {
        printf("Find edit control window\n");
        HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL);

        if(NULL != hwndEdit)
        {
            printf("- get text length\n");
            int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);
            printf("textlength: %d\n", textLen);
            if(0 < textLen)
            {
                const int bufferSize = 1024;
                TCHAR textBuffer[bufferSize];
                SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
                printf("getting text:\n");
                printf("%s\n", textBuffer);
            }
            else
            {
                printf("its empty\n");
            }
        }
        else
        {
            printf("I cant find this control\n");
        }
    }
    else
    {
        printf("I cant find notepad window. \n");
    }
    return 0;
}
查看更多
登录 后发表回答