-1

In a VB6 application I am checking if a certain VB.NET WinForms window exists:

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

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

The messagebox is shown, but the window DOES exist.

I inspect it by

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

What goes wrong here? Am I perhaps handling the return value of "IsWindow" incorrectly?

Thank you.

Maiken Roskilde
  • 447
  • 3
  • 14
  • 2
    The return value of [IsWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633528.aspx) is of type `BOOL`, a typedef for `int` (32 bit signed integer). [Long](https://msdn.microsoft.com/en-us/library/y595sc15.aspx) in VB.NET is 64 bits. Use [Integer](https://msdn.microsoft.com/en-us/library/06bkb8w2.aspx) instead. – IInspectable Jan 16 '16 at 22:27
  • `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.` – Ňɏssa Pøngjǣrdenlarp Jan 16 '16 at 23:05
  • Your window handle is odd? Really? – David Heffernan Jan 17 '16 at 11:46
  • 1
    As noted by Bob77 in a comment, this code is apparently VB6 (not VB.NET). In that case, the [Long data type](https://msdn.microsoft.com/en-us/library/aa263420(v=vs.60).aspx) is in fact 32 bits, and the function signature should be ok. – IInspectable Jan 17 '16 at 15:01
  • I am not checking in VB.NET. I am checking in VB6. And I do use the function to see if the window still exists. I am doing this check rather often, so the chance that the window handle has been recycled is not very big. – Maiken Roskilde Jan 19 '16 at 08:23

1 Answers1

0

I found the solution:

I was indeed using the WinAPI function incorrectly.

I should have used

If IsWindow(102937) <> 1 Then
Maiken Roskilde
  • 447
  • 3
  • 14
  • 4
    No, that's wrong. IsWindow returns either zero or an arbitrary non-zero value, so you must compare the value to 0, not to 1. IInspectable's comment is correct and the most likely cause of the problem. (If in doubt, print the output of IsWindow and see what it is. I think you'll find the bottom 32 bits are zero, and you're getting rubbish in the top 32 bits.) – Harry Johnston Jan 17 '16 at 01:03
  • Good, but note that this is VB6, where Long is 32 bits not 64. – Bob77 Jan 17 '16 at 04:27