tasklist could go missing. I whipped up
a little something for you.
Just be-careful though. Killing
process by name depending on the use could have undesirable consequences. If you killing
a unique
process name belonging to your package, no problem. If you targeting something else.., you could terminate the wrong target.
I could have several Notepad.exe processes running, however I might only want to target specific one. Targeting by
process name isn't good approach in this case.
In the example code, I simply have it stop looking after successful kill of
a target. However, like I was saying, doesn't necessarily mean I've hit my mark.
Code: Select all
If OSVersion() < #PB_OS_Windows_7
MessageRequester("Windows OS Requirements: ", "Windows 7 and higher", #PB_MessageRequester_Ok)
End
EndIf
;- Initialize Kernel32 Prototypes
Prototype.l EnumProcesses(*pProcessIds, cb.l, pBytesReturned.l)
Prototype.l EnumProcessModules(hProcess, lphModule.l, cb.l, lpcbNeeded.l)
Prototype.l GetModuleBaseName(hProcess, hModule.l, *lpBaseName, nSize.l)
Procedure.b Kernel32_Init()
Shared Kernel32
Protected Retr.b, fExt.s
Kernel32 = OpenLibrary(#PB_Any, "Kernel32.dll")
If Kernel32 <> 0
CompilerIf #PB_Compiler_Unicode : fExt = "W" : CompilerElse : fExt = "A" : CompilerEndIf
Global EnumProcesses.EnumProcesses = GetFunction(Kernel32, "K32EnumProcesses")
Global EnumProcessModules.EnumProcessModules = GetFunction(Kernel32, "K32EnumProcessModules")
Global GetModuleBaseName.GetModuleBaseName = GetFunction(Kernel32, "K32GetModuleBaseName"+fExt)
Retr = 1
EndIf
ProcedureReturn Retr
EndProcedure
;- DeInitialize Kernel32 Prototypes
Procedure.b Kernel32_End()
Shared Kernel32
CloseLibrary(Kernel32)
EndProcedure
Procedure.l EnumProcessesSmart(nInitNumProc.l = 1024)
; Enumerate all running processes (independent of the number of processes running in the system)
; 'nInitNumProc' = initial number of processes To allocate the Array For
; (it will be increased If there're more processes running)
Protected.l nNumProc = nInitNumProc, dwNumProcUsed, LoopSafety, ProcCount
Global Dim pIDs.l(nInitNumProc) : nNumProc = nInitNumProc
Repeat
If EnumProcesses(@pIDs(), nNumProc * SizeOf( LONG ), @dwcbBytesNeeded.l)
dwNumProcUsed = dwcbBytesNeeded / SizeOf(LONG)
If dwNumProcUsed < nNumProc
nNumProc = dwNumProcUsed
ReDim pIDs(nNumProc-1)
ProcCount = nNumProc
Break
Else
; Increase our Array size for the next iteration
nNumProc + nInitNumProc
ReDim pIDs(nNumProc)
EndIf
EndIf
LoopSafety + 1
Until LoopSafety > 4
If Not ProcCount
FreeArray(pIDs()) : nNumProc = 0
EndIf
ProcedureReturn nNumProc
EndProcedure
Procedure.l KillProcName(ProcessByName.s)
Protected.l ProcCount, Retr, State.b
If Kernel32_Init()
If EnumProcessesSmart(200)
Protected hProcess, szProcessName.s = Space(#MAX_PATH)
For k=1 To ArraySize(pIDs())
hProcess = OpenProcess_( #PROCESS_ALL_ACCESS, #False, pIDs(k))
If hProcess
If EnumProcessModules(hProcess, #Null, #Null, @cbNeeded.l)
GetModuleBaseName(hProcess, #Null, @szProcessName, cbNeeded)
; find the process and kill it
If LCase(szProcessName) = LCase(ProcessByName)
Retr = #WAIT_OBJECT_0
While Retr = #WAIT_OBJECT_0
; use WaitForSingleObject to make sure it's dead
Retr = WaitForSingleObject_(hProcess, 100)
TerminateProcess_(hProcess, 0)
Wend
State = #True
Break
EndIf
EndIf
CloseHandle_(hProcess)
EndIf
Next
FreeArray(pIDs())
EndIf
Kernel32_End()
EndIf
ProcedureReturn State
EndProcedure
If KillProcName("Notepad.exe")
Debug "Successfully Killed Notepad.exe"
EndIf