none
How to Click a Button in another Application Programmatically in C#?

    Question

  • I would like to invoke the button click event from one application to another application. Is there any to way to trigger the other application events from the main application. Please help.

     

    Thanks in advance.

    Wednesday, December 27, 2006 5:45 AM

Answers

  • Hi, Prabu

    Normally, what you want to do is inter-process communication.

    Here I give you a sample to show you how it works with Win32 using windows message :

    I let AppA's Button1 to invoke the Button1_click event in AppB

    In AppB I wrote:

            private string msgstr = "interprocess communication";

            private uint msg;

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

            static extern uint RegisterWindowMessage(string lpString);

            private void Form1_Load(object sender, System.EventArgs e)

            {

                msg = RegisterWindowMessage(msgstr);

                if (msg == 0)

                {

                    MessageBox.Show(Marshal.GetLastWin32Error().ToString());

                }

            }

            private void button1_Click(object sender, System.EventArgs e)

            {

                MessageBox.Show("AppB's button is clicked");

            }

            protected override void WndProc(ref Message m)

            {

                if (m.Msg == msg)

                {

                    //MessageBox.Show(msgstr + " from wndproc");

                    button1.PerformClick();

                }

                base.WndProc(ref m);

            }

    And in AppA I wrote:

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

            public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam,

            IntPtr lparam);

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

            static extern uint RegisterWindowMessage(string lpString);

     

            private string msgstr = "interprocess communication";

            private uint msg;

            private const int HWND_BROADCAST = 0xffff;

            private void button1_Click(object sender, System.EventArgs e)

            {

                msg = RegisterWindowMessage(msgstr);

                if (msg == 0)

                {

                    MessageBox.Show(Marshal.GetLastWin32Error().ToString());

                }

                PostMessage(HWND_BROADCAST, msg, IntPtr.Zero, IntPtr.Zero);

            }

    PS. Both should using System.Runtime.InteropServices;

    Actually, this is about win32 programming on platform.

    Otherwise, you can reference to the .NET remoting.

    Some useful url: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/hawkremoting.asp

    http://www.developer.com/net/cplus/article.php/10919_1479761_1

    http://www.springframework.net/doc-1.1-P3/reference/html/remoting-quickstart.html

    --These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

     

    Wednesday, December 27, 2006 8:00 AM
  • Hi, Prabu

    It's about .NET remoting technologies.

    Remoting is a means by which one operating system process, or program, can communicate with another process. The two processes can exist on the same computer or on two computers connected by a LAN or the Internet.

    In any operating system, two paramount goals are security and stability. One way to achieve these goals is to load each executing program into its own process. By design, processes are isolated from each other—the code in one process cannot access the code or data of another process. That design enhances security by preventing one program from snooping around where it does not have access rights, and it enhances stability by ensuring that problems in one program cannot inadvertently corrupt the memory space of another program or of the operating system itself. The .NET Framework provides an additional level of isolation with application domains, which permit two or more programs to run within the same process, maintaining the same level of isolation as if they were in separate processes while minimizing the overhead of multiple processes.
    The .NET Framework provides several method for cross-process communication, collectively called remoting. Web services are probably the best known type of remoting, but they are not your only option. :-)

    Click to see .NET Remoting Overview

    Wednesday, December 27, 2006 6:45 AM

All replies

  • Hi, Prabu

    It's about .NET remoting technologies.

    Remoting is a means by which one operating system process, or program, can communicate with another process. The two processes can exist on the same computer or on two computers connected by a LAN or the Internet.

    In any operating system, two paramount goals are security and stability. One way to achieve these goals is to load each executing program into its own process. By design, processes are isolated from each other—the code in one process cannot access the code or data of another process. That design enhances security by preventing one program from snooping around where it does not have access rights, and it enhances stability by ensuring that problems in one program cannot inadvertently corrupt the memory space of another program or of the operating system itself. The .NET Framework provides an additional level of isolation with application domains, which permit two or more programs to run within the same process, maintaining the same level of isolation as if they were in separate processes while minimizing the overhead of multiple processes.
    The .NET Framework provides several method for cross-process communication, collectively called remoting. Web services are probably the best known type of remoting, but they are not your only option. :-)

    Click to see .NET Remoting Overview

    Wednesday, December 27, 2006 6:45 AM
  • Hi Figo Fei,

     

    Thanks for the brief explanation about Remoting Overview. If possible, could you please provide me a bit of code snippet for triggering the command button’s click event of another application. Both applications have been written in C#.Net. So I think it’s easy to access other applications object.

     

    Thanks in advance.

    Wednesday, December 27, 2006 6:59 AM
  • Hi, Prabu

    In other way, you can use windows message mechanism to communicate between mutiple app in a computer. Which way you prefer depends on yourself.

    Wednesday, December 27, 2006 7:12 AM
  • Hi Figo Fei,

     

    Can you please suggest the better and easy way to handles this problem. Also it would be great if you send some the useful links to know about these concepts.

    Wednesday, December 27, 2006 7:24 AM
  • Hi, Prabu

    Normally, what you want to do is inter-process communication.

    Here I give you a sample to show you how it works with Win32 using windows message :

    I let AppA's Button1 to invoke the Button1_click event in AppB

    In AppB I wrote:

            private string msgstr = "interprocess communication";

            private uint msg;

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

            static extern uint RegisterWindowMessage(string lpString);

            private void Form1_Load(object sender, System.EventArgs e)

            {

                msg = RegisterWindowMessage(msgstr);

                if (msg == 0)

                {

                    MessageBox.Show(Marshal.GetLastWin32Error().ToString());

                }

            }

            private void button1_Click(object sender, System.EventArgs e)

            {

                MessageBox.Show("AppB's button is clicked");

            }

            protected override void WndProc(ref Message m)

            {

                if (m.Msg == msg)

                {

                    //MessageBox.Show(msgstr + " from wndproc");

                    button1.PerformClick();

                }

                base.WndProc(ref m);

            }

    And in AppA I wrote:

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

            public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam,

            IntPtr lparam);

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

            static extern uint RegisterWindowMessage(string lpString);

     

            private string msgstr = "interprocess communication";

            private uint msg;

            private const int HWND_BROADCAST = 0xffff;

            private void button1_Click(object sender, System.EventArgs e)

            {

                msg = RegisterWindowMessage(msgstr);

                if (msg == 0)

                {

                    MessageBox.Show(Marshal.GetLastWin32Error().ToString());

                }

                PostMessage(HWND_BROADCAST, msg, IntPtr.Zero, IntPtr.Zero);

            }

    PS. Both should using System.Runtime.InteropServices;

    Actually, this is about win32 programming on platform.

    Otherwise, you can reference to the .NET remoting.

    Some useful url: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/hawkremoting.asp

    http://www.developer.com/net/cplus/article.php/10919_1479761_1

    http://www.springframework.net/doc-1.1-P3/reference/html/remoting-quickstart.html

    --These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

     

    Wednesday, December 27, 2006 8:00 AM
  • Hi Figo Fei,

     

    I am trying  to ue the same concept you have given for invoking an exe (AppB) which is not visible.(For App B , Notify icon is present in system tray). In AppB in certain conditions, only the notify icon is present, the main window is not visible.

     

    In this case, how do is send a message to AppB? i tried but it is not invoking the AppB function.

     

    Thanks

    Pradeep

    Monday, May 14, 2007 11:30 AM
  • Hi, Pradeep

    The WndProc method recieves the message regardless whether the form is visible or not. But button1.PerformClick() method are not fired when the form is hiden.

    Try to use other method instead of such method can solve the problem. (if you want to raise the button click event, just call the handle delegate: button1_Click(null, null); )

    Thanks

    Tuesday, May 15, 2007 2:50 AM
  • Note in Windows CE, the Microsoft.WindowsCE.Forms assembly has a Message and MessageWindow class which helps send messages without the [DllImport....]

    I don't know if desktop Windows has similar.

    As an example, here is a program which broadcasts a message to switch desktop wallpaper.

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms; // has Message and MessageWindow
    
    using Microsoft.WindowsCE.Forms;
    
    namespace Wallpaper
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        // i hope there is a better way to get these numbers, but I don't know it.
        private const int HWND_BROADCAST = 0xffff;
        public const int WM_SETTINGCHANGE = 0x001A; // I got this from the web
        private const uint SPI_SETDESKWALLPAPER = 20; // and this from web also
    
        private void button1_Click(object sender, EventArgs e)
        {
          // this is the C++ thing we are trying to do: PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETDESKWALLPAPER, 0);
          Message msg = Message.Create((IntPtr) HWND_BROADCAST,
            WM_SETTINGCHANGE, (IntPtr)SPI_SETDESKWALLPAPER, (IntPtr)0);
          MessageWindow.SendMessage(ref msg); 
        }
    
      }
    }
    Friday, July 30, 2010 6:16 PM
  • How would one modify this example to have AppA and AppB on different computers?

     

    Thanks,

    shontay

    Tuesday, October 26, 2010 1:59 AM
  • Hi, Pradeep

    The WndProc method recieves the message regardless whether the form is visible or not. But button1.PerformClick() method are not fired when the form is hiden.

    Try to use other method instead of such method can solve the problem. (if you want to raise the button click event, just call the handle delegate: button1_Click(null, null); )

    Thanks


    I just start to learn it, I've learned a lot.
    Tuesday, February 22, 2011 1:37 AM