Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Private hProcess As Long
-----------------在程序中------------------
Dim pid As Long
pid = Shell("notepad.exe", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
----------------如果要關(guān)閉-----------------
If hProcess <> 0 Then
aa = TerminateProcess(hProcess, 3838)
End If
***********************************************************************************************************
ExitProcess
VB聲明
Declare Sub ExitProcess Lib "kernel32" Alias "ExitProcess" (ByVal uExitCode As Long)
說明
中止一個進程
參數(shù)表
參數(shù) 類型及說明
uExitCode Long,,指定想中斷的那個進程的一個退出代碼
在VB中使用
應盡量避免用該函數(shù)來關(guān)閉進程,。不要在自己的VB程序中使用它。此時,,應試著向要關(guān)閉的那個程序的主窗口投遞一條WM_CLOSE消息
***************************************************************************************************************
Option Explicit
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessID As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private hProcess As Long
'打開進程
Private Sub Command1_Click()
Dim pid As Long
pid = Shell("c:/winnt/System32/calc.exe", vbNormalFocus)
If pid = 0 Then
MsgBox "沒有打開程序"
Else
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid)
End If
End Sub
'關(guān)閉進程
Private Sub Command2_Click()
Dim l As Long
l = TerminateProcess(hProcess, 1)
If l <> 0 Then
MsgBox "成功關(guān)閉"
Else
MsgBox "未關(guān)閉"
End If
End Sub
|