开发者

Send parameters to a running application in C#

开发者 https://www.devze.com 2023-04-07 01:23 出处:网络
I am trying to send pa开发者_运维百科rameters to an application which is already in the processor. I am using Mutex to find if the application is running or not already. I need to send any command lin

I am trying to send pa开发者_运维百科rameters to an application which is already in the processor. I am using Mutex to find if the application is running or not already. I need to send any command line parameter and that text is added to the listbox. But the parameter is going in but the values are not getting added to the listbox. Application's name is "MYAPPLICATION" and the function which adds the value to listbox is parameters()

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [DllImport("user32.dll")]   
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 Frm1 = new Form1();
        bool createdNew = true;
        using (Mutex mutex = new Mutex(true, "MYAPPLICATION", out createdNew)) //Finding if application is running or not
        {
            if (createdNew)
            {
                //foreach (string abc in Environment.GetCommandLineArgs())
                //{
                //    MessageBox.Show(abc);
                //}
                Application.Run(Frm1);                                
            }
            else
            {
                Process current = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        Frm1.parameters(Environment.GetCommandLineArgs());
                        break;
                    }
                }
            }
        }
    }


the easiest and for me the most reliable way to send other applications a message... is to use the WM_COPY event. You can get to this with some old school API calls.

Still valid in Windows 7 We implemented this same thing in a recent application and it works flawlessly on all windows platforms. (tested back to windows xp, but have use the same api in windows 98)

Here is a link to codeproject.

http://www.codeproject.com/KB/cs/ipc_wmcopy.aspx

Essentially register a window in the applications and send messages to that window. Then you can filter it down to the applications.

Pretty cool, and quite efficient. With a little tooling you can make an unlimited amount of application instances communicate with each other.


A message queue is a common pattern to communicate between processes. Volure's version of that is cool. A more common approach is to use MSMQ (Microsoft Message Queueing).

Check it out here

http://msdn.microsoft.com/en-us/library/ms978430.aspx

http://en.wikipedia.org/wiki/Microsoft_Message_Queuing

http://blog.goyello.com/2009/09/08/why-msmq-is-excelent-for-net-developers/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号