说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

VB如何结束 Shell 所启动的程序?

HTML文档下载 WORD文档下载 PDF文档下载
VB如何结束 Shell 所启动的程序?
如果被 Shell 所启动的程序还没有结束, 我们就想主动结束它,该怎么做呢?
此时应调用的 Windows API 是 TerminateProcess, 细节如下:
1. API 的声明:
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
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 WaitForSingleObject Lib "kernel32" (ByVal
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" Alias "
TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As
Long
注:如果以上的声明放在「一般模块」底下, 应将 Declare 之前的 Private 保
留字去掉, 并且在 Const 之前加上 Public 保留字。
2. 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 )
但以上的方案只适用于 Shell 所启动的程序, ShellExecute 则不适用, 原因是
ShellExecute 函数是通过资源管理器来启动程序, 而资源管理器启动程序之后,
并没有将 Process ID 或 Process Handle 传回来。


备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘