Page 1 of 1

Kill running program

Posted: Sat Aug 06, 2005 10:05 am
by bidanh00co
Long time ago I found a VB source-code which help us to kill (force to close) another running program. Please help me a code to do like that in PureBasic !!
Thank you.

Posted: Sat Aug 06, 2005 10:55 am
by Max.
This is code I use. As stated in the remarks, it is something Siegfried Rings came up with and I just changed the behaviour a tad bit and added the RKillProcess stuff so noone needs the external program pskill.exe.

Have a look here: viewtopic.php?t=11593&highlight=

Code: Select all

;Kill a named process (like "explorer.exe")
;if you can't kill certain processes and really want to do, look for some code to
;assign the SeDebugPrivilege.
; 06.August 2005 - forum.purebasic.com - Max.
; Relies heavily (very heavily, just changed behaviour, not function, except for the RKillProcess procedure) on code by Siegfried Rings. 
; Reference thread: http://forums.purebasic.com/english/viewtopic.php?t=11593&highlight=

#NbProcessesMax = 10000 
Dim ProcessesArray(#NbProcessesMax) 

#PROCESS_ALL_ACCESS=$1F0FFF 
#PROCESS_QUERY_INFORMATION = $400
#PROCESS_VM_READ = $10

Procedure RKillProcess(pID) 

  pHandle = OpenProcess_(#PROCESS_ALL_ACCESS, #false, pID) 
  Result=TerminateProcess_(pHandle,0) 
  
  ProcedureReturn Result 

EndProcedure 

Procedure DoProcessListAndKill(KillTask.s) 



  If OpenLibrary(0, "psapi.dll") 
  
    EnumProcesses      = IsFunction(0, "EnumProcesses") 
    EnumProcessModules = IsFunction(0, "EnumProcessModules") 
    GetModuleBaseName  = IsFunction(0, "GetModuleBaseNameA") 
    GetModuleBaseNameFull  = IsFunction(0, "GetModuleFileNameExA")  
    Debug GetModuleBaseNameFull 
    If EnumProcesses And EnumProcessModules And GetModuleBaseName  ; Be sure we have detected all the functions 
      
      CallFunctionFast(EnumProcesses, ProcessesArray(), #NbProcessesMax, @nProcesses) 
      
      For k=1 To nProcesses/4 
        PID=ProcessesArray(k-1) 
        hProcess = OpenProcess_(#PROCESS_QUERY_INFORMATION | #PROCESS_VM_READ, 0, PID) 
        
        If hProcess 
          CallFunctionFast(EnumProcessModules, hProcess, @BaseModule, 4, @cbNeeded) 
          
          Name$ = Space(255) 
          CallFunctionFast(GetModuleBaseName, hProcess, BaseModule, @Name$, Len(Name$)) 
          FullName$=Space(1024) 
          CallFunctionFast(GetModuleBaseNameFull,hProcess, BaseModule, @FullName$, Len(FullName$)) 

          Debug Str(PID) + " "+ Name$ 
          Name$=Trim(LCase(Name$)) 
          CloseHandle_(hProcess) 
        
           If KillTask=Name$ 
            If RKillProcess(PID)  
               Debug (" Process "+KillTask+" killed as requested.") 
            EndIf 
           EndIf
        EndIf
        Next
      EndIf
    CloseLibrary(0)      
    EndIf 
      
EndProcedure 



DoProcessListAndKill("killme.exe")

Posted: Sat Aug 06, 2005 1:51 pm
by thefool
i can highly recommend NoahPhense's Process Library. Grap it here:
http://www.liquidbuzz.com/dev/

- lbdt_ProcessIt_FindProcess(partofwindowtitle.s)
- lbdt_ProcessIt_GetProcessFilename(partofwindowtitle.s)
- lbdt_ProcessIt_GetProcessID(partofwindowtitle.s)
- lbdt_ProcessIt_KillProcess(partofwindowtitle.s)

Posted: Sat Aug 06, 2005 7:29 pm
by akj
The method I prefer to use is:
SendNotifyMessage_(hWin, #WM_SYSCOMMAND, #SC_CLOSE, 0)
where hWin is a handle to the process's main window.
To determine hWin, you could perhaps use the API call FindWindow_().

Posted: Sun Aug 07, 2005 2:52 am
by bidanh00co
Thank you for all of you !!

There're many way to do, It make me comfused...
So please tell me which is the best ?? :)


- Oh, I read in Help file, see that PureBASIC support ASM Inline.
To my honest, I'm a beginner, an amateur...I know nothing about ASM...follow the instruction, I go to www.flatassembler.com but that link is dead. So, Would you please give me a Website address that give us full information about ASM (what is it, and how it work, and how to study it??)...I really want to know that.
Thank you.

Posted: Sun Aug 07, 2005 7:25 pm
by thefool
its http://flatassembler.net/ ;)

however, to learn it, search this forum. plenty of usefull links etc. also search google eg. however this is a fasm tutorial, http://www.decard.net/article.php

there are lots of other stuff at the internet too.