views:

37

answers:

1

Hello ,

I have a program that needs to send the BM_CLICK message to another applications button. I can get the parent window handle but when I try to get the button handle if always returns 0

I got the button caption name and button type from Spy++ it seems right but I know I must of gotten something wrong. below is my code

 public const Int BM_CLICK = 0x00F5;

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);



private void button1_Click(object sender, EventArgs e)
{
    Process[] processes = Process.GetProcessesByName("QSXer");

    foreach (Process p in processes)
    {
        ////the Button's Caption is "Send" and it is a "Button".  
        IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", "Send");
       //ButtonHandle is always zero thats where I think the problem is 
    SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

    }

}

Spy screen shot

alt text

A: 

Try to pass null for the window text and instead try to find any button:

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", null);

After that you can use the second parameter and a new call to get the next button handle a couple more times.

Also could you please try checking Marshal.GetLastWin32Error to see what the error result is?

Brian R. Bondy
hi Brian,Unless I misunderstand what your asking I believe that the class name always has to be a string no?
Mike
Corrected my answer.
Brian R. Bondy
Hi Brian,Ok give that a try and still nothing :-)
Mike
Mike
What does Marshal.GetLastWin32Error return? Also I changed my answer above, please try what I suggested again.
Brian R. Bondy
I capture the GetLastWin32Error and it comes up with 0 MessageBox.Show(Marshal.GetLastWin32Error().ToString());
Mike