Dynamically getting text from a textbox

461 views Asked by At

I'm trying to get text from other window dynamically( if I write something in a textfield of that window and then launch my program, I must see what I wrote ).So if I use getWindowText, it gives me a static initialized textbox.So that's the problem.It's similar ,what spy++ does. Here's the code example what I done:

#include <Windows.h>
#include <vector>
#include <iostream>
#include <string>
#include <conio.h> 
using namespace std;

int main() 
{
HWND hWnd;
MSG msg;
vector<HWND> a;
hWnd = FindWindow( NULL, "SomeList" );
vector<string> phrases;
char p[100];
if( !hWnd )
{
    cout << "Window hasn't been found " << endl;
    _getch();
    exit( 1 );
}

hWnd = GetWindow(hWnd, GW_CHILD);
while (hWnd !=0)
{
     hWnd = GetWindow(hWnd, GW_HWNDNEXT);
     GetClassName( hWnd, p, 10 );
     string k( p );
     if( k == "Edit" )
         a.push_back( hWnd );
     GetWindowText(hWnd,p,100);
      cout << p << endl;
}
phrases.resize( a.size() );

for( auto i = a.begin();i != a.end();i++ )
{
    int index = 0;
    GetWindowText( *i,p, 10 );
    string n( p );
    if( n.size() != 0 )
    {
        phrases[index] =  n;
        index++;
    }
}
_getch();
return 0;
}
1

There are 1 answers

4
manuell On BEST ANSWER

GetWindowText documentation:

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

Example:

HWND hWndEdit;

[....]

char szText[ 128 ] = { 0 };
int cbCopied = SendMessage( hWndEdit, WM_GETTEXT, (WPARAM)sizeof( szText ),
                            (LPARAM)szText );