IT-Consultant: Patrice Terrier > Useful TIPS

TIP - Kill a process matching a full PathName

(1/1)

Patrice Terrier:
Kill a process matching a full PathName


--- Code: ---#COMPILE EXE "KillProcess.exe"

%TH32CS_SNAPALL     = 15
%MAX_PATH           = 260
%PROCESS_TERMINATE  = &H0001
%PROCESS_QUERY_INFORMATION = &H0400
%PROCESS_VM_READ    = &H0010

TYPE PROCESSENTRY32
   dwSize AS DWORD
   cntUsage AS DWORD
   th32ProcessID AS DWORD
   th32DefaultHeapID AS DWORD PTR
   th32ModuleID AS DWORD
   cntThreads AS DWORD
   th32ParentProcessID AS DWORD
   pcPriClassBase AS LONG
   dwFlags AS LONG
   szExeFile AS ASCIIZ * %MAX_PATH
END TYPE

DECLARE FUNCTION CreateToolhelp32Snapshot LIB "kernel32.dll" ALIAS "CreateToolhelp32Snapshot" (BYVAL dwFlags AS DWORD, BYVAL th32processID AS DWORD) AS LONG
DECLARE FUNCTION Process32First LIB "kernel32.dll" ALIAS "Process32First" ( BYVAL hHandle AS LONG, rProc AS PROCESSENTRY32) AS LONG
DECLARE FUNCTION Process32Next LIB "kernel32.dll" ALIAS "Process32Next" ( BYVAL hHandle AS LONG, rProc AS PROCESSENTRY32) AS LONG
DECLARE FUNCTION TerminateProcess LIB "KERNEL32.DLL" ALIAS "TerminateProcess" (BYVAL hProcess AS DWORD, BYVAL uExitCode AS DWORD) AS LONG
DECLARE FUNCTION CloseHandle LIB "KERNEL32.DLL" ALIAS "CloseHandle" (BYVAL hObject AS DWORD) AS LONG
DECLARE FUNCTION OpenProcess LIB "KERNEL32.DLL" ALIAS "OpenProcess" (BYVAL dwDesiredAccess AS DWORD, BYVAL bInheritprocHandle AS LONG, BYVAL dwProcessId AS DWORD) AS DWORD
DECLARE FUNCTION GetModuleFileNameEx LIB "PSAPI.DLL" ALIAS "GetModuleFileNameExA" (BYVAL hProcess AS DWORD, BYVAL hModule AS DWORD, lpFilename AS ASCIIZ, BYVAL nSize AS DWORD) AS DWORD

DECLARE FUNCTION zTrace LIB "zTrace.DLL" ALIAS "zTrace" (zMessage AS ASCIIZ) AS LONG

FUNCTION killProcessByFullPathName(zFileName AS ASCIIZ) AS LONG
    LOCAL hSnapShot, hRes, hProcess, DoKill, nCountKill AS LONG
    LOCAL pe32 AS PROCESSENTRY32
    LOCAL szPathName AS ASCIIZ * %MAX_PATH

    zFileName = LCASE$(zFileName)

    hSnapShot = CreateToolhelp32Snapshot(%TH32CS_SNAPALL, 0)
    IF hSnapShot <> -1 THEN '// %INVALID_HANDLE_VALUE
       pe32.dwSize = SIZEOF(pe32)
       hRes = Process32First(hSnapShot, pe32)
       WHILE hRes
          hProcess = OpenProcess(%PROCESS_QUERY_INFORMATION OR %PROCESS_VM_READ, 1, pe32.th32ProcessID)
          IF hProcess THEN
             DoKill = 0
             IF GetModuleFileNameEx(hProcess, 0, szPathName, SIZEOF(szPathName)) THEN
                IF LCASE$(szPathName) = zFileName THEN
                   INCR nCountKill
                   DoKill = -1 '// OK, we have found one
'//                zTrace(">>"+szPathName+"<>"+zFileName+"<")
                END IF
             END IF
             CloseHandle(hProcess)
             IF DoKill THEN '// Kill the process we just found
                hProcess = OpenProcess(%PROCESS_TERMINATE, 0, pe32.th32ProcessID)
                IF hProcess THEN
                   TerminateProcess(hProcess, 9)
                   CloseHandle(hProcess)
                END IF
             END IF
          END IF
          hRes = Process32Next(hSnapShot, pe32)
       WEND
       CloseHandle(hSnapShot)
    END IF
    FUNCTION = nCountKill
END FUNCTION

FUNCTION PBMAIN()
    LOCAL zFileName AS ASCIIZ * %MAX_PATH
    zFileName = EXE.Path$ + "fwp.exe"
    MsgBox "Kill count =" + STR$(killProcessByFullPathName(zFileName)),,"Kill process"
END FUNCTION

--- End code ---

Navigation

[0] Message Index

Go to full version