這篇是我在CSDN的BLOG中寫的,到這裡來儲存一下。
一、CMD中可執行檔結束進程命令(其實是一個進程調試工具)
ntsd -c q -p pid (pid 為進程標識符,在工作管理員中可以調出這一屬性列)
例:
如explorer.exe的pid為1332,則
運行:
ntsd -c q -p 1332就能結束explorer.exe進程
ntsd -c q -pn ***.exe (***.exe 為進程名,exe不能省)
例:
運行:ntsd -c q -pn explorer.exe就結束explorer.exe進程
二、應用
因為這個命令要比工作管理員中的結束進程功能強,所以我另外做了一個進程結束器,以VB為環境,利用ntsd命令和shell函數就能搞出來了。
思路:
先建立一個批次檔(直接讓CMD接受命令變數感覺不行,而這個檔案可以直接在CMD中執行),,預先寫入ntsd -c q -p ,然後接受輸入的pid,傳送PID到BAT檔案,點擊按鈕執行BAT檔案。
因為涉及檔案操作,所以要在工程中引用microsoft scripting runtime
下面是代碼:
Dim ts As New FileSystemObject
Dim tf As TextStream
Private Sub Command1_Click()
Set ts = CreateObject("Scripting.FileSystemObject")
Set tf = ts.CreateTextFile("d:\1.bat")
tf.Write ("ntsd -c q -p ") '預先寫好前段命令
tf.Write (Text1.Text) '等待寫入進程PID
tf.Close
Shell "D:\1.bat", vbMinimizedFocus '最小化執行結束進程命令
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell "cmd /c del d:\1.bat", vbMinimizedFocus '關閉時刪除臨時檔案
End Sub
另外一個程式,這個是用輸‘進程名’並用‘winexec’來結束進程的代碼:
Dim ts As New FileSystemObject
Dim tf As TextStream
Dim df As File
Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Private Sub Command1_Click()
Set ts = CreateObject("Scripting.FileSystemObject")
Set tf = ts.CreateTextFile("d:\1.bat")
tf.Write ("ntsd -c q -pn ") '預先寫好前段命令
tf.Write (Text1.Text) '等待寫入進程PID
tf.Close
WinExec "D:\1.bat", 3 '執行命令
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set df = ts.GetFile("d:\1.bat")
ts.DeleteFile (df) '關閉時刪除臨時檔案,跟上面方法不一樣
End Sub
其實taskkill的進程式控制制功能更強,不過只有WINXP以上才有。