Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: The Swash en 31 Marzo 2010, 18:27 pm



Título: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: The Swash en 31 Marzo 2010, 18:27 pm
Código:
'-----------------------------------------------------------
' Function : [GetTitleActiveApp]
' Type     : [SNIPPET]
' Autor    : [The Swash]
' DateTime : [31/03/2010]
'-----------------------------------------------------------
Option Explicit

'User32 Lib Apis
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long

'SendMessage Constants
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE

Public Function GetTitleActiveApp() As String
Dim hRet     As Long
Dim hSpace   As Long
Dim sBuffer  As String

  hRet = GetForegroundWindow
  If hRet <> 0 Then
   hSpace = SendMessage(hRet, WM_GETTEXTLENGTH, 0&, 0&) + 1
   If hSpace > 0 Then
    sBuffer = Space$(hSpace)
    Call SendMessage(hRet, WM_GETTEXT, hSpace, sBuffer)
   End If
  End If
  
  GetTitleActiveApp = Trim(sBuffer)
    
End Function

Call:
Código:
MsgBox GetTitleActiveApp


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Karcrack en 31 Marzo 2010, 20:30 pm
Muy interesante... pero se podria acortar si damos un buffer grande y trabajamos con el valor que retorna WM_GETTEXT:
Citar
The return value is the number of TCHARs copied, not including the terminating null character.

Por lo tanto:
Código
  1. 'USER32
  2. Private Declare Function GetForegroundWindow Lib "USER32" () As Long
  3. Private Declare Function SendMessageW Lib "USER32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
  4.  
  5. Private Const WM_GETTEXT = &HD
  6.  
  7. Public Function GetWindowText(ByVal lhWnd As Long) As String
  8.    GetWindowText = Space$(512)
  9.    GetWindowText = Left$(GetWindowText, SendMessageW(lhWnd, WM_GETTEXT, ByVal 512&, StrPtr(GetWindowText)))
  10. End Function
Código
  1. MsgBox GetWindowText(GetForegroundWindow)
Tu forma seria un mas optima, ya que crea el buffer necesario, ni mas grande ni mas pequeño...


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: The Swash en 31 Marzo 2010, 21:35 pm
Gracias por tu comentario amigo Karcrack, siempre te gustan los codes pequeñitos  :silbar:

Salu2  :D


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: wh0! en 1 Abril 2010, 03:49 am
jaja, Karcrack es Minimalista  :xD


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Hasseds en 1 Abril 2010, 05:25 am

Tu forma seria un mas optima, ya que crea el buffer necesario, ni mas grande ni mas pequeño...


Código:

Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function WindowTexto() As String
    Dim Texto As String: Texto = Space(256)
    Dim ret As Long: ret = GetWindowText(GetForegroundWindow(), Texto, 256)
    WindowTexto = Left$(Texto, ret)
End Function




Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Karcrack en 1 Abril 2010, 12:04 pm
@Hasseds: No es necesario crear una nueva varible para el buffer puedes trabajar con WindowTexto. Ademas, sigue habiendo el mismo prolema... El titulo puede ser mucho mas pequeño que 256 o mas grande... Por lo tanto no es optimo

@SkullByte: Si!  :-* Minimalismo :-* :xD :xD


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Hasseds en 1 Abril 2010, 12:48 pm
Por supuesto, tranquilo, es  solo una muestra con GetForegroundWindow  directamente dentro de GetWindowText,, y devuelve el buffer, igual te tu code, 

Preferí no usar WindowTexto y declarar ret porque no me gusta usar un string para un dato que es Long.




Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Karcrack en 1 Abril 2010, 13:18 pm
Por supuesto, tranquilo, es  solo una muestra con GetForegroundWindow  directamente dentro de GetWindowText,, y devuelve el buffer, igual te tu code, 
Me refiero a que para ser optimo tendrias que usar GetWindowTextLenght()

Preferí no usar WindowTexto y declarar ret porque no me gusta usar un string para un dato que es Long.
No entiendo a que te refieres... digo hacer esto:
Código
  1. Public Function WindowTexto() As String
  2.    WindowTexto = Space$(256)
  3.    WindowTexto = Left$(Texto, GetWindowText(GetForegroundWindow(), WindowTexto, 256))
  4. End Function


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Hasseds en 1 Abril 2010, 14:01 pm
Ahora te entendí, me estaba llendo para el carajo, quedó minimalista !


Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Hasseds en 1 Abril 2010, 14:57 pm
con GetWindowTextLength  ?

Código:

Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
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

Public Function WindowTexto() As String
 WindowTexto = Space$(GetWindowTextLength(GetForegroundWindow) + 1)
 WindowTexto = Left$(WindowTexto, GetWindowText(GetForegroundWindow(), WindowTexto, Len(WindowTexto)))
End Function



Título: Re: [SNIPPET] GetTitleActiveApp (VB6)
Publicado por: Karcrack en 1 Abril 2010, 15:01 pm
Exacto, de esta forma no te pasas o te quedas corto con el Buffer... Aunque hay que usar un API mas... pero bueno, depende de las necesidades que tengas :P

Saludos