要解决GenerateConsoleCtrlEvent的限制,我必须创建一个中间“中间人”进程来处理启动某些控制台应用程序。该进程的主要目的是在自身上调用GenerateConsoleCtrlEvent,导致自身和所有子进程干净地关闭以响应ctrl + break信号(而不是使用Process.Kill)。这种需要源于GenerateConsoelCtrlEvent基本上没有效果,除非进程组id为零,这意味着它只对调用进程组本身有效。请参阅:https://stackoverflow.com/a/2431295/88409
所以无论如何......我创建了这个中间过程,它启动了一个在处理特定用户定义消息的表单上调用Application.Run的线程。
我的问题是......如何向此流程发送消息来控制它?
我有Process对象及其进程ID,但这就是全部。 Process.MainWindowHandle为零。
所以我需要一种方法将消息发送到特定进程或将消息广播到特定进程中的所有窗口。
FindWindow不是一个选项,因为它试图在任何进程上按名称和类标识一个窗口,这是不可靠的。我想在没有歧义的情况下向特定流程发送消息。
答案 0 :(得分:6)
有3种情况可以将 的消息视为发送或发布到流程:
方法1可能过于具体,因为它针对特定但任意的窗口。 方法2可能不够具体,因为第一个枚举线程是任意的并且可能没有消息循环。 方法3是一种混合方法,它首先识别一个窗口,然后将一个线程消息发布到该窗口的线程,因此它不是针对特定窗口(即它是一个“线程消息”),但它的目标是一个线程,即可能有一个消息循环,因为该主题至少拥有一个窗口。
以下是支持所有三种方法以及两种方法“发送”和“发布”的实现。方法1包含在下面的方法SendMessage和PostMessage中。方法2和3包含在下面的PostThreadMessage方法中,具体取决于您是否设置了可选参数ensureTargetThreadHasWindow
(true =方法3,false =方法2)。
public static class ProcessExtensions
{
private static class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostThreadMessage(uint threadId, uint msg, IntPtr wParam, IntPtr lParam);
public delegate bool EnumThreadDelegate (IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", SetLastError=true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
//Sends a message to the first enumerated window in the first enumerated thread with at least one window, and returns the handle of that window through the hwnd output parameter if such a window was enumerated. If a window was enumerated, the return value is the return value of the SendMessage call, otherwise the return value is zero.
public static IntPtr SendMessage( this Process p, out IntPtr hwnd, UInt32 msg, IntPtr wParam, IntPtr lParam )
{
hwnd = p.WindowHandles().FirstOrDefault();
if (hwnd != IntPtr.Zero)
return Win32.SendMessage( hwnd, msg, wParam, lParam );
else
return IntPtr.Zero;
}
//Posts a message to the first enumerated window in the first enumerated thread with at least one window, and returns the handle of that window through the hwnd output parameter if such a window was enumerated. If a window was enumerated, the return value is the return value of the PostMessage call, otherwise the return value is false.
public static bool PostMessage( this Process p, out IntPtr hwnd, UInt32 msg, IntPtr wParam, IntPtr lParam )
{
hwnd = p.WindowHandles().FirstOrDefault();
if (hwnd != IntPtr.Zero)
return Win32.PostMessage( hwnd, msg, wParam, lParam );
else
return false;
}
//Posts a thread message to the first enumerated thread (when ensureTargetThreadHasWindow is false), or posts a thread message to the first enumerated thread with a window, unless no windows are found in which case the call fails. If an appropriate thread was found, the return value is the return value of PostThreadMessage call, otherwise the return value is false.
public static bool PostThreadMessage( this Process p, UInt32 msg, IntPtr wParam, IntPtr lParam, bool ensureTargetThreadHasWindow = true )
{
uint targetThreadId = 0;
if (ensureTargetThreadHasWindow)
{
IntPtr hwnd = p.WindowHandles().FirstOrDefault();
uint processId = 0;
if (hwnd != IntPtr.Zero)
targetThreadId = Win32.GetWindowThreadProcessId( hwnd, out processId );
}
else
{
targetThreadId = (uint)p.Threads[0].Id;
}
if (targetThreadId != 0)
return Win32.PostThreadMessage( targetThreadId, msg, wParam, lParam );
else
return false;
}
public static IEnumerable<IntPtr> WindowHandles( this Process process )
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in process.Threads)
Win32.EnumThreadWindows( (uint)thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero );
return handles;
}
}
在Process对象上使用此扩展方法:
Process process = Process.Start( exePath, args );
IntPtr hwndMessageWasSentTo = IntPtr.Zero; //this will receive a non-zero value if SendMessage was called successfully
uint msg = 0xC000; //The message you want to send
IntPtr wParam = IntPtr.Zero; //The wParam value to pass to SendMessage
IntPtr lParam = IntPtr.Zero; //The lParam value to pass to SendMessage
IntPtr returnValue = process.SendMessage( out hwndMessageWasSentTo, msg, wParam, lParam );
if (hwndMessageWasSentTo != IntPtr.Zero)
Console.WriteLine( "Message successfully sent to hwnd: " + hwndMessageWasSentTo.ToString() + " and return value was: " + returnValue.ToString() );
else
Console.WriteLine( "No windows found in process. SendMessage was not called." );
答案 1 :(得分:2)
您无法向流程发送或发送消息,但您可以向消息发布消息。当然,该线程必须启动一个消息循环来处理它。
答案 2 :(得分:0)
使用EnumWindows扫描顶级窗口,并使用GetProcessHandleFromHWnd确定目标进程中是否存在窗口。
然后您可以向该hwnd发送消息,并且它们应该由您的目标进程接收(如果您的目标进程有消息循环)。
答案 3 :(得分:0)
我刚刚回答了一个非常类似的问题(带有示例代码)here。
快速答案是PostThreadMessage()
。