How do I find and use a Window Handle?

  • Thread starter Thread starter Vic Joseph
  • Start date Start date
V

Vic Joseph

I'm trying to get information about the window of an external program. I
understand that I need to use API functions to do this, but somehow I cannot
get them to work. Whenever I try to use the handle (assuming I have found it
correctly using the FindWindow function) I get the error: "A call to PInvoke
function '...FlashWindow' has unbalanced the stack. This is likely because
the managed PInvoke signature does not match the unmanaged target
signature." Can anyone tell me what I am doing wrong?

Here's an example of the code, which tries to flash the Calculator.exe
window once:

'DECLARE API FUNCTIONS
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FlashWindow Lib "user32" Alias "FlashWindow" (ByVal hwnd As
Long, ByVal bInvert As Long) As Long

'TEST THE FUNCTIONS
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim wHandle As Long
wHandle = WindowFinder.FindWindow("", "Calculator")
'THIS IS WHERE I GET THE ERROR:
If wHandle <> "" Then FlashWindow(wHandle, 1)

End Sub

thanks in advance en vriendelijke groet, Vic Joseph
 
I noticed just too late that I pasted in the wrong bit of code. In the line:
wHandle = WindowFinder.FindWindow("", "Calculator")

"WindowFinder." shouldn't have been there. The line is:

wHandle = FindWindow("", "Calculator")


Vic Joseph
 
'DECLARE API FUNCTIONS
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FlashWindow Lib "user32" Alias "FlashWindow" (ByVal hwnd As
Long, ByVal bInvert As Long) As Long

Your declarations are incorrect, they should be

Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As
String, ByVal lpWindowName As String) As IntPtr
Declare Function FlashWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal
bInvert As Boolean) As Boolean

I also recommend that you turn on Option Strict.


Mattias
 
Thanks for your help Mattias.

I have corrected the calls as you suggested and turned on Option Strict, but
I still can't get things working. As a simple test of FindWindow, I put the
following lines in my subroutine:

Dim wHandle As IntPtr = FindWindow("", "Calculator")
MsgBox("Windows Handle: " & wHandle.ToString)

The result is that wHandle always appears to be zero. Presumably, as
Calculator.exe is running, it ought to have a positive nonzero value. Do I
need to supply a string for the first parameter of FindWindow? If so, what?
Or what else am I doing wrong?

regards, Vic Joseph
 
In case anyone is interested in the answer to this: the first parameter
given to FindWindow should have been Nothing, not an empty string!
Vic Joseph

Vic Joseph said:
Thanks for your help Mattias.

I have corrected the calls as you suggested and turned on Option Strict,
but I still can't get things working. As a simple test of FindWindow, I
put the following lines in my subroutine:

Dim wHandle As IntPtr = FindWindow("", "Calculator")
MsgBox("Windows Handle: " & wHandle.ToString)

The result is that wHandle always appears to be zero. Presumably, as
Calculator.exe is running, it ought to have a positive nonzero value. Do I
need to supply a string for the first parameter of FindWindow? If so,
what? Or what else am I doing wrong?

regards, Vic Joseph
 
Back
Top