2011-09-22 33 views
2

我試圖將參數發送到已經在處理器中的應用程序。我正在使用Mutex來查找應用程序是否正在運行或尚未運行。我需要發送任何命令行參數,並將該文本添加到列表框中。但參數正在進入,但值不會被添加到列表框。應用程序的名稱是「爲MyApplication」和增加值,以列表框的參數()發送參數到C#中的正在運行的應用程序

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; 
        } 
       } 
      } 
     } 
    } 
+3

可能重複:http://stackoverflow.com/questions/3793997/pass-arguments-to-running-application –

回答

3

最簡單,對我來說是最可靠的方法來發送其他應用程序的消息功能......是使用WM_COPY事件。你可以通過一些老派的API調用來獲得這一點。

在Windows 7中仍然有效我們在最近的應用程序中實現了同樣的功能,它可以在所有的Windows平臺上完美地工作。 (測試回到Windows XP,但在Windows 98中使用相同的API)

這是一個codeproject的鏈接。

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

在應用基本上寄存器一個窗口,併發送消息到該窗口。然後,您可以將其過濾到應用程序。

很酷,很有效率。使用小工具,您可以使無限量的應用程序實例相互通信。

+0

喜歡你的版本;) –

+0

+1,酷解決方案 –

相關問題