|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我的edge浏览器不知道怎么设置的,关闭之后还有进程,加了结束进程树的代码,每次运行前先运行这个。
- Option Explicit
- #If VBA7 Then
- Declare PtrSafe Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As LongPtr
- Declare PtrSafe Function Process32First Lib "kernel32" (ByVal hSnapshot As LongPtr, uProcess As PROCESSENTRY32) As Long
- Declare PtrSafe Function Process32Next Lib "kernel32" (ByVal hSnapshot As LongPtr, uProcess As PROCESSENTRY32) As Long
- Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
- Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As LongPtr, ByVal uExitCode As Long) As Long
- #Else
- Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
- Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
- Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
- Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
- Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
- #End If
- Private Const TH32CS_SNAPPROCESS As Long = 2
- Private Const PROCESS_TERMINATE As Long = &H1
- Private Const MAX_PATH As Integer = 260
- 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 * MAX_PATH
- End Type
- Sub TerminateEdgeProcesses()
- Dim hSnapshot As LongPtr
- Dim uProcess As PROCESSENTRY32
- Dim hProcess As LongPtr
- Dim strProcessName As String
-
- ' 初始化结构体
- uProcess.dwSize = Len(uProcess)
-
- ' 创建进程快照
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
- If hSnapshot <> 0 Then
- ' 查找第一个进程
- If Process32First(hSnapshot, uProcess) Then
- Do
- ' 获取进程名
- strProcessName = Left(uProcess.szExeFile, InStr(1, uProcess.szExeFile, Chr(0)) - 1)
-
- ' 如果是Edge进程,则尝试结束
- If InStr(1, strProcessName, "msedge.exe", vbTextCompare) > 0 Then
- ' 打开进程
- hProcess = OpenProcess(PROCESS_TERMINATE, 0, uProcess.th32ProcessID)
- If hProcess <> 0 Then
- ' 结束进程
- TerminateProcess hProcess, 0
- CloseHandle hProcess
- End If
- End If
-
- Loop While Process32Next(hSnapshot, uProcess)
- End If
- ' 关闭快照句柄
- CloseHandle hSnapshot
- End If
- End Sub
复制代码 |
|