Killing programs / processes with purebasic?

Just starting out? Need help? Post your questions and find answers here.
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

Killing programs / processes with purebasic?

Post by PresFox »

Hello everyone,

I have a fairly show PC, and before i start a heavy game i have to kill all background programs (like MSN, Internet Explorer, Windows Explorer, etc)

Currently i am doing that manually every time via the task manager, but i wonder, is it with purebasic possible to kill those programs automatically? Like, creating a "Game mode" for my pc?

The program would have to check or a certain service, (like msnmessenger.exe) is running, and if so, kill it.

Can this be done, and if so how?

Not looking for code, a general direction where to look will suffice.

Thanks in advance!
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Killing programs / processes with purebasic?

Post by blueznl »

Well, it's not exactly 'killing' but you could send 'close' messages to each application.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Killing programs / processes with purebasic?

Post by cas »

Code: Select all

SendMessage_(FindWindow_(0, "Windows Live Messenger"), #WM_CLOSE, 0, 0)
It is like closing window with [ X ] button on window.
Or use TerminateProcess_() which is same as killing process with TaskManager:

Code: Select all

EnableExplicit

Procedure GetPidFromName(Name.s)
  Protected result
  Protected Process.PROCESSENTRY32
  Protected ProcSnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If ProcSnap <> #ERROR_INVALID_HANDLE
    Process\dwsize = SizeOf(PROCESSENTRY32)
    If Process32First_(ProcSnap, Process) = #True
      While Process32Next_(ProcSnap, Process) <> #False
        If Trim(PeekS(@Process\szExeFile,#MAX_PATH)) = Name.s
          result = Process\th32ProcessID
          Break
        EndIf
      Wend
    EndIf
    CloseHandle_(ProcSnap)
  EndIf
  ProcedureReturn result
EndProcedure

Procedure TerminateProcess(Name.s, ExitCode = 0)
  Protected result
  Protected processID = GetPidFromName(Name.s)
  If processID
    Protected hProcess  = OpenProcess_(#PROCESS_TERMINATE, #False, processID)
    If hProcess
      If TerminateProcess_(hProcess, ExitCode)
        result = #True
      EndIf
      CloseHandle_(hProcess)
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

;test:
If TerminateProcess("msnmsgr.exe")
  Debug "success"
Else
  Debug "failed to terminate process"
EndIf
Post Reply