This is the testbed that I used for killing Shelled Apps
Call SendMessage(mWnd, WM_DESTROY, 0, 0)
is fine for Windows Apps - while no use for DOS boxes.
You'll find both methods in here - the 'drastic' version is remmed
out.
HTH
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long)
As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal
hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long,
ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock
As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const WM_QUIT = &H12
Private Const WM_DESTROY = &H2
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Dim Pid As Long
' --- This gives a hWnd from a Process ID
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd,
test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
Dim S$
'Execute LIST.COM
'S$ = Environ("comspec") + " /c c:\utils\list.com *.exe"
S$ = "notepad.exe"
Pid = Shell(S$, vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
'we don't use it - but you may find a solution
'and it is what you asked for
mWnd = InstanceToWnd(Pid)
End Sub
' --- This is pretty drastic - but works
Private Sub Command1_Click()
Dim hProcess&
'hProcess = OpenProcess(0, False, Pid)
'TerminateProcess hProcess, 0
'CloseHandle hProcess
Call SendMessage(mWnd, WM_DESTROY, 0, 0) ' No use for dos apps
End Sub
Quote:
>Hello!
>It is possible to start an application using the shell-statement and as a
>result you get the taskID of this process. I would like to know now, if it
>would also be possible to quit this application by the VB-program which has
>started it. Is there a possiblilty to kill this process, when I know the
>taskID ?
>Thanx in advance.
>Dirk