背景:做另一个程序时用到的,把这个功能做成了单独的程序,今后调用会比较方便

适应症:1.正常情况下无法关闭的;2.关闭时不希望目标程序获得通知;3.阻止目标程序在关闭时的收尾工作(通常用来模拟掉线的情况)

工作原理:TerminateProcess()函数执行后,被终止的进程是不会得到任何关于程序退出的通知的。也就是说,被终止的进程是无法在结束运行前进行退出前的收尾工作的。所以,通常只有在其他任何方法都无法迫使进程退出时才会考虑使用TerminateProcess()去强制结束进程的。

调用方法:Forcekill QQ.exe(将QQ.exe替换为需要强制关闭的进程)

点击这里下载:ForceKill

源码:

  1. Private?Declare?Function?OpenProcess?Lib?"kernel32"?(ByVal?dwDesiredAccess?As?Long,?ByVal?bInheritHandle?As?Long,?ByVal?dwProcessId?As?Long)?As?Long
  2. Private?Declare?Function?CloseHandle?Lib?"kernel32"?(ByVal?hObject?As?Long)?As?Long
  3. Private?Const?SYNCHRONIZE?=?&H100000
  4. Private?Const?STANDARD_RIGHTS_REQUIRED?=?&HF0000
  5. Private?Const?PROCESS_ALL_ACCESS?=?(STANDARD_RIGHTS_REQUIRED?Or?SYNCHRONIZE?Or?&HFFF)
  6. Private?Declare?Function?NtSuspendProcess?Lib?"ntdll.dll"?(ByVal?hProc?As?Long)?As?Long
  7. Private?Declare?Function?NtResumeProcess?Lib?"ntdll.dll"?(ByVal?hProc?As?Long)?As?Long
  8. Private?Declare?Function?TerminateProcess?Lib?"kernel32"?(ByVal?hProcess?As?Long,?ByVal?uExitCode?As?Long)?As?Long
  9. Private?hProcess?As?Long
  10. Private?Declare?Function?CreateToolhelp32Snapshot?Lib?"kernel32"?(ByVal?dwFlags?As?Long,?ByVal?th32ProcessID?As?Long)?As?Long
  11. Private?Declare?Function?Process32First?Lib?"kernel32"?(ByVal?hSnapshot?As?Long,?lppe?As?PROCESSENTRY32)?As?Long
  12. Private?Declare?Function?Process32Next?Lib?"kernel32"?(ByVal?hSnapshot?As?Long,?lppe?As?PROCESSENTRY32)?As?Long
  13. Private?Type?PROCESSENTRY32
  14. ????????dwSize???As?Long
  15. ????????cntUsage???As?Long
  16. ????????th32ProcessID???As?Long
  17. ????????th32DefaultHeapID???As?Long
  18. ????????th32ModuleID???As?Long
  19. ????????cntThreads???As?Long
  20. ????????th32ParentProcessID???As?Long
  21. ????????pcPriClassBase???As?Long
  22. ????????dwFlags???As?Long
  23. ????????szExeFile???As?String?*?1024
  24. End?Type
  25. 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
  26. Const?TH32CS_SNAPHEAPLIST?=?&H1
  27. Const?TH32CS_SNAPPROCESS?=?&H2
  28. Const?TH32CS_SNAPTHREAD?=?&H4
  29. Const?TH32CS_SNAPMODULE?=?&H8
  30. Const?TH32CS_SNAPALL?=?(TH32CS_SNAPHEAPLIST?Or?TH32CS_SNAPPROCESS?Or?TH32CS_SNAPTHREAD?Or?TH32CS_SNAPMODULE)
  31. Const?TH32CS_INHERIT?=?&H80000000
  32. Public?pid?As?Long
  33. Public?pname?As?String
  34. Public?a?As?String
  35. Public?EXEPath?As?String
  36. Public?TimerCount?As?Long
  37. Public?CMDCaption?As?Long
  38. Public?ResumeTimer?As?Long
  39. Public?TerminateTimer?As?Long
  40. Public?TotalCount?As?Long
  41. Public?Sub?cmdTerminate()
  42. a?=?Trim(LCase(EXEPath))
  43. Dim?my?As?PROCESSENTRY32
  44. Dim?l?As?Long
  45. Dim?l1?As?Long
  46. Dim?flag?As?Boolean
  47. Dim?mName?As?String
  48. Dim?i?As?Integer
  49. l?=?CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,?0)
  50. If?l?Then
  51. ????my.dwSize?=?1060
  52. End?If
  53. If?(Process32First(l,?my))?Then
  54. ???Do
  55. ??????i?=?InStr(1,?my.szExeFile,?Chr(0))
  56. ??????mName?=?LCase(Left(my.szExeFile,?i?-?1))
  57. ??????If?mName?=?a?Then
  58. ??????????pid?=?my.th32ProcessID
  59. ??????End?If
  60. ??????If?hProcess?Then
  61. ?????????TerminateProcess?hProcess,?0
  62. ??????Else
  63. ????????If?IsNumeric(pid)?Then
  64. ????????????hProcess?=?OpenProcess(PROCESS_ALL_ACCESS,?False,?CLng(pid))
  65. ????????????If?hProcess?<>?0?Then
  66. ????????????????TerminateProcess?hProcess,?0
  67. ????????????End?If
  68. ????????End?If
  69. ??????End?If
  70. ???Loop?Until?(Process32Next(l,?my)?<?1)
  71. End?If
  72. End?Sub
  73. Private?Sub?Form_Load()
  74. Me.Visible?=?False
  75. EXEPath?=?Command
  76. If?EXEPath?=?""?Then?End
  77. Call?cmdTerminate
  78. End
  79. End?Sub

标签: Program, Windows

评论已关闭