Reading from a text field in another application's window

后端 未结 7 1269
逝去的感伤
逝去的感伤 2020-11-30 23:16

Is there a way for a Windows application to access another applications data, more specifically a text input field in GUI, and grab the text there for processing in our own

7条回答
  •  一整个雨季
    2020-11-30 23:45

    OK, I have somewhat figured this out.

    The starting point is now knowing the window handle exactly, we only know partial window title, so first thing to do is find that main window:

    ...
    EnumWindows((WNDENUMPROC)on_enumwindow_cb, 0);
    ...
    

    which enumerates through all the windows on desktop. It makes a callback with each of these window handles:

    BOOL CALLBACK on_enumwindow_cb(HWND hwndWindow, LPARAM lParam) {
        TCHAR wsTitle[2048];
        LRESULT result;
    result = SendMessage(hwndWindow, WM_GETTEXT, (WPARAM) 2048, (LPARAM) wsTitle);
        ...
    

    and by using the wsTitle and little regex magic, we can find the window we want.

    By using the before mentioned Spy++ I could figure out the text edit field class name and use it to find wanted field in the hwndWindow:

    hwndEdit = FindWindowEx(hwndWindow, NULL, L"RichEdit20W", NULL);
    

    and then we can read the text from that field:

    result = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM) 4096, (LPARAM) wsText);
    

    I hope this helps anyone fighting with the same problem!

提交回复
热议问题