Hola
Vamos a ver si te sirve esto hecho en vb5.0:
Introduce el código adjunto en un módulo y crea un formulario vacio con el nombre frm1.
El objeto inicial debe ser el sub main().
Cuando tengas el ejecutable creado p.e. Miejecutable.exe, para matar una tarea como puede ser la calculadora haces lo siguiente desde Inicio -> Ejecutar:
Miejecutable.exe Calculadora
El funcionamiento es muy sencillo, se trata de un recorrido a traves de todas las ventanas abiertas en windows tomando como punto de partida la ventana abierta por el propio programa llamada frm1. Cuando encuentra una ventana con el nombre pasado como paramentro al ejecutable la activa y manda las teclas alt+f4 que es lo mismo que cerrarla. Es importante que el nombre pasado como parametro coincida plenamente con el nombre de la tarea (titulo del formulario).
No es la mejor solución pero puede servir en algunas ocasiones.
Saludos
Attribute VB_Name = "Module1"
Declare Function WinExec Lib "Kernel" (ByVal lpCmdLine As String, ByVal nCmdShow As Integer) As Integer
Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Const gw_child = 5
Const gw_hwndfirst = 0
Const gw_hwndlast = 1
Const gw_hwndnext = 2
Const gw_hwndprev = 3
Const gw_owner = 4
Sub main()
Dim strTarea As String
frm1.Show
strTarea = obtener_titulo(Command$)
frm1.Hide
If strTarea <> "" Then
AppActivate strTarea, False
SendKeys "%{f4}", True
End If
End
End Sub
Function obtener_titulo(titulo As String) As String
'
' obtener de la lista de tareas el titulo completo de la tarea "titulo"
'
Dim actual As Long, longitud As Long, entrada As String
actual = GetWindow(frm1.hwnd, gw_hwndfirst)
While actual <> 0
' recorrer las tareas de la ventana
longitud = GetWindowTextLength(actual)
entrada = Space$(longitud + 1)
longitud = GetWindowText(actual, entrada, longitud + 1)
If longitud > 0 Then
If InStr(UCase$(entrada), UCase$(titulo)) > 0 Then
' la entrada coincide
obtener_titulo = entrada
Exit Function
End If
End If
actual = GetWindow(actual, gw_hwndnext)
DoEvents
Wend
End Function
|