Código:
Public Class Form1
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Function findPartialTitle(ByVal partialTitle As String) As IntPtr
For Each p As Process In Process.GetProcesses()
If p.MainWindowTitle.IndexOf(partialTitle, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
'
'We found a match, so return the handle to the window
'
Return p.MainWindowHandle
End If
Next
'
'If no match, return NULL (or close enough)
'
Return IntPtr.Zero
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hwnd As Long = findPartialTitle("YouTube")
Dim lngLen As Integer = 512
Dim MyStr As String
'Create a buffer
MyStr = 255
'Get the window's text
GetWindowText(hwnd, MyStr, 254)
MsgBox("La ventana abierta no es correcta, intentalo de nuevo: " &MyStr)
End Sub
End Class
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Function findPartialTitle(ByVal partialTitle As String) As IntPtr
For Each p As Process In Process.GetProcesses()
If p.MainWindowTitle.IndexOf(partialTitle, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
'
'We found a match, so return the handle to the window
'
Return p.MainWindowHandle
End If
Next
'
'If no match, return NULL (or close enough)
'
Return IntPtr.Zero
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hwnd As Long = findPartialTitle("YouTube")
Dim lngLen As Integer = 512
Dim MyStr As String
'Create a buffer
MyStr = 255
'Get the window's text
GetWindowText(hwnd, MyStr, 254)
MsgBox("La ventana abierta no es correcta, intentalo de nuevo: " &MyStr)
End Sub
End Class
El error al depurar es en esta linea:
Código:
GetWindowText(hwnd, MyStr, 254)