Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Check if a Process Exists. If it does, kill it.



  • Please log in to reply
18 replies to this topic
GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

Try also this:

Process, Exist, %Table%.exe
If !ErrorLevel ; is not running
   return ; do nothing
; else
pid := ErrorLevel
WinClose, % "ahk_pid " pid



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Can you test this?

Table := "chrome"

KillProcess(Table)

KillProcess(proc)
{
    static SYNCHRONIZE                 := 0x00100000
    static STANDARD_RIGHTS_REQUIRED    := 0x000F0000
    static OSVERSION                   := (A_OSVersion = "WIN_XP" ? 0xFFF : 0xFFFF)
    static PROCESS_ALL_ACCESS          := STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | OSVERSION

    local tPtr := pPtr := nTTL := 0, PList := ""
    if !(DllCall("wtsapi32.dll\WTSEnumerateProcesses", "Ptr", 0, "Int", 0, "Int", 1, "PtrP", pPtr, "PtrP", nTTL))
        return "", DllCall("kernel32.dll\SetLastError", "UInt", -1)

    tPtr := pPtr
    loop % (nTTL)
    {
        if (InStr(PList := StrGet(NumGet(tPtr + 8)), proc))
        {
            PID := NumGet(tPtr + 4, "UInt")
            if !(hProcess := DllCall("kernel32.dll\OpenProcess", "UInt", PROCESS_ALL_ACCESS, "UInt", FALSE, "UInt", PID, "Ptr"))
                return DllCall("kernel32.dll\GetLastError")
            if !(DllCall("kernel32.dll\TerminateProcess", "Ptr", hProcess, "UInt", 0))
                return DllCall("kernel32.dll\GetLastError")
            if !(DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess))
                return DllCall("kernel32.dll\GetLastError")
        }
        tPtr += (A_PtrSize = 4 ? 16 : 24)
    }
    DllCall("wtsapi32.dll\WTSFreeMemory", "Ptr", pPtr)

    return "", DllCall("kernel32.dll\SetLastError", "UInt", nTTL)
}

[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

Spektre
  • Members
  • 6 posts
  • Last active: Dec 12 2014 10:54 PM
  • Joined: 12 Dec 2014
✓  Best Answer

Simple issue.  My original code and the various suggestions here have the wrong logic.

 

It should be:

 

If (ErrorLevel)

 

not:

 

If (!ErrorLevel)

 

 

I still have some variable usage problem in my function version, but this fixes the inline version.

 

Thanks all

 



flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009

If you only want to do what the title states,  this thing can be simplified using a one-liner:

Process, Close, %table%.exe

No Process Exist statement, and no if / else structure.  If the process exists, it will be closed. If it does not exists, nothing happens (and the errorlevel is set to 0 so you can check that it has indeed closed).


Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”