Завершить сторонний процесс - VB
Формулировка задачи:
Столкнулся со следующей проблемой:
Открыл из VB файл "C:\Documents and Settings\Евгений\Рабочий стол\День.exe"
Как его закрыть из VB ??
Спасибо за ответ..
Решение задачи: «Завершить сторонний процесс»
textual
Листинг программы
Option Explicit 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 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 Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long) Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Sub Kill_Process_API() Dim hSnapShot&, uProcess As PROCESSENTRY32, r&, rClose&, hProc&, ProcessName$, ProcessToClose$, ProcessFound As Boolean '********************************************************************** ProcessToClose = "notepad.exe" 'Задаем имя закрываемого процесса здесь. hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&) uProcess.dwSize = Len(uProcess) r = Process32First(hSnapShot, uProcess) Do While r ProcessName = Left$(uProcess.szExeFile, IIf(InStr(uProcess.szExeFile, Chr$(0)) > 0, InStr(uProcess.szExeFile, Chr$(0)) - 1, 0)) Debug.Print ProcessName If StrComp(ProcessName, ProcessToClose, vbTextCompare) = 0 Then ProcessFound = True hProc = OpenProcess(&H1, False, uProcess.th32ProcessID) TerminateProcess hProc, 0& rClose = Err.LastDllError Exit Do End If r = Process32Next(hSnapShot, uProcess) Loop CloseHandle hSnapShot If ProcessFound Then CloseHandle hProc If rClose = 0 Then MsgBox "Процесс " & ProcessToClose & " успешно завершен." Else MsgBox "Ошибка " & rClose & " при завершении процесса " & ProcessToClose End If Else MsgBox "Процесс " & ProcessToClose & " не найден!" End If End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д