windows 在 C# 中将参数发送到正在运行的应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7522583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send parameters to a running application in C#
提问by Vivek Sampara
I am trying to send parameters 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()
我正在尝试将参数发送到已经在处理器中的应用程序。我正在使用互斥锁来查找应用程序是否正在运行。我需要发送任何命令行参数,并将该文本添加到列表框中。但是参数正在输入,但值没有添加到列表框中。应用程序的名称是“MYAPPLICATION”,将值添加到列表框的函数是 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;
}
}
}
}
}
回答by The Lazy Coder
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.
向其他应用程序发送消息的最简单且对我来说最可靠的方法是使用 WM_COPY 事件。你可以通过一些老式的 API 调用来解决这个问题。
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)
在 Windows 7 中仍然有效我们在最近的一个应用程序中实现了同样的功能,它在所有 Windows 平台上都能完美运行。(测试回 windows xp,但在 windows 98 中使用相同的 api)
Here is a link to codeproject.
这是代码项目的链接。
http://www.codeproject.com/KB/cs/ipc_wmcopy.aspx
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.
很酷,而且效率很高。使用一些工具,您可以使无限数量的应用程序实例相互通信。
回答by Eric J.
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).
消息队列是进程间通信的常用模式。Volure 的版本很酷。更常见的方法是使用 MSMQ(Microsoft 消息队列)。
Check it out here
在这里查看
http://msdn.microsoft.com/en-us/library/ms978430.aspx
http://msdn.microsoft.com/en-us/library/ms978430.aspx
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://blog.goyello.com/2009/09/08/why-msmq-is-excelent-for-net-developers/
http://blog.goyello.com/2009/09/08/why-msmq-is-excelent-for-net-developers/