elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [SNIPPET] GetTitleActiveApp (VB6)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: [SNIPPET] GetTitleActiveApp (VB6)  (Leído 5,294 veces)
The Swash

Desconectado Desconectado

Mensajes: 194


Programmer


Ver Perfil WWW
[SNIPPET] GetTitleActiveApp (VB6)
« 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


« Última modificación: 31 Marzo 2010, 18:37 pm por The Swash » En línea

Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #1 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...


En línea

The Swash

Desconectado Desconectado

Mensajes: 194


Programmer


Ver Perfil WWW
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #2 en: 31 Marzo 2010, 21:35 pm »

Gracias por tu comentario amigo Karcrack, siempre te gustan los codes pequeñitos  :silbar:

Salu2  :D
En línea

wh0!

Desconectado Desconectado

Mensajes: 10



Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #3 en: 1 Abril 2010, 03:49 am »

jaja, Karcrack es Minimalista  :xD
En línea

Hasseds

Desconectado Desconectado

Mensajes: 145



Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #4 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


En línea

Sergio Desanti
Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #5 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
En línea

Hasseds

Desconectado Desconectado

Mensajes: 145



Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #6 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.


En línea

Sergio Desanti
Karcrack


Desconectado Desconectado

Mensajes: 2.416


Se siente observado ¬¬'


Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #7 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
En línea

Hasseds

Desconectado Desconectado

Mensajes: 145



Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #8 en: 1 Abril 2010, 14:01 pm »

Ahora te entendí, me estaba llendo para el carajo, quedó minimalista !
En línea

Sergio Desanti
Hasseds

Desconectado Desconectado

Mensajes: 145



Ver Perfil
Re: [SNIPPET] GetTitleActiveApp (VB6)
« Respuesta #9 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

En línea

Sergio Desanti
Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[m][SNIPPET] IsUserAnAdmin?
Programación Visual Basic
Karcrack 5 2,982 Último mensaje 30 Julio 2010, 17:38 pm
por Karcrack
[SNIPPET] Decimal a Romano
Programación Visual Basic
Karcrack 4 2,223 Último mensaje 17 Diciembre 2010, 14:41 pm
por 79137913
[SNIPPET-VB6] DrawGraph - Dibujar sobre controles.
Programación Visual Basic
F3B14N 0 1,461 Último mensaje 12 Marzo 2011, 14:48 pm
por F3B14N
[SNIPPET][VB6] WhereAmI, thisexe, GetMyPath...
Programación Visual Basic
Karcrack 4 2,914 Último mensaje 11 Marzo 2014, 03:42 am
por LeandroA
Ayuda .cpp Snippet
Programación C/C++
alexlopezcifuentes 1 1,530 Último mensaje 6 Marzo 2015, 11:11 am
por eferion
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines