在vb里面,怎么结束已经shell的程序?

2024-11-24 09:58:15
推荐回答(2个)
回答1:

如果被 Shell 所启动的程序还没有结束,我们就想主动结束它,该怎么做呢? 此时应调用的 Windows API 是 TerminateProcess,
细节如下:
API 的声明:
Const SYNCHRONIZE = &H100000

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As
Long
Private Declare Function TerminateProcess Lib "kernel32" Alias
"TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Shell 的程序范例:(以执行 MS-DOS 为例)
Dim pId As Long, pHnd As Long ' 分别声明 Process
Id 及 Process Handle 变数
pId = Shell("Command.com", vbNormalFocus) ' Shell
传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process
Handle
Call TerminateProcess( pHnd, 0 ) ' TerminateProcess 所传入的是 Process
Handle
Call CloseHandle( pHnd )

回答2:

Dim pid
Private Sub Command1_Click()
pid = Shell("C:\windows\system32\notepad.exe")
End Sub
Private Sub Command2_Click()
cmd = "taskkill /pid " + CStr(pid)
Shell (cmd)
End Sub