要關閉的處理程序無法使用Findwindow的api找到,只能在工作管理員的處理程序中找到,請問如何用vb來結束,或是NT也像XP和2000有提供結束程式的指令?
2007-09-29 01:18:32 · 2 個解答 · 發問者 小豪 2 in 電腦與網際網路 ➔ 程式設計
W.J.S. 的方法在windowns 2000能使用,但在NT無法使用,是否有其他方法?謝謝
2007-10-08 18:41:32 · update #1
'表單置1個ListBox及2個CommandButton
Dim WMI As Object
Private Sub Command1_Click() '列舉程式
Dim Obj As Object, S As String
List1.Clear
S = "Select * From Win32_Process"
For Each Obj In WMI.ExecQuery(S)
List1.AddItem Obj.Description
Next
Set Obj = Nothing
End Sub
Private Sub Command2_Click() '結束程式
If List1 = "" Then Exit Sub
Dim Obj As Object, S As String
S = "Select * From Win32_Process Where Name = '" & List1 & "'"
For Each Obj In WMI.ExecQuery(S)
Obj.Terminate
Next
List1.RemoveItem (List1.ListIndex)
Set Obj = Nothing
End Sub
Private Sub Form_Load()
Set WMI = GetObject("WinMgmts:")
End Sub
2007-10-11 01:47:47 補充:
因為沒有NT可試,無法幫你,建議你在Google輸入 "TerminateProcess" "NT" "VB" 去查詢,就會有很多資料跑出來.
2007-09-29 21:54:00 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
依照題意可以找到 Process ID ,在工作管理員下->檢視->選擇欄位將PID打勾!這時就可以看到每個程式所用的ProcessID
(程式中利用各種方法可以亦能得到PID!ex: shell函數的return)
取得PID後一切好辦
利用ProcessID 取得 ProcessHandle
利用TermateProcess傳入ProcessHandle 關閉程式
以下舉個較為一般的例子,先自行開一個小算盤利用程式關掉他。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Sub Command1_Click()
Dim hwnd&, Pid&, Phnd&
hwnd = FindWindow(vbNullString, "小算盤")
GetWindowThreadProcessId hwnd, Pid
Debug.print Pid
Phnd = OpenProcess(&H1, 0, Pid)
Debug.print Phnd
TerminateProcess Phnd, 0
CloseHandle Phnd
End Sub
2007-09-29 04:06:04 · answer #2 · answered by 鉦傑 3 · 0⤊ 0⤋