Hello. How can i use SendMessage to "press" other program's button? I don't know other program's button's ID but there is only one button in that program. I searched MSDN and found something. I tried this piece of code:

HWND hWnd;
hWnd = HWND(FindWindow(NULL, "WindowName"));
SendMessageW(hWnd, BM_CLICK, NULL, NULL);

At MSDN says that wParm and lParm has to be NULL. Can you help me?
Thank you.

Edited 6 Years Ago by lukename: n/a

Hi,

AutoIt

After installing autoit, there is a file named "AutoIt window info". That should help you get the button ID.

Thank you. This seems to be useful. But how can i send BM_CLICK to specific button? Maybe i should add button ID to wParam or lParam?

Here, this code demonstrates sending click to a calculator:
Open a calculator, input any value then executing the code below will reset the value:

int main(int argc, char *argv[])
{
LPARAM ID = 81;
LRESULT res;
HWND hWnd;
HWND hWnd1; //handle to the Button 'C'
hWnd = HWND(FindWindow("SciCalc", "Calculator"));
hWnd1 = HWND(GetDlgItem(hWnd, 81));
res = SendMessage(hWnd1, BM_CLICK, NULL, NULL);
cout << "\nResult: " << res;
getchar();
return 0;
}

Edited 6 Years Ago by mayyubiradar: More info

What headers do i need to include?

Line 8: error C2664: 'void CWnd::GetDlgItem(int,HWND *) const' : cannot convert parameter 1 from 'HWND' to 'int'
Line 8: error C2514: 'HWND' : class has no constructors
Line 9: error C2660: 'CWnd:: SendMessageA' : function does not take 4 arguments

Edited 6 Years Ago by lukename: n/a

HWND hWnd;
    HWND *p_hWnd = &hWnd;
    HWND hButton;
    hWnd = HWND(FindWindow(NULL, "Dialog"));
    hButton = HWND(GetDlgItem(1001, p_hWnd));
    SendMessageW(hButton, BM_CLICK, NULL, NULL);

Line 5: error C2440: '<function-style-cast>' : cannot convert from 'void' to 'HWND'
So how am i supposed to get button handle to HWND variable if GetDlgItem isn't returning anything??? :( any advice?

P.S I'm using MFC. Does that change anything?

Edited 6 Years Ago by lukename: n/a

This question has already been answered. Start a new discussion instead.