Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Stereo en 22 Mayo 2012, 19:44 pm



Título: Obtener texto de la ventana abierta
Publicado por: Stereo en 22 Mayo 2012, 19:44 pm
Buenas, llevo todo el dia intentando obtener el titulo de la ventana que tienes abierta pero no lo he conseguido, e aqui mi intento:

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

El error al depurar es en esta linea:

Código:
GetWindowText(hwnd, MyStr, 254)


Título: Re: Obtener texto de la ventana abierta
Publicado por: The Swash en 22 Mayo 2012, 20:33 pm
GetForeGroundWindow()
Retrieves a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.
Código:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx


Título: Re: Obtener texto de la ventana abierta
Publicado por: Stereo en 22 Mayo 2012, 20:56 pm
Gracias pero el caso es que esa función no la reconoce, uso visual studio 2010


Título: Re: Obtener texto de la ventana abierta
Publicado por: noele1995 en 22 Mayo 2012, 21:00 pm
Si no me equivoco eso es .net y hay un subforo para ello.

Aqui va el codigo

Código
  1. Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long,ByVal lpString As String, ByVal cch As Long) As Long  
  2. Private Declare Function GetForegroundWindow Lib "User32" () As Long  
  3.  
  4. public function TextoVentanaActiva() as string
  5.  
  6. Dim hwnd_Ventana As Long  
  7. Dim Caption_Ventana As String  
  8. Dim length As Long  
  9.  
  10.    ' Captura el Hwnd de la ventana activa  
  11.    hwnd_Ventana = GetForegroundWindow()  
  12.  
  13.    'Crea un Buffer
  14.    Caption_Ventana = Space$(1024)  
  15.  
  16.    ' Retorna la cantidad de caracteres  
  17.    length = GetWindowText(hwnd_Ventana, Caption_Ventana, Len(Caption_Ventana))  
  18.  
  19.    ' Obtiene solo el caption sin los espacios nulos de la cadena  
  20.    TextoVentanaActiva = Left$(Caption_Ventana, length)  
  21. end function
  22.  


Título: Re: Obtener texto de la ventana abierta
Publicado por: Stereo en 22 Mayo 2012, 21:21 pm
Me sigue tirando error al empezar el programa con debug en esta linea :S

Dice: Se detectó PInvokeStackImbalance

Código:
length = GetWindowText(hwnd_Ventana, Caption_Ventana, Len(Caption_Ventana))