Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

   <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.

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...