WM_Close没有到达目标窗口;间谍++也看不到wm_close

发布于 2025-02-08 01:16:47 字数 991 浏览 1 评论 0 原文

我正在尝试将 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}'");
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-02-15 01:16:47

且来自后呼叫的返回代码为零,所以后消息说已发送消息。

这完全是错误的。 postmessage 函数 。因此,假设您的代码(如所示)未显示错误消息,则 postmessage 呼叫失败,并且 wm_close 不会发送到目标。

您需要更改错误处理以使用如果(结果== 0){... ,然后,在该块中,调用 getlasterror 函数以检索错误代码。如果那是 5 (我怀疑),则由于UIPI访问不正确而导致呼叫失败,您将需要如下Q/A中所述解决该问题:交叉程序后,UIPI限制和UIACCESS =“ true”


注意:调用 getLasterror winapi函数从C#和/或.NET项目并非微不足道;有关如何正确执行的讨论,请参见此处: winapi -getlasterror vs.marshal.getlastwin32222ror

And the return code from the PostMessage call is zero, so PostMessage is saying that it sent the message.

That's just wrong. The PostMessage function returns zero on failure. So, assuming your code (as presented) doesn't show the error message, then the PostMessage call is failing, and WM_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 the GetLastError function to retrieve the error code. If that is 5 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文