none
Detecting Message Boxes and close them automaticaly. RRS feed

  • Question

  •  

    Hi guys, I need some tips to detect message boxes from another applications.

     

    Someone knows how can I detect message boxes of some application? I want to know when the message box was opened and get the information wrote (ctrl+C). If possible I'd like to use windows API to do that.

     

    For example: the message show up from some windows application, my app must detect this, get what is wrote in message box and close it.

     

    I want to do this with C#.

     

    Please help me!!!

    Saturday, January 12, 2008 6:29 PM

Answers

  • Hi, Fabio

    You can use the FindWindow() API to get the handle of the MessageBox, use FindWindowEx() API to get the handle of the Text window, use GetWindowTextLength() API to get the text length in the MessageBox, use GetWindowText() API get the text in the MessageBox, and use SendMessage() method to send the WM_CLOSE message to the MessageBox to close it. If you want to detect all the MessageBox automatically, you can run a timer to do these stuff, in the following sample I run a timer every one second to detect the MessageBox, see the details below:

    Code Block



     

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr FindWindowEx(IntPtr hwndParent,

                IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

            static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

     

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

            static extern int GetWindowTextLength(IntPtr hWnd);

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

     

            private const int WM_CLOSE = 0x10;

     

            private void Form1_Load(object sender, EventArgs e)

            {

                Timer timer = new Timer();

                timer.Interval = 1000;//detect the MessageBox every seconds

                timer.Tick += new EventHandler(timer_Tick);

                timer.Start();

            }

     

            void timer_Tick(object sender, EventArgs e)

            {

                //Get the MessageBox handle

                IntPtr handle = FindWindow("#32770", null);

                this.richTextBox1.AppendText("Hanlde1: " + handle.ToString() + "\n");

     

                //Get the Text window handle

                IntPtr txtHandle = FindWindowEx(handle, IntPtr.Zero, "Static", null);

                this.richTextBox1.AppendText("\ttext handle: " + txtHandle.ToString() + "\n");

                int len = GetWindowTextLength(txtHandle);

                StringBuilder sb = new StringBuilder();

     

                //Get the text

                GetWindowText(txtHandle, sb, len + 1);

                this.richTextBox1.AppendText("\ttext: " + sb.ToString() + "\n\n");

                this.richTextBox1.ScrollToCaret();

     

                //close the messagebox

                SendMessage(new HandleRef(null, handle), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

            }

        }

     

     



    Best Regards,
    Zhi-xin Ye.

    Friday, January 18, 2008 4:12 AM

All replies

  • Hi, Fabio

    You can use the FindWindow() API to get the handle of the MessageBox, use FindWindowEx() API to get the handle of the Text window, use GetWindowTextLength() API to get the text length in the MessageBox, use GetWindowText() API get the text in the MessageBox, and use SendMessage() method to send the WM_CLOSE message to the MessageBox to close it. If you want to detect all the MessageBox automatically, you can run a timer to do these stuff, in the following sample I run a timer every one second to detect the MessageBox, see the details below:

    Code Block



     

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr FindWindowEx(IntPtr hwndParent,

                IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

            static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

     

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

            static extern int GetWindowTextLength(IntPtr hWnd);

     

            [DllImport("user32.dll", SetLastError = true)]

            static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

     

            private const int WM_CLOSE = 0x10;

     

            private void Form1_Load(object sender, EventArgs e)

            {

                Timer timer = new Timer();

                timer.Interval = 1000;//detect the MessageBox every seconds

                timer.Tick += new EventHandler(timer_Tick);

                timer.Start();

            }

     

            void timer_Tick(object sender, EventArgs e)

            {

                //Get the MessageBox handle

                IntPtr handle = FindWindow("#32770", null);

                this.richTextBox1.AppendText("Hanlde1: " + handle.ToString() + "\n");

     

                //Get the Text window handle

                IntPtr txtHandle = FindWindowEx(handle, IntPtr.Zero, "Static", null);

                this.richTextBox1.AppendText("\ttext handle: " + txtHandle.ToString() + "\n");

                int len = GetWindowTextLength(txtHandle);

                StringBuilder sb = new StringBuilder();

     

                //Get the text

                GetWindowText(txtHandle, sb, len + 1);

                this.richTextBox1.AppendText("\ttext: " + sb.ToString() + "\n\n");

                this.richTextBox1.ScrollToCaret();

     

                //close the messagebox

                SendMessage(new HandleRef(null, handle), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

            }

        }

     

     



    Best Regards,
    Zhi-xin Ye.

    Friday, January 18, 2008 4:12 AM
  •  

      Zhi-Xin Ye,

     

    Thank you so much!!! You help me a lot!!! This is incredible.

     

     

    Saturday, January 19, 2008 7:50 PM