Page 1 of 1

Killing programs / processes with purebasic?

Posted: Wed Sep 22, 2010 8:12 am
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!

Re: Killing programs / processes with purebasic?

Posted: Wed Sep 22, 2010 1:54 pm
by blueznl
Well, it's not exactly 'killing' but you could send 'close' messages to each application.

Re: Killing programs / processes with purebasic?

Posted: Wed Sep 22, 2010 4:07 pm
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