Control the application using C# application.

denesh neupane 121 Reputation points
2022-03-01T04:50:51.667+00:00

i want to clicked the save button of another application from my c# application.if there is data in that application then save dialogbox will appear while in absence of data message box will appear.After i want to give the filename as date and time similarly i want to click the ok button for messagebox.

i did it using 3 button save,enter filename button and ok button but the problem is when i clicked the save button window is changed to either save dialogbox or message box after that my c# application freezed. if i restart c# application it will work.how to solve this?
if possible i want to do it using single button.

 [DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]static public extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
 [DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
 private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
 private static extern int SendNotifyMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
 [DllImport("User32.dll", CharSet = CharSet.Auto)]
 private static extern int SetForegroundWindow(IntPtr points);
 [DllImport("user32.dll")]
 private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//save button
private void saveBtn_Click(object sender, EventArgs e)
  {
    IntPtr maindHwnd = FindWindow(null, "app1");
    IntPtr maindHwnd1 = FindWindow(null, "Error");
    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);
     }
   }
  }
//click messagebox
private void button4_Click(object sender, EventArgs e)
  {
    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); 
        }
      else
      {
        textBox3.BackColor = Color.Yellow;
        textBox3.Text = "error;
                }
         }
         else
         {
          textBox3.BackColor = Color.Yellow;
        textBox3.Text = "hmd is zero";
         }
        }
 //save dialogbox

String textBox = DateTime.Now.ToString("yyyyMMdd_HH-mm-ss");
  IntPtr maindHwnd = FindWindow(null, "save");
    IntPtr hWnd = FindWindow(null, "Error");
   if (maindHwnd != IntPtr.Zero)
    {
   {
     IntPtr panel = FindWindowEx(maindHwnd, IntPtr.Zero, "ComboBoxEx32", null);
      IntPtr panel1 = FindWindowEx(panel, IntPtr.Zero, "ComboBox", null);
      IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "Edit", null);
       if (panel2 != IntPtr.Zero)
        {

          SendKeys.Send(textBox);

         }

                }
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2022-03-02T11:16:26.4+00:00

    Following example shows use of SendMessageTimeout and SendMessageCallback to click a button in a different application that invokes a modal dialog without causing the sending application to block.

    Before using SendMessageTimeout or SendMessageCallback -

    179283-nofreeze.png

    After -
    179291-msgboxclicked.png

    Sample code -

            private void OnClickTimeout(object sender, EventArgs e)  
            {  
                IntPtr hwnd = Win32.FindWindow("#32770", "Target");  
                if (hwnd != IntPtr.Zero)  
                {  
                    IntPtr hBtn = Win32.FindWindowEx(hwnd, IntPtr.Zero, "BUTTON", "MsgBox");  
                    if (hBtn != IntPtr.Zero)  
                    {  
                        UIntPtr uiResult;  
                        Win32.SetForegroundWindow(hwnd);  
                        // use 100 millisecond timeout  
                        Win32.SendMessageTimeout(hBtn, Win32.BM_CLICK, UIntPtr.Zero, IntPtr.Zero,  
                            Win32.SendMessageTimeoutFlags.SMTO_NORMAL, 100, out uiResult);  
                    }  
                }  
            }  
      
            private void OnClickCallback(object sender, EventArgs e)  
            {  
                IntPtr hwnd = Win32.FindWindow("#32770", "Target");  
                if (hwnd != IntPtr.Zero)  
                {  
                    IntPtr hBtn = Win32.FindWindowEx(hwnd, IntPtr.Zero, "BUTTON", "MsgBox");  
                    if (hBtn != IntPtr.Zero)  
                    {  
                        Win32.SetForegroundWindow(hwnd);  
                        Win32.SendMessageCallback(hBtn, Win32.BM_CLICK, UIntPtr.Zero, IntPtr.Zero, smcallback, UIntPtr.Zero);  
                    }  
                }  
            }  
            private void smcallback(IntPtr hwnd, uint uMsg, UIntPtr dwData, IntPtr lResult)  
            {  
                Debug.Print("Callback received");  
            }  
      
            private void OnFreezer(object sender, EventArgs e)  
            {  
                IntPtr hwnd = Win32.FindWindow("#32770", "Target");  
                if (hwnd != IntPtr.Zero)  
                {  
                    IntPtr hBtn = Win32.FindWindowEx(hwnd, IntPtr.Zero, "BUTTON", "MsgBox");  
                    if (hBtn != IntPtr.Zero)  
                    {  
                        Win32.SetForegroundWindow(hwnd);  
                        Win32.SendMessage(hBtn, Win32.BM_CLICK, UIntPtr.Zero, IntPtr.Zero);  
                    }  
                }  
            }  
        }  
      
        internal class Win32  
        {  
            [Flags]  
            internal enum SendMessageTimeoutFlags : uint  
            {  
                SMTO_NORMAL = 0x0,  
                SMTO_BLOCK = 0x1,  
                SMTO_ABORTIFHUNG = 0x2,  
                SMTO_NOTIMEOUTIFNOTHUNG = 0x8,  
                SMTO_ERRORONEXIT = 0x20  
            }  
      
            internal const uint BM_CLICK = 0xF5;  
      
            [UnmanagedFunctionPointer(CallingConvention.StdCall)]  
            internal delegate void SendMessageDelegate(IntPtr hWnd, uint uMsg, UIntPtr dwData, IntPtr lResult);  
      
            [DllImport("User32.dll", CallingConvention =CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern IntPtr FindWindow(string strclass, string strname);  
      
             [DllImport("User32.dll", CallingConvention =CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern IntPtr FindWindowEx(IntPtr hwnd, IntPtr hwndAfter, string strclass, string strname);  
      
            [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern bool SetForegroundWindow(IntPtr hwnd);  
      
            [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern bool SendMessage(IntPtr hwnd, uint msg, UIntPtr wParam, IntPtr lParam);  
      
            [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern bool SendMessageTimeout(IntPtr hwnd, uint msg, UIntPtr wParam, IntPtr lParam,  
                SendMessageTimeoutFlags fuflags, uint uTimeout, out UIntPtr result);  
      
            [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]  
            internal static extern bool SendMessageCallback(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam,  
                SendMessageDelegate lpCallBack, UIntPtr dwData);  
        }  
    }  
      
    

2 additional answers

Sort by: Most helpful
  1. RLWA32 49,536 Reputation points
    2022-03-01T14:56:14.857+00:00

    Hmm, this looks like the same SendMessage problem for which I posted an answer at how-to-clicked-ok-button-of-messagebox-pops-up-on.html

    You should look into using UI Automation instead of the Windows API functions. See https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview

    1 person found this answer helpful.

  2. Gunjan Singh Khandpur 1 Reputation point
    2022-03-01T14:52:49.973+00:00

    Can you put some Exception Handling using catch block and share the exception you are getting. It will be good if you can share the actual code since the above code doesn't compile.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.