'窗体上两个按钮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 Const PROCESS_QUERY_INFORMATION = &H400 Const STILL_ALIVE = &H103Dim hProcess As Long Private Sub Command1_Click() Dim pid As Long pid = Shell("notepad.exe", vbNormalFocus) hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid) Debug.Print hProcess End SubPrivate Sub Command2_Click() Dim aa As Long If hProcess <> 0 Then aa = TerminateProcess(hProcess, 3018) End IfEnd SubPrivate Sub Form_Load() Me.Command1.Caption = "调用记事本" Me.Command2.Caption = "关闭记事本" End Sub
如何强迫shell所Create的process结束呢,那便是 Dim aa As Long If hProcess <> 0 Then aa = TerminateProcess(hProcess, 3838) End IfhProcess便是先前的例子中所取得的那个Process Handle, 3838所指的是传给 GetExitCodeProcess()中的第二叁数,这是我们任意给的,但最好不要是0,因为 0一般是代表正常结束,当然这样设也不会有错。当然不可设&H103,以这个例子来 看,如果程式正处於以下的LOOP Do Call GetExitCodeProcess(hProcess, ExitCode) Debug.Print ExitCode DoEvents Loop While ExitCode = STILL_ALIVE Debug.print ExitCode 而执行了 TerminateProcess(hProcess, 3838)那会看到ExitCode = 3838。然 而,这个方式在win95没问题,在NT中,可能您要在OpenProcess()的第一个叁数要 更改成 PROCESS_QUERY_INFORMATION Or PROCESS_TERMINATE 这样才能Work。不过 良心的建议,非到最後关头,不要使用TerminateProcess(),因不正常的结束,往 往许多程式结束前所要做的事都没有做,可能造成Resource的浪费,甚者,下次再 执行某些程式时会有问题
(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
Const PROCESS_QUERY_INFORMATION = &H400
Const STILL_ALIVE = &H103Dim hProcess As Long
Private Sub Command1_Click()
Dim pid As Long
pid = Shell("notepad.exe", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
Debug.Print hProcess
End SubPrivate Sub Command2_Click()
Dim aa As Long
If hProcess <> 0 Then
aa = TerminateProcess(hProcess, 3018)
End IfEnd SubPrivate Sub Form_Load()
Me.Command1.Caption = "调用记事本"
Me.Command2.Caption = "关闭记事本"
End Sub
Dim aa As Long
If hProcess <> 0 Then
aa = TerminateProcess(hProcess, 3838)
End IfhProcess便是先前的例子中所取得的那个Process Handle, 3838所指的是传给
GetExitCodeProcess()中的第二叁数,这是我们任意给的,但最好不要是0,因为
0一般是代表正常结束,当然这样设也不会有错。当然不可设&H103,以这个例子来
看,如果程式正处於以下的LOOP
Do
Call GetExitCodeProcess(hProcess, ExitCode)
Debug.Print ExitCode
DoEvents
Loop While ExitCode = STILL_ALIVE
Debug.print ExitCode 而执行了 TerminateProcess(hProcess, 3838)那会看到ExitCode = 3838。然
而,这个方式在win95没问题,在NT中,可能您要在OpenProcess()的第一个叁数要
更改成 PROCESS_QUERY_INFORMATION Or PROCESS_TERMINATE 这样才能Work。不过
良心的建议,非到最後关头,不要使用TerminateProcess(),因不正常的结束,往
往许多程式结束前所要做的事都没有做,可能造成Resource的浪费,甚者,下次再
执行某些程式时会有问题