-1

I'm trying to find another (MFC) program's MessageBox using FindWindow/FindWindowEx in WindowsAPI. My code is infinite looping. How can I find it?

I'm using this code:

        case 17: // target Program's Create Button

            // Click
            SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
            SendMessage(hWnd, WM.LBUTTONUP, 0, null);
            // -> Show MessageBox in Display
            // my program is stop. What's the happen ?

            /******** here is Problem Code. (infinite loop) ********/
            int main = FindWindow(null, "Create Connecter"); // Parent
            int finish = 0;
            do {
                finish = FindWindowEx(main, 0, null, "Encode"); // MessageBox
                textBox1.AppendText("Wating....");
                System.Threading.Thread.Sleep(100);
            } while (finish == 0);
            textBox1.AppendText("Find MessageBox !!");
            /********************************************************/

            // OK Button Click in MessageBox
            hWnd = FindWindowEx(finish, 0, "Button", "OK");
            SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
            SendMessage(hWnd, WM.LBUTTONUP, 0, null);

That is:

  1. Target Program's Create Button Click -> appeared MessageBox in target Program
  2. I'm trying to find target program's MessageBox
  3. It's infinite looping
  4. Click on OK Button by my mouse -> continue Next step.

Somebody help me... I want go to bed.

  • if you use winspy++, does the window have a context handle? some windows aren't really in the tree depending on how they are created. – Kevin Cook Jun 26 '14 at 17:42
  • how about not starting a loop if main == 0? handling the case when finish = 0 which presumably should not happen if main != 0? – Alex K. Jun 26 '14 at 17:43
  • Your pinvoke declaration(s) are wrong. You never checked that FindWindow() actually succeeded. – Hans Passant Jun 26 '14 at 17:48
  • edit my question. sorry. main is (target Program's Create Button)'s parent. it is allways operation. – Dynamic GT Jun 26 '14 at 17:48
  • where is wrong in my code ? i cant understad.. – Dynamic GT Jun 26 '14 at 17:54
  • Have you verified with Spy++ that main is in fact the parent of the messagebox? For that matter, have you verified that the first FindWindow call actually returns a valid hwnd? 'connecter' looks really suspicious. – Eric Brown Jun 27 '14 at 03:13
  • Lord, no, don't! Register a [WinEvent](http://msdn.microsoft.com/en-us/library/windows/desktop/dd373889.aspx) hook for [`EVENT_SYSTEM_DIALOGSTART` (or `EVENT_OBJECT_CREATE`)](http://msdn.microsoft.com/en-us/library/windows/desktop/dd318066.aspx) and use the `IAccessbile` interface passed to your callback to automate the UI. – IInspectable Jun 27 '14 at 08:05
  • IInspectable// okok~ I'm update my code. I'm not using TextBox. now, using Console. :) – Dynamic GT Jun 27 '14 at 16:52
  • FindWindow and FindWindowEx return HWND, not int. – Werner Henze Jul 04 '14 at 11:09

1 Answers1

0

I'm resolved a problem myself.

using this code:

    case 17: // target Program's Create Button

        // Click : SendMessage -> PostMessage
        PostMessage(hWnd, WM.LBUTTONDOWN, 0, null);
        PostMessage(hWnd, WM.LBUTTONUP, 0, null);
        // used SendMessage : my program is stop
        // useing PostMessage : no problem 

        /******** here is Problem Code. (infinite loop) ********/
        int main = FindWindow(null, "Create Connecter"); // Parent
        int finish = 0;
        do {
            finish = FindWindowEx(main, 0, null, "Encode"); // MessageBox
            textBox1.AppendText("Wating....");
            System.Threading.Thread.Sleep(100);
        } while (finish == 0 || main == finish);
        textBox1.AppendText("Find MessageBox !!");
        /********************************************************/

        // OK Button Click in MessageBox
        hWnd = FindWindowEx(finish, 0, "Button", "OK");
        SendMessage(hWnd, WM.LBUTTONDOWN, 0, null);
        SendMessage(hWnd, WM.LBUTTONUP, 0, null);