假设按command1按钮就启动A.EXE、B.EXE、C.EXE,在程序退出时关闭它们。首先在一个模块里作以下声明: Const SYNCHRONIZE = &H100000 public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long dim pida as long dim pidb as long dim pidc as long dim phnda as long dim phndb as long dim phndc as longprivate sub command1_click() pIda = Shell("a.exe", vbNormalFocus) ' Shell 传回a.exe的 Process Id pHnda = OpenProcess(SYNCHRONIZE, 0, pIda) ' 取得a.exe的 Process HandlepIdb = Shell("b.exe", vbNormalFocus) ' Shell 传回b.exe的 Process Id pHndb = OpenProcess(SYNCHRONIZE, 0, pIdb) ' 取得b.exe的 Process Handle
pIdc = Shell("c.exe", vbNormalFocus) ' Shell 传回c.exe的 Process Id pHndc = OpenProcess(SYNCHRONIZE, 0, pIdc) ' 取得c.exe的 Process Handle
end sub private form1_unload() Call TerminateProcess( pHnda, 0 ) ' TerminateProcess 所传入的是 Process Handle Call CloseHandle( pHnda )Call TerminateProcess( pHndb, 0 ) ' TerminateProcess 所传入的是 Process Handle Call CloseHandle( pHndb )Call TerminateProcess( pHndc, 0 ) ' TerminateProcess 所传入的是 Process Handle Call CloseHandle( pHndc )end end sub
试过了,可以的,你用ctrl+alt+del叫出任务管理器,把你的vb程序终止,那么a.exe,b.exe,c.exe都会一起结束的,另外,上面的代码不小心写错了, dim pida as long dim pidb as long dim pidc as long dim phnda as long dim phndb as long dim phndc as long 应该改为: public pida as long public pidb as long public pidc as long public phnda as long public phndb as long public phndc as long98+vb6下通过.
Option ExplicitPublic Const GW_HWNDNEXT = 2Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) 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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long' Return the window handle for an instance handle. Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long Dim test_pid As Long Dim test_thread_id As Long ' Get the first window handle. test_hwnd = FindWindow(ByVal 0&, ByVal 0&) ' Loop until we find the target or we run out ' of windows. Do While test_hwnd <> 0 ' See if this window has a parent. If not, ' it is a top-level window. If GetParent(test_hwnd) = 0 Then ' This is a top-level window. See if ' it has the target instance handle. test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then ' This is the target. InstanceToWnd = test_hwnd Exit Do End If End If ' Examine the next window. test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End FunctionPrivate Sub Command1_Click() Dim pid As Long Dim buf As String Dim buf_len As Long Dim styles As Long pid = Shell("notepad.exe", vbNormalFocus) If pid = 0 Then MsgBox "Error starting program" Exit Sub End If Notepad_Hwnd& = InstanceToWnd(pid) Notepad_OldParent& = SetParent(Notepad_Hwnd&, Me.hwnd)End Sub
Const SYNCHRONIZE = &H100000
public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
dim pida as long
dim pidb as long
dim pidc as long
dim phnda as long
dim phndb as long
dim phndc as longprivate sub command1_click()
pIda = Shell("a.exe", vbNormalFocus) ' Shell 传回a.exe的 Process Id
pHnda = OpenProcess(SYNCHRONIZE, 0, pIda) ' 取得a.exe的 Process HandlepIdb = Shell("b.exe", vbNormalFocus) ' Shell 传回b.exe的 Process Id
pHndb = OpenProcess(SYNCHRONIZE, 0, pIdb) ' 取得b.exe的 Process Handle
pIdc = Shell("c.exe", vbNormalFocus) ' Shell 传回c.exe的 Process Id
pHndc = OpenProcess(SYNCHRONIZE, 0, pIdc) ' 取得c.exe的 Process Handle
end sub
private form1_unload()
Call TerminateProcess( pHnda, 0 ) ' TerminateProcess 所传入的是 Process Handle
Call CloseHandle( pHnda )Call TerminateProcess( pHndb, 0 ) ' TerminateProcess 所传入的是 Process Handle
Call CloseHandle( pHndb )Call TerminateProcess( pHndc, 0 ) ' TerminateProcess 所传入的是 Process Handle
Call CloseHandle( pHndc )end
end sub
此方法适用于98,在nt、2000下无效。
dim pida as long
dim pidb as long
dim pidc as long
dim phnda as long
dim phndb as long
dim phndc as long
应该改为:
public pida as long
public pidb as long
public pidc as long
public phnda as long
public phndb as long
public phndc as long98+vb6下通过.
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) 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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long' Return the window handle for an instance handle.
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long ' Get the first window handle.
test_hwnd = FindWindow(ByVal 0&, ByVal 0&) ' Loop until we find the target or we run out
' of windows.
Do While test_hwnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(test_hwnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then
' This is the target.
InstanceToWnd = test_hwnd
Exit Do
End If
End If ' Examine the next window.
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End FunctionPrivate Sub Command1_Click()
Dim pid As Long
Dim buf As String
Dim buf_len As Long
Dim styles As Long pid = Shell("notepad.exe", vbNormalFocus)
If pid = 0 Then
MsgBox "Error starting program"
Exit Sub
End If Notepad_Hwnd& = InstanceToWnd(pid) Notepad_OldParent& = SetParent(Notepad_Hwnd&, Me.hwnd)End Sub
已经声明在先,我的方法在nt、2000下是无效的。
发送消息就可以了