How to Clicked ok button of messagebox pops up on click the xyz application from my c# application

denesh neupane 121 Reputation points
2022-01-20T02:55:59.607+00:00

i am trying to click the ok button of messagebox pops up when i click the button of xyz application from my c# application. i have tried the following code so that the button of xyz is clicked but C# application freeze after pops up message box is appeared. i have created two button button1- to click the button of xyz application button2- to click the ok button of message box.

//button1

 IntPtr maindHwnd = FindWindow(null,"Inspector");
    if (maindHwnd != IntPtr.Zero)
     {
         IntPtr panel = FindWindowEx(maindHwnd, IntPtr.Zero, "MDIClient", null);
         IntPtr panel1 = FindWindowEx(panel, IntPtr.Zero, "TAveForm", null);
         IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "TPanel", "Panel5");
         IntPtr panel3 = FindWindowEx(panel2, IntPtr.Zero, "TPanel", null);
         IntPtr childHwnd = FindWindowEx(panel3, IntPtr.Zero, "TBitBtn", "Save");
      if (childHwnd != IntPtr.Zero)
       {
          SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
       }
   }
        

//button2

IntPtr hWnd = FindWindow(null, "Error");
        if (hWnd != IntPtr.Zero)
        {
            IntPtr childHwnd = FindWindowEx(hWnd, IntPtr.Zero, "Button", "Ok");   
            if (childHwnd != IntPtr.Zero)
            {
                SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);     
            }
       }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,320 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 40,856 Reputation points
    2022-01-20T09:41:20.673+00:00

    When SendMessage sends the BM_CLICK message to the button in a different process it will block until that other process has returned from handling the message. If the button click in the receiving process opens a message box that enters a modal loop then the window procedure that received the BM_CLICK message will not return from handling BM_CLICK until the message box is dismissed. So while the message box is active the sending process is still blocking inside its call to SendMessage. Consequently, after a short time Windows will see the sending process as "Not Responding" since it has stopped handling window messages.

    If the sending process uses a function that returns immediately like SendNotifyMessage or SendMessageCallback then the problem will be avoided. The sender can also use SendMessageTimeout to avoid blocking indefinitely.

    0 comments No comments