玩酷之家

 找回密码
 立即注册
查看: 84|回复: 0

VB 根据文件名结束应用程序进程

[复制链接]
发表于 2021-3-12 16:35:46 | 显示全部楼层 |阅读模式

遍历查找记事本程序,找到后结束进程

  1. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  2. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  3. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  4. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
  5. Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
  6. Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long '
  7. Private Const TH32CS_SNAPPROCESS = &H2
  8. Private Const PROCESS_ALL_ACCESS = &H100000 + &HF0000 + &HFFF

  9. Private Type PROCESSENTRY32
  10.     dwSize As Long
  11.     cntUseage As Long
  12.     th32ProcessID As Long
  13.     th32DefaultHeapID As Long
  14.     th32ModuleID As Long
  15.     cntThreads As Long
  16.     th32ParentProcessID As Long
  17.     pcPriClassBase As Long
  18.     swFlags As Long
  19.     szExeFile As String * 1024
  20. End Type



  21. Private Sub Form_Load()

  22. Dim MySnapHandle As Long
  23. Dim hProcess As Long
  24. Dim ProcessInfo As PROCESSENTRY32
  25.     MySnapHandle = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
  26.     ProcessInfo.dwSize = Len(ProcessInfo)
  27.     If ProcessFirst(MySnapHandle, ProcessInfo) <> 0 Then
  28.         Do
  29.             If Left(LCase(ProcessInfo.szExeFile), InStr(ProcessInfo.szExeFile, ".") + 3) = "notepad.exe" Then
  30.             '遍历进程,查找notepad.exe记事本程序,找到后执行操作.......,进程名全小写
  31.                hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessInfo.th32ProcessID)
  32.              TerminateProcess hProcess, 1
  33.              CloseHandle hProcess
  34.             End If
  35.         Loop While ProcessNext(MySnapHandle, ProcessInfo) <> 0
  36.     End If
  37.     CloseHandle MySnapHandle
  38.    
  39. End Sub
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号 )

GMT+8, 2021-4-22 11:41 , Processed in 1.076402 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表