Kill running program

Just starting out? Need help? Post your questions and find answers here.
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Kill running program

Post 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.
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post 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")
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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)
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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_().
Anthony Jordan
bidanh00co
User
User
Posts: 56
Joined: Thu Jul 07, 2005 10:06 am

Post 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.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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.
Post Reply