我正在尝试将 wm_close
从我的应用程序发送到目标应用程序,以告诉其关闭。我正在Net 6.0上的VS调试器中运行。
我找到了目标应用程序的句柄,并将 wm_close
消息发送到其句柄,但是间谍++说该消息永远不会到达目标窗口,因此,目标窗口当然不会关闭。
我知道我有正确的目标手柄,因为我可以在间谍++中看到它(我用间谍++ Crosshairs手动标记目标窗口标题栏),并且它们匹配。同样,当我从句柄获得窗口标题时,它在目标应用程序的标题栏中可以看到相同的窗口标题。并且来自 postmessage
调用的返回代码为零,因此 postmessage
在说它已发送消息。
但是, postmessage
and sendmessage
发送的消息在目标窗口的消息中可见,这些消息是在间谍++滚动显示中实时记录的。
为什么我的 wm_close
消息不是:(1)在间谍++中可见,(2)未达到目标应用程序?
public static void
SendCloseToHandle(IntPtr handle) {
var result = PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
if (result != 0) {
FormAppendTextContent($"WM_CLOSE failed to send. {result:X}");
return;
}
//SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
var title = GetWindowTitle(handle);
FormAppendTextContent($"Posted WM_CLOSE message to {title} '{handle:X}'");
}
I am trying to send WM_CLOSE
from my app to a target app to tell it to close. I am running in the VS debugger on NET 6.0.
I find the handle of the target app and send a WM_CLOSE
message to its handle, but Spy++ says that the message never arrives at the target window, and so of course the target window does not close.
I know I have the right target handle because I can see it in Spy++ (I tag the target window title bar manually with the Spy++ crosshairs), and they match. Also when I get the window title from the handle, it gives the same window title visible in the title bar of the target app. And the return code from the PostMessage
call is zero, so PostMessage
is saying that it sent the message.
But neither PostMessage
nor SendMessage
sends a message that is visible in the messages for the target window that are being logged in real-time in the Spy++ rolling display.
Why is my WM_CLOSE
message not: (1) visible in Spy++, and (2) not reaching the target app?
public static void
SendCloseToHandle(IntPtr handle) {
var result = PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
if (result != 0) {
FormAppendTextContent(quot;WM_CLOSE failed to send. {result:X}");
return;
}
//SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
var title = GetWindowTitle(handle);
FormAppendTextContent(quot;Posted WM_CLOSE message to {title} '{handle:X}'");
}
发布评论
评论(1)
这完全是错误的。
postmessage
函数 。因此,假设您的代码(如所示)未显示错误消息,则postmessage
呼叫失败,并且wm_close
不会发送到目标。您需要更改错误处理以使用
如果(结果== 0){...
,然后,在该块中,调用getlasterror
函数以检索错误代码。如果那是5
(我怀疑),则由于UIPI访问不正确而导致呼叫失败,您将需要如下Q/A中所述解决该问题:交叉程序后,UIPI限制和UIACCESS =“ true” 。注意:调用
getLasterror
winapi函数从C#和/或.NET项目并非微不足道;有关如何正确执行的讨论,请参见此处: winapi -getlasterror vs.marshal.getlastwin32222ror 。That's just wrong. The
PostMessage
function returns zero on failure. So, assuming your code (as presented) doesn't show the error message, then thePostMessage
call is failing, andWM_CLOSE
isn't being sent to the target.You need to change your error-handling to use
if (result == 0) { ...
and then, in that block, call theGetLastError
function to retrieve the error code. If that is5
(as I suspect), then the call is failing due to incorrect UIPI access, and you will need to address that issue as described in this Q/A: Cross-process PostMessage, UIPI restrictions and UIAccess=”true”.Note: Calling the
GetLastError
WinAPI function from C# and/or .Net projects is not trivial; for an in-depth discussion of how to properly do so, see here: WinApi - GetLastError vs. Marshal.GetLastWin32Error.