Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't get findwindow to work - newbie question

Status
Not open for further replies.

imjbhowe

IS-IT--Management
Jul 26, 2001
18
0
0
US
I am trying to test for an open window using findwindow but I always get a value returned in the hwnd variable whether the app is open or not. I have tried putting garbage in for the caption name and it still returns numbers. All the info out there on find window indicates that if the window you are testing for is closed, it should return 0 but I can't get it to return 0. Any help is appreciated, here is the code.

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Dim hwnd As Long
hwnd = FindWindow("XLMAIN", 0)
If hwnd <> 0 Then
MessageBox.Show(&quot;The window handle is &quot; & hwnd)
Else
MessageBox.Show(&quot;the app is not running&quot;)
End If

End Sub


 
You should use:

Imports System.Runtime.InteropServices

Public Class MyApi
<DllImport(&quot;user32.dll&quot;)> Public Shared Function _
FindWindow(ByVal strClassName As String, ByVal _ strWindowName As String) As Integer
End Function
End Class

...
MyApi.FindWindow(...)
...

As opposed to:
Public Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

This old fasioned way doesn't seem to work anymore.

malvarez@clientsoft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top