Page 1 sur 1

killer un exe qui tourne

Publié : mar. 29/nov./2016 20:32
par ChaudEf
bonsoir, j'ai cherche sur le forum mais pas trouve de code que je pouvait comprendre
j'ai un exe, disons test.exe qui tourne sur le pc, je veux verifier si il tourne et si oui le killer.

j'ai vu ca http://www.purebasic.fr/french/viewtopi ... ll+process mais a vrai dire c'a m'a embrouiller

voila si qnn peut m'aider, merci bcp.

Re: killer un exe qui tourne

Publié : mar. 29/nov./2016 20:48
par ChaudEf
bon j'ai trouve un truc, mais je l'ais concote avec differents codes, il y a pllein de lignes en trop, mais enfin, ca marche

Code : Tout sélectionner

#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
#PSAPI = 9998


#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

Procedure.s ExePath(); - Return the path and name of the running execute
  Prg.s = Space(#MAX_PATH+1)
  GetModuleFileName_(GetModuleHandle_(0), @Prg, #MAX_PATH)
  ProcedureReturn Prg
EndProcedure

Procedure assassine(exe_name.s)
FileName.s=GetFilePart(ExePath())
If OpenLibrary (#PROCESS32LIB, "kernel32.dll")
  snap = CallFunction (#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
  If snap
    Define.PROCESSENTRY32 Proc32
    Proc32\dwSize = SizeOf (PROCESSENTRY32)
    If CallFunction (#PROCESS32LIB, "Process32First", snap, @Proc32)
      While CallFunction (#PROCESS32LIB, "Process32Next", snap, @Proc32)
        If PeekS (@Proc32\szExeFile)<>FileName.s
        	If Trim(LSet(PeekS (@Proc32\szExeFile),30)) = exe_name
        		KillProcess ( Val(RSet(Str(Proc32\th32ProcessID),4) ))
        		EndIf
        EndIf
      Wend     
    EndIf   
    CloseHandle_ (snap)
  EndIf
  CloseLibrary (#PROCESS32LIB)
EndIf
EndProcedure

RunProgram("calc.exe")
Delay(2000)
assassine("calc.exe")

Re: killer un exe qui tourne

Publié : mer. 30/nov./2016 12:30
par GallyHC
Bonjour,

J'ai testé tel quel et pour moi cela ne fonctionne pas. "Calc" ce lance bien, mais il n'est pas killer.

Cordialement,
GallyHC

Re: killer un exe qui tourne

Publié : mer. 30/nov./2016 12:38
par GallyHC
Bonjour,

Voila une version qui fonctionne pour moi.

Code : Tout sélectionner

DisableExplicit

; ****************************************************************************
; ****************************************************************************
; ****************************************************************************

Procedure.i GetProcessPidByName(sfilename.s) 
; ROUTINE DE RECHERCHE DU PID D'UN EXECUTABLE.
  Protected shot.i
  Protected ipid.i
  Protected tool.i
  Protected first.i
  Protected inext.i
  Protected found.i
  Protected name.s
  Protected entry.PROCESSENTRY32\dwSize = entry

  sfilename = UCase(sfilename)
  If OpenLibrary(0, "Kernel32.dll")
    tool  = GetFunction(0, "CreateToolhelp32Snapshot")
    first = GetFunction(0, "Process32First")
    inext = GetFunction(0, "Process32Next")
    If tool And first And inext
      shot  = CallFunctionFast(tool, #TH32CS_SNAPPROCESS, 0)
      If shot
        found = CallFunctionFast(first, shot, entry)
        While found
          name = GetFilePart(UCase(PeekS(@entry\szExeFile, -1, #PB_Ascii)))
          If name = sfilename
            ipid = entry\th32ProcessID
            Break
          EndIf
          found = CallFunctionFast(inext, shot, entry)          
        Wend
      EndIf
      CloseHandle_(shot)
    EndIf
    CloseLibrary(0)
  EndIf
  ProcedureReturn ipid

EndProcedure

Procedure SetKillProcessByPID(pid.i)
; ROUTINE DE KILL DES PROCESS WINDOWS.
  Define phandle.i
  Define result.b = #False

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

EndProcedure

; ****************************************************************************
; ****************************************************************************
; ****************************************************************************

RunProgram("calc.exe")

Delay(2000)

Define ipid = GetProcessPidByName("calc.exe")
If ipid <> 0
  SetKillProcessByPID(ipid)
EndIf
Cordialement,
GallyHC

Re: killer un exe qui tourne

Publié : mer. 30/nov./2016 17:47
par ChaudEf
merci beaucoup