Search code examples
c++cmacroswindowhook

C/C++ Hook to access the window's elements of a program


I'm trying to access window's elements of a program, of which I have only the binaries and installed stuff.

For example, I want to click on a certain item of a certain listbox in an automated way.

I'm only interested in accessing one listbox of one window of the program, but this would help me create a very good macro (the list box is full of "expands", it's long, and dynamic).

I thought about using macros like Autohotkey, but quickly considered it a bad idea.

I don't even know how to call this, but i tried searching for "hook" related stuff, without success.

PS: I'm on a 64 bits Windows 7 system, using Code Blocks. Let me know if there is a better IDE for that, like VS, because i can use it too.

EDIT

Ok, so I caught the information of the window using WinSpy++, though I don't really know what to do with it. I'm trying to get the handle of the child window, but am failing.
I'm very noob at this. I reminded of an important detail, I need to get the names of the listbox elements and their positions (1st, 2nd, 3rd), is it possible?

EDIT 2 (Where I am)
Ok, here is where I am so far: I caught the main window handle, but can't get the handle of any of the children. My googling turned up a "GetDlgItem" function, but didn't work. Any ideas?


Solution

  • You may be able to use Spy++ to find the specific class and/or caption for the window (the listbox control) and then target it via FindWindow().

    If not, you'll have to find the app's main window, and traverse its children, looking for the children of the children until you find the listbox window.

    Once you finally have the window handle, you can just PostMessage() the WM_CLICK message to it.

    EDIT 1 (For Meysam)

    I started by googling for Listview window messages. That took me to this MSDN page. Looking down through the list, LVM_GETITEM stands out. It takes an LVITEM structure as an LPARAM.

    In that struct, the iItem member identifies the index. All that you need to know now is an item count, so that you can iterate through each index, and write code like this pseudocode:

    for (int i = 0; i < item_count; i++)
    {
        LVITEM lvi;
        lvi.iItem = i; // probably need to set other pieces of the struct
        LRESULT item = SendMessage(hwnd, LVM_GETITEM, NULL, (LPARAM)lvi);
    }
    

    For how many items (item_count), use LVM_GETITEMCOUNT.

     LRESULT item_count = SendMessage(hwnd, LVM_GETITEMCOUNT, NULL, NULL);