强制关闭进程 TerminateProcess
背景:做另一个程序时用到的,把这个功能做成了单独的程序,今后调用会比较方便
适应症:1.正常情况下无法关闭的;2.关闭时不希望目标程序获得通知;3.阻止目标程序在关闭时的收尾工作(通常用来模拟掉线的情况)
工作原理:TerminateProcess()函数执行后,被终止的进程是不会得到任何关于程序退出的通知的。也就是说,被终止的进程是无法在结束运行前进行退出前的收尾工作的。所以,通常只有在其他任何方法都无法迫使进程退出时才会考虑使用TerminateProcess()去强制结束进程的。
调用方法:Forcekill QQ.exe(将QQ.exe替换为需要强制关闭的进程)
点击这里下载:ForceKill
源码:
- Private?Declare?Function?OpenProcess?Lib?"kernel32"?(ByVal?dwDesiredAccess?As?Long,?ByVal?bInheritHandle?As?Long,?ByVal?dwProcessId?As?Long)?As?Long
- Private?Declare?Function?CloseHandle?Lib?"kernel32"?(ByVal?hObject?As?Long)?As?Long
- Private?Const?SYNCHRONIZE?=?&H100000
- Private?Const?STANDARD_RIGHTS_REQUIRED?=?&HF0000
- Private?Const?PROCESS_ALL_ACCESS?=?(STANDARD_RIGHTS_REQUIRED?Or?SYNCHRONIZE?Or?&HFFF)
- Private?Declare?Function?NtSuspendProcess?Lib?"ntdll.dll"?(ByVal?hProc?As?Long)?As?Long
- Private?Declare?Function?NtResumeProcess?Lib?"ntdll.dll"?(ByVal?hProc?As?Long)?As?Long
- Private?Declare?Function?TerminateProcess?Lib?"kernel32"?(ByVal?hProcess?As?Long,?ByVal?uExitCode?As?Long)?As?Long
- Private?hProcess?As?Long
- Private?Declare?Function?CreateToolhelp32Snapshot?Lib?"kernel32"?(ByVal?dwFlags?As?Long,?ByVal?th32ProcessID?As?Long)?As?Long
- Private?Declare?Function?Process32First?Lib?"kernel32"?(ByVal?hSnapshot?As?Long,?lppe?As?PROCESSENTRY32)?As?Long
- Private?Declare?Function?Process32Next?Lib?"kernel32"?(ByVal?hSnapshot?As?Long,?lppe?As?PROCESSENTRY32)?As?Long
- Private?Type?PROCESSENTRY32
- ????????dwSize???As?Long
- ????????cntUsage???As?Long
- ????????th32ProcessID???As?Long
- ????????th32DefaultHeapID???As?Long
- ????????th32ModuleID???As?Long
- ????????cntThreads???As?Long
- ????????th32ParentProcessID???As?Long
- ????????pcPriClassBase???As?Long
- ????????dwFlags???As?Long
- ????????szExeFile???As?String?*?1024
- End?Type
- Private?Declare?Function?ShellExecute?Lib?"shell32.dll"?Alias?"ShellExecuteA"?(ByVal?hwnd?As?Long,?ByVal?lpOperation?As?String,?ByVal?lpFile?As?String,?ByVal?lpParameters?As?String,?ByVal?lpDirectory?As?String,?ByVal?nShowCmd?As?Long)?As?Long
- Const?TH32CS_SNAPHEAPLIST?=?&H1
- Const?TH32CS_SNAPPROCESS?=?&H2
- Const?TH32CS_SNAPTHREAD?=?&H4
- Const?TH32CS_SNAPMODULE?=?&H8
- Const?TH32CS_SNAPALL?=?(TH32CS_SNAPHEAPLIST?Or?TH32CS_SNAPPROCESS?Or?TH32CS_SNAPTHREAD?Or?TH32CS_SNAPMODULE)
- Const?TH32CS_INHERIT?=?&H80000000
- Public?pid?As?Long
- Public?pname?As?String
- Public?a?As?String
- Public?EXEPath?As?String
- Public?TimerCount?As?Long
- Public?CMDCaption?As?Long
- Public?ResumeTimer?As?Long
- Public?TerminateTimer?As?Long
- Public?TotalCount?As?Long
- Public?Sub?cmdTerminate()
- a?=?Trim(LCase(EXEPath))
- Dim?my?As?PROCESSENTRY32
- Dim?l?As?Long
- Dim?l1?As?Long
- Dim?flag?As?Boolean
- Dim?mName?As?String
- Dim?i?As?Integer
- l?=?CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,?0)
- If?l?Then
- ????my.dwSize?=?1060
- End?If
- If?(Process32First(l,?my))?Then
- ???Do
- ??????i?=?InStr(1,?my.szExeFile,?Chr(0))
- ??????mName?=?LCase(Left(my.szExeFile,?i?-?1))
- ??????If?mName?=?a?Then
- ??????????pid?=?my.th32ProcessID
- ??????End?If
- ??????If?hProcess?Then
- ?????????TerminateProcess?hProcess,?0
- ??????Else
- ????????If?IsNumeric(pid)?Then
- ????????????hProcess?=?OpenProcess(PROCESS_ALL_ACCESS,?False,?CLng(pid))
- ????????????If?hProcess?<>?0?Then
- ????????????????TerminateProcess?hProcess,?0
- ????????????End?If
- ????????End?If
- ??????End?If
- ???Loop?Until?(Process32Next(l,?my)?<?1)
- End?If
- End?Sub
- Private?Sub?Form_Load()
- Me.Visible?=?False
- EXEPath?=?Command
- If?EXEPath?=?""?Then?End
- Call?cmdTerminate
- End
- End?Sub
评论已关闭