Clase: Extraer el nombre del proceso activo (GetActiveProcessName)
publicado
El objeto de la clase ActiveProcessInfo, nos indica el nombre del proceso activo.
He combinado dos librerías para conseguir tal efecto; La librería de KillProcess (que recorre los procesos y tiene acceso al nombre .exe) y la librería para obtener la ID del proceso activo. He buscado y buscado por internet sin encontrar nada similar y en mi búsqueda he encontrado muchos hilos de gente que buscaba esto mismo pero sin ningún éxito.
¿Qué para qué sirve? En mi caso, el uso que le he dado ha sido para restringir la ejecución de una macro solo a las ventanas activas que yo decida.
En el siguiente ejemplo, el programa solo se ejecutará si tenemos abierto el navegador Firefox y la ventana está activa:
If MiVentana.GetActiveProcessName = "firefox.exe" Then
Call EjecutarPrograma
End If[/CODE]
Código de la Clase ActiveProcessInfo
[CODE]Option Explicit
'-------------------------------------------------------
'Primeras 3 extraidas de KillProcess
Private Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
'Extraidas de: http://www.vbforums.com/showthread.php?558785-Get-Active-Window
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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
'-------------------------------------------------------
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private uProcess As PROCESSENTRY32
Private RProcessFound As Long
Private hSnapshot As Long
Private i As Integer
Private ActiveWindowID As Long
Private ActiveWindowHandle As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
Public Function GetActiveWindowHandle() As Long
Dim ActiveWindowHandle As Long
'get the handle of the active window
GetActiveWindowHandle = GetForegroundWindow()
End Function
Public Function GetActiveProcessTitle() As String
Dim ActiveWindowHandle As Long
'get the handle of the active window
ActiveWindowHandle = GetForegroundWindow()
Dim Title As String * 255
' get the title of the active window
GetWindowText ActiveWindowHandle, Title, Len(Title)
GetActiveProcessTitle = Trim(Title)
End Function
Public Function GetActiveProcessName() As String
Dim Res As String
Res = "NULL"
'Get the handle of the active window
ActiveWindowHandle = GetForegroundWindow()
'Extrae el lpdwProcessID
Call GetWindowThreadProcessId(ActiveWindowHandle, ActiveWindowID)
'Recorre las ID de los procesos hasta encontrar una coincidencia con ActiveWindowID
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
If uProcess.th32ProcessID = ActiveWindowID Then
Res = LCase$(Left$(uProcess.szexeFile, i - 1))
Exit Do
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
GetActiveProcessName = Res
End Function
'-------------------------------------------------------[/CODE]
En el adjunto viene la clase y un ejemplo de uso de sus propiedades.
El objeto de la clase ActiveProcessInfo, nos indica el nombre del proceso activo.
He combinado dos librerías para conseguir tal efecto; La librería de KillProcess (que recorre los procesos y tiene acceso al nombre .exe) y la librería para obtener la ID del proceso activo. He buscado y buscado por internet sin encontrar nada similar y en mi búsqueda he encontrado muchos hilos de gente que buscaba esto mismo pero sin ningún éxito.
¿Qué para qué sirve? En mi caso, el uso que le he dado ha sido para restringir la ejecución de una macro solo a las ventanas activas que yo decida.
En el siguiente ejemplo, el programa solo se ejecutará si tenemos abierto el navegador Firefox y la ventana está activa:
Código de la Clase ActiveProcessInfo
'-------------------------------------------------------
'Primeras 3 extraidas de KillProcess
Private Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
'Extraidas de: http://www.vbforums.com/showthread.php?558785-Get-Active-Window
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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
'-------------------------------------------------------
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private uProcess As PROCESSENTRY32
Private RProcessFound As Long
Private hSnapshot As Long
Private i As Integer
Private ActiveWindowID As Long
Private ActiveWindowHandle As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
Public Function GetActiveWindowHandle() As Long
Dim ActiveWindowHandle As Long
'get the handle of the active window
GetActiveWindowHandle = GetForegroundWindow()
End Function
Public Function GetActiveProcessTitle() As String
Dim ActiveWindowHandle As Long
'get the handle of the active window
ActiveWindowHandle = GetForegroundWindow()
Dim Title As String * 255
' get the title of the active window
GetWindowText ActiveWindowHandle, Title, Len(Title)
GetActiveProcessTitle = Trim(Title)
End Function
Public Function GetActiveProcessName() As String
Dim Res As String
Res = "NULL"
'Get the handle of the active window
ActiveWindowHandle = GetForegroundWindow()
'Extrae el lpdwProcessID
Call GetWindowThreadProcessId(ActiveWindowHandle, ActiveWindowID)
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
'Recorre las ID de los procesos hasta encontrar una coincidencia con ActiveWindowID
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
If uProcess.th32ProcessID = ActiveWindowID Then
Res = LCase$(Left$(uProcess.szexeFile, i - 1))
Exit Do
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
GetActiveProcessName = Res
End Function
'-------------------------------------------------------[/CODE]
En el adjunto viene la clase y un ejemplo de uso de sus propiedades.
Que lo disfruten.
Get Active Window Process Name.xls