Prozess killen

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
X360 Andy
Beiträge: 1206
Registriert: 11.05.2008 00:22
Wohnort: Bodensee
Kontaktdaten:

Beitrag von X360 Andy »

Hab mal auch en Task Manager zusammengeschustert...
Vieleicht hilft der Code dir.... (Beispiel haft ist der warscheinlich nicht aber egal :D)

Damit richtig geht, Icon beim Compiler einstellen ( egal welches )

Code: Alles auswählen

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Button_0
  #Listview_0
  #Text_0
EndEnumeration


Procedure  Main()
  If OpenWindow(#Window_0, 595, 154, 238, 330, "Task Killer",  #PB_Window_SystemMenu  | #PB_Window_TitleBar )
      ButtonGadget(#Button_0, 140, 300, 80, 20, "Killen")
      ListViewGadget(#Listview_0, 20, 10, 200, 270)
      TextGadget(#Text_0, 20, 300, 120, 20, "")
  EndIf
EndProcedure


#TH32CS_SNAPHEAPLIST = $1
#TH32CS_SNAPPROCESS = $2
#TH32CS_SNAPTHREAD = $4
#TH32CS_SNAPMODULE = $8
#TH32CS_SNAPALL = #TH32CS_SNAPHEAPLIST | #TH32CS_SNAPPROCESS | #TH32CS_SNAPTHREAD | #TH32CS_SNAPMODULE
#TH32CS_INHERIT = $80000000
#INVALID_HANDLE_VALUE = -1
#MAX_PATH = 260
#PROCESS32LIB = 9999

Global NewList Proc32.PROCESSENTRY32()

Procedure InitProcess32()
  ProcedureReturn OpenLibrary(#PROCESS32LIB, "kernel32.dll")
EndProcedure

Procedure CloseProcess32()
  ProcedureReturn CloseLibrary(#PROCESS32LIB)
EndProcedure

Procedure CreateProcessList()
  ClearList(Proc32())
  ProcedureReturn CallFunction(#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
EndProcedure

Procedure FreeProcessList(snapshot)
  ; Free process list (.PROCESSENTRY32 structures)...
  ClearList(Proc32())
  ; Close snapshot handle...
  ProcedureReturn CloseHandle_(snapshot)
EndProcedure

Procedure GetFirstProcess(snapshot)
  ; Allocate a new .PROCESSENTRY32 structure and fill in SizeOf (structure)...
  AddElement(Proc32())
  Proc32()\dwSize = SizeOf(PROCESSENTRY32)
  ; Call Process32First with snapshot handle and pointer to structure...
  If CallFunction(#PROCESS32LIB, "Process32First", snapshot, @Proc32())
    ProcedureReturn #True
  Else
    ; Free the structure if function call failed...
    DeleteElement(Proc32())
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure GetNextProcess(snapshot)
  ; Allocate a new .PROCESSENTRY32 structure and fill in SizeOf (structure)...
  AddElement(Proc32())
  Proc32()\dwSize = SizeOf(PROCESSENTRY32)
  ; Call Process32Next with snapshot handle and pointer to structure...
  If CallFunction(#PROCESS32LIB, "Process32Next", snapshot, @Proc32())
    ProcedureReturn #True
  Else
    ; Free the structure if function call failed...
    DeleteElement(Proc32())
    ProcedureReturn #False
  EndIf
EndProcedure

Structure PROCESSENTRY32s
  dwsize.l
  cntusage.l
  th32ProcessID.l
  th32DefaultHeapID.l
  th32ModuleID.l
  cntThreads.l
  th32ParentProcessID.l
  pcPriClassBase.l
  dwFlags.l
  szExeFile.s{1024}
EndStructure
#TH32CS_SNAPPROCESS = $2

Procedure.l FindPid(s.s)
  Process.PROCESSENTRY32s
  ProcSnap.l
  ProcSnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If ProcSnap<>0
    Process\dwsize = SizeOf(Process)
    Process32First_(ProcSnap, Process)
    While Process32Next_(ProcSnap, Process)>0
      If Process\szExeFile = s
        ProcedureReturn Process\th32ProcessID
        Break
      EndIf
    Wend
  EndIf
EndProcedure
; --------------------- Get PID end

#PROCESS_TERMINATE = $1
#PROCESS_CREATE_THREAD = $2
#PROCESS_VM_OPERATION = $8
#PROCESS_VM_READ = $10
#PROCESS_VM_WRITE = $20
#PROCESS_DUP_HANDLE = $40
#PROCESS_CREATE_PROCESS = $80
#PROCESS_SET_QUOTA = $100
#PROCESS_SET_INFORMATION = $200
#PROCESS_QUERY_INFORMATION = $400
#PROCESS_ALL_ACCESS = #STANDARD_RIGHTS_REQUIRED | #SYNCHRONIZE | $FFF


Procedure KillProcess(pid)
  phandle = OpenProcess_(#PROCESS_TERMINATE, #False, pid)
  If phandle<>#Null
    If TerminateProcess_(phandle, 1)
      result = #True
    EndIf
    CloseHandle_(phandle)
  EndIf
  ProcedureReturn result
EndProcedure



Main()
If InitProcess32()
  snapshot = CreateProcessList()
  If snapshot
    If GetFirstProcess(snapshot)
      Repeat
        result = GetNextProcess(snapshot)
      Until result = #False
    EndIf
    ResetList(Proc32())
    While NextElement(Proc32())
      AddGadgetItem(#Listview_0, -1, PeekS(@Proc32()\szExeFile))
    Wend
    FreeProcessList(snapshot)
  EndIf
  CloseProcess32()
EndIf
SetWindowTitle(#Window_0," Task Killer "+Str(CountGadgetItems(#Listview_0))+" Tasks")
Repeat
  event = WaitWindowEvent(50)
  
  Select event
    Case #PB_Event_SysTray
      If EventType() = #PB_EventType_LeftDoubleClick
        HideWindow(#Window_0, 0)
        RemoveSysTrayIcon(1)
         ClearGadgetItems(#Listview_0)
          SetGadgetText(#Text_0, "")
          
          If InitProcess32()
            snapshot = CreateProcessList()
            If snapshot
              If GetFirstProcess(snapshot)
                Repeat
                  result = GetNextProcess(snapshot)
                Until result = #False
              EndIf
              ResetList(Proc32())
              While NextElement(Proc32())
                AddGadgetItem(#Listview_0, -1, PeekS(@Proc32()\szExeFile))
              Wend
              FreeProcessList(snapshot)
            EndIf
            CloseProcess32()
          EndIf
          SetWindowTitle(#Window_0," Task Killer "+Str(CountGadgetItems(#Listview_0))+" Tasks")

        ElseIf EventType() = #PB_EventType_RightClick
       End
        EndIf
        
    Case #PB_Event_CloseWindow
      HideWindow(#Window_0, 1)
      AddSysTrayIcon(1, WindowID(#Window_0), GetClassLongPtr_(WindowID(#Window_0), #GCL_HICON)) 
    Case #PB_Event_Gadget
      
      Select EventGadget()
        Case #Listview_0
          SetGadgetText(#Text_0, GetGadgetText(#Listview_0))
          
        Case #Button_0
          KillProcess(FindPid(GetGadgetText(#Listview_0)))
          ClearGadgetItems(#Listview_0)
          SetGadgetText(#Text_0, "")
          
          If InitProcess32()
            snapshot = CreateProcessList()
            If snapshot
              If GetFirstProcess(snapshot)
                Repeat
                  result = GetNextProcess(snapshot)
                Until result = #False
              EndIf
              ResetList(Proc32())
              While NextElement(Proc32())
                AddGadgetItem(#Listview_0, -1, PeekS(@Proc32()\szExeFile))
              Wend
              FreeProcessList(snapshot)
            EndIf
            CloseProcess32()
          EndIf
          SetWindowTitle(#Window_0," Task Killer "+Str(CountGadgetItems(#Listview_0))+" Tasks")

      EndSelect
  EndSelect
  
Until exit = 1
Antworten