winapi

Finding handle for a button in the window of another program


I need some help. I wrote my questions at the end and will first explain what exactly my code is supposed to do:

I am making a program that will communicate with other programs. What I need my software to do is be able to click on a button on another program, and I believe the appropriate code to do this would be:

SendMessage(hWnd, Msg, wParam, lParam);

with

Msg = B_Click
wParam = 0;
lParam = 0;

however I am not sure how to get the hWnd which is the handle of a specific button on a specific window of another program that is running at the same time. I have read somewhere that I could possibly do the following:

HWND buttonHandle = FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow);

where:

HWND hwndParent = A handle to the parent window whose child windows are to be searched
HWND hwndChildAfter = if its null child windows are of the parent window are looked through
LPCTSTR lpszClass = (NOT SURE WHAT THIS IS)
LPCTSTR lpszWindow = (NOT SURE WHAT THIS IS)

I have a few issues with the FindWindowEX() function however.

Question 1: The window I am looking at has various buttons, so how would the function know which one of the 3 i am looking at?

Question 2: What are the lpszClass and lpszWindow variables and how might I get them?

Question 3: Is this even the right approach? If it's not, please point me to the right direction!


Solution

  • You don't need the handle of the button, you need the handle of its parent window. The button sends BN_CLICKED to its parent window. Look up the button's ID with spy++. Then use EnumChildWindows of the parent to look at all the child windows. For each child use GetWindowLong with GWL_ID to check its ID.