【问题标题】:IsWindow returns false for existing window handleIsWindow 为现有的窗口句柄返回 false
【发布时间】:2016-01-16 21:50:57
【问题描述】:

在一个 VB6 应用程序中,我正在检查某个 VB.NET WinForms 窗口是否存在:

Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long

If Not IsWindow(102937) Then
      MessageBox("Window not found!")
End If

消息框已显示,但窗口确实存在。

我检查它通过

Debug.Print(Me.Handle.ToInt32)'it prints 102937

这里出了什么问题? 我是否可能错误地处理了“IsWindow”的返回值?

谢谢。

【问题讨论】:

  • IsWindow 的返回值是 BOOL 类型,int 的 typedef(32 位有符号整数)。 Long 在 VB.NET 中是 64 位的。请改用Integer
  • A thread should not use IsWindow for a window that it did not create because the window could be destroyed after this function was called. Further, because window handles are recycled the handle could even point to a different window.
  • 你的窗口句柄很奇怪?真的吗?
  • 正如 Bob77 在评论中指出的,这段代码显然是 VB6(不是 VB.NET)。在这种情况下,Long data type 实际上是 32 位,函数签名应该没问题。
  • 我没有检查 VB.NET。我正在检查 VB6。而且我确实使用该功能来查看窗口是否仍然存在。我经常做这个检查,所以窗口句柄被回收的机会不是很大。

标签: vb.net winapi vb6 window-handles


【解决方案1】:

我找到了解决办法:

我确实是错误地使用了 WinAPI 函数。

我应该用过

If IsWindow(102937) <> 1 Then

【讨论】:

  • 不,那是错误的。 IsWindow 返回零或任意非零值,因此您必须将该值与 0 进行比较,而不是与 1 进行比较。IInspectable 的注释是正确的,并且是问题的最可能原因。 (如果有疑问,打印 IsWindow 的输出,看看它是什么。我想你会发现底部 32 位为零,而顶部 32 位是垃圾。)
  • 很好,但请注意这是 VB6,其中 Long 是 32 位而不是 64。
猜你喜欢
  • 2017-06-19
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2012-11-12
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多