進程結束命令及應用

來源:互聯網
上載者:User

這篇是我在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以上才有。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.