0

Question:

In my program, I want to get the content of a specific edit box (or input box, text box...) of another process. For example, when the cursor is in the first column, "TextBox1" should be returned. enter image description here

What I have tried

  1. Use GetDlgItem and GetWindowText
  2. Send WM_GETTEXT message to the window, which was suggested in a Q&A in from the august 2001 issue of MSDN Magazine and a stackoverflow question
char currentContext[256];
SendMessage(cfg.mainWnd, 
WM_GETTEXT, sizeof(currentContext) / sizeof(currentContext[0]), (LPARAM)currentContext);  
// cfg.mainWnd is the window which edit boxes lie in.
  1. EnumChildWindows and callback
HWND GetInputWindow()
{
    HWND activeWindow = GetForegroundWindow();
    // lpdwProcessId cannot be null...
    DWORD a = 1;
    LPDWORD lpdwProcessId = &a;
    auto threadId = GetWindowThreadProcessId(activeWindow, lpdwProcessId);
    std::cout << "ThreadId: " << threadId << " ProcessId: " << *lpdwProcessId << "\n";
    return activeWindow;
}

BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
    std::cout << "Being called.\n";
    char temp1[256];
    char temp2[256];
    GetClassNameA(hWnd, temp1, 255);
    if (!strcmp(temp1, "Edit"))
    {
        SendMessage(hWnd, WM_GETTEXT, sizeof(temp2) / sizeof(char), (LPARAM)temp2);
        return false;
    }
    printf("text: %s", temp2);
    MessageBox(NULL, temp2, L"test", MB_OK);
    return true;
}
// ...
HWND activeWindow = GetInputWindow();
EnumChildWindows(activeWindow, EnumChildProc, 0);

All the methods above just get "MainWindow", i.e. the title of the window in the example above.

Appendix

I used spy++ to monitor the window in the example. Just one window was catched. enter image description here

DerekLu
  • 79
  • 2
  • 7
  • 2
    Have you ran Spy++ to confirm that those textboxes have an associated HWND? A lot of modern apps do a custom draw of all their controls (hence, no child HWND). – selbie Apr 28 '21 at 01:31
  • And just by looking at the screenshot provided, those fields do not look like standard Win32 EDIT controls. – Remy Lebeau Apr 28 '21 at 01:33
  • 2
    And if not, you can probably switch to using the Windows Accessibility APIs to read that data, assuming the developers coded it to be accessible from screen readers. – selbie Apr 28 '21 at 01:34
  • @selbie. Thanks for your reply. I have used spy++. The window in the example doesn't have child HWND (you see it in the newly added appendix in my post). I will check the Windows Accessibility APIs. – DerekLu Apr 28 '21 at 01:41
  • @RemyLebeau. The example is generated from .net framework – DerekLu Apr 28 '21 at 01:42
  • UIAutomation is the library you need – David Heffernan Apr 28 '21 at 06:19
  • [UI Automation](https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32). – IInspectable Apr 28 '21 at 06:20

0 Answers0