ADO DOT NET Posted April 8, 2007 Posted April 8, 2007 Hi, In vb6 I was using findwindow to see if there's a window with a specific caption. but it won't work in .net 2005? any other way? thanks Dim hhwnd As Long Dim retval As Long hhwnd = FindWindow(vbNullString, "AnotherApp") If hhwnd <> 0 Then MsgBox "Another app is running." End End If Quote
Administrators PlausiblyDamp Posted April 8, 2007 Administrators Posted April 8, 2007 It's probably down to your declaration of FindWindow - various datatypes etc. have changed since VB6. Try replacing your declaration with the following _ Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function _ Private Shared Function FindWindowByClass(ByVal lpClassName As String, ByVal zero As IntPtr) As IntPtr End Function _ Private Shared Function FindWindowByCaption(ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted April 8, 2007 Author Posted April 8, 2007 <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. Quote
Administrators PlausiblyDamp Posted April 8, 2007 Administrators Posted April 8, 2007 You don't 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. private Class Test Class _ Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function _ Private Shared Function FindWindowByClass(ByVal lpClassName As String, ByVal zero As IntPtr) As IntPtr End Function _ 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts