find winodw

ADO DOT NET

Well-known member
Joined
Dec 20, 2006
Messages
156
Hi,
In vb6 I was using findwindow to see if theres a window with a specific caption.
but it wont work in .net 2005?
any other way?
thanks
Code:
   Dim hhwnd As Long
   Dim retval As Long
   hhwnd = FindWindow(vbNullString, "AnotherApp")
   If hhwnd <> 0 Then
    MsgBox "Another app is running."
    End
   End If
 
Its probably down to your declaration of FindWindow - various datatypes etc. have changed since VB6.

Try replacing your declaration with the following
Code:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass(ByVal lpClassName As String, ByVal zero As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption(ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr
End Function
 
Code:
    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowByCaption(ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr
        Dim hhwnd As Integer
        hhwnd = FindWindowByCaption(vbNullString, "AnotherApp")
        If hhwnd <> 0 Then
            MsgBox("Another app is running.")
            End
        End If
    End Function
Error:
Error 1 System.Runtime.InteropServices.DllImportAttribute cannot be applied to a Sub, Function, or Operator with a non-empty body.
 
You dont need to apply the attributes to your own function - just paste them direct within your class and leave your original function as it was. i.e.

Code:
private Class Test Class

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass(ByVal lpClassName As String, ByVal zero As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption(ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr
End Function

Private Sub TestMethod

    Dim hhwnd As Integer

    hhwnd = FindWindow(vbNullString, "AnotherApp")

    If hhwnd <> 0 Then
        MessageBox.Show ( "Another app is running.")
        End    Exiting your application this way is not normally a good thing either...
    End If 
End Sub
End Class
 
Back
Top