#NoEnv
#SingleInstance Force
SetBatchLines -1

; https://autohotkey.com/boards/viewtopic.php?t=9556
; http://ahkscript.org/boards/viewtopic.php?f=5&t=3443

ProcessPrivilege(DllCall("Kernel32.dll\GetCurrentProcessId"), "SeDebugPrivilege")
Gui, Add, ListView, x0 y0 w615 h320 Sort vLst gLst +AltSubmit, PID|Name|PrivateUsage|WorkingSetSize
;Gui, Add, ListView, x0 y0 w615 h320 Sort, PID|Name|PrivateUsage|WorkingSetSize
;Gui, Add, ListView, x0 y0 w615 h320 Sort vLst gLst +AltSubmit, ProcessPID|ProcessName|PrivateUsage|WorkingSetSize
GuiControl, -Redraw, SysListView321

;GoSub, LoadProcessList
;Gui, 1: Show, Autosize, ProcessList

IL_Destroy(ImageList)
, ImageList := IL_Create()
, LV_Delete()
, LV_SetImageList(ImageList)
for k, v in ProcessEnum() {
  if !(IL_Add(ImageList, ProcessPath(k)))
    IL_Add(ImageList, A_WinDir "\explorer.exe")
  LV_Add("Icon" A_Index, k, v, FormatNumComma((i:=ProcessMemoryInfo(k)).PrivateUsage/1024) " KB"
      , FormatNumComma(i.WorkingSetSize/1024) "KB")
} LV_ModifyCol(1, "AutoHdr"), LV_ModifyCol(2, "AutoHdr")
GuiControl, +Redraw, SysListView321
Gui, Show, w618 h323, AHK Task Manager
SetTimer, Update, 2000
return

GuiEscape:
GuiClose:
ExitApp
return

Lst:
    If (A_GuiEvent = "DoubleClick") {
        LV_GetText(xPid, A_EventInfo, 1)
        LV_GetText(xNam, A_EventInfo, 2)
        MsgBox % "Pid`t" xpid "`nName`t" xNam
    }
    If (A_GuiEvent = "RightClick") {
        LV_GetText(xPid, A_EventInfo, 1)
        LV_GetText(xNam, A_EventInfo, 2)
        MsgBox, 36, , Pid: %xPid%`nName: %xNam%`n`nEnd process?
        IfMsgBox, Yes
        {
            If (SafeProcessKill(xPid))
{
                MsgBox, 64, , Process successfully ended!
                Reload ; Added
}
            Else
                MsgBox, 48, , Failure: Could not end the process!
        }
    }
Return

SafeProcessKill(p) {
    WinClose,ahk_pid %p%
    WinWaitClose, ahk_pid %p%, , 1
    If (ErrorLevel) ; Force kill
    {
        MsgBox, 36, , Process refuses to close.`nForce kill?
        IfMsgBox, No
            Return 0
        Process, Close, %p%
        Return ErrorLevel
    }
    Return 1
}

LoadProcessList:
 GuiControl, -Redraw, Lst
  LV_Delete()
  For Process in ComObjGet("WinMgmts:").ExecQuery("Select * from Win32_Process")
  {
    LV_Add("", Process.Name, Process.ProcessID)
  }
  LV_ModifyCol()
  LV_ModifyCol(2, "50 Integer")
  GuiControl, +Redraw, Lst
Return

Update:
SetTimer,, Off
Loop, % LV_GetCount() {
  LV_GetText(PID, A_Index, 1)
  LV_Modify(A_Index,,,, FormatNumComma((i:=ProcessMemoryInfo(PID)).PrivateUsage/1024) " KB"
  , FormatNumComma(i.WorkingSetSize/1024) "KB")
} SetTimer,, On
return

ProcessEnum() {
  dwSize := VarSetCapacity(lppe, A_PtrSize=4?296:304, 0), NumPut(dwSize, lppe, 0, "UInt")
  , hSnapshot := DLLCall("Kernel32.dll\CreateToolhelp32Snapshot", "UInt", 0x00000002, "UInt", 0)
  , DllCall("Kernel32.dll\Process32First", "Ptr", hSnapshot, "Ptr", &lppe), List := [],r:=hSnapshot?0:1
  Loop
    List[NumGet(lppe, 8, "UInt")] := StrGet(&lppe+(A_PtrSize=4?36:44), 260, "CP0")
  until !(DllCall("Kernel32.dll\Process32Next", "Ptr", hSnapshot, "UInt", &lppe))
  return List, DllCall("Kernel32.dll\CloseHandle", "Ptr", hSnapshot), ErrorLevel := r
}

ProcessMemoryInfo(ProcessName) {
  ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
  , hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0010|0x0400, "UInt", 0, "UInt", ProcessId)
  , nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 72 : 40), NumPut(nSize, memCounters)
  if !(DllCall("Psapi.dll\GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize)) {
    memCounters := "", nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 80 : 44), NumPut(nSize, memCounters)
    if !(DllCall("Kernel32.dll\K32GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize))
      return false, ErrorLevel := true
  } i := {}, i.PageFaultCount := NumGet(memCounters, 4, "UInt")
    , i.PeakWorkingSetSize := NumGet(memCounters, 8, "Ptr")
    , i.WorkingSetSize := NumGet(memCounters, A_PtrSize = 8 ? 16 : 12, "Ptr") 
    , i.QuotaPeakPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 24 : 16, "Ptr")
    , i.QuotaPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 32 : 20, "Ptr")
    , i.QuotaPeakNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 40 : 24, "Ptr")
    , i.QuotaNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 48 : 28, "Ptr")
    , i.PagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 56 : 32, "Ptr")
    , i.PeakPagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 64 : 36, "Ptr")
    , i.PrivateUsage := NumGet(memCounters, A_PtrSize = 8 ? 72 : 40, "Ptr")
  return i, DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess), ErrorLevel := false
}

ProcessExist(ProcessName) {
  Process, Exist, %ProcessName%
  return ErrorLevel
}

ProcessPath(ProcessName) {
  ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
  , hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400|0x0010, "UInt", 0, "UInt", ProcessId)
  , FileNameSize := VarSetCapacity(ModuleFileName, (260 + 1) * 2, 0) / 2
  if !(DllCall("Psapi.dll\GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
    if !(DllCall("Kernel32.dll\K32GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
      DllCall("Kernel32.dll\QueryFullProcessImageNameW", "Ptr", hProcess, "UInt", 1, "Str", ModuleFileName, "UIntP", FileNameSize)
  return ModuleFileName, DllCall("Kernel32.dll\CloseHandle", "Ptr", hProcess)
}

FormatNumComma(lpValue, Locale := 0x0400) {
  Locale := Locale="."?0x0456:Locale=","?0x0409:!Locale?0x0400:Locale
  if !(cchNumber := DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0))
    cchNumber := DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0)
    VarSetCapacity(lpNumberStr, cchNumber * 2)
    if !DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
    DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
    return SubStr(lpNumberStr, 1, StrLen(lpNumberStr) - 3)
}

ProcessPrivilege(ProcessName, Privileges) {
  ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
  , hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400, "UInt", 0, "UInt", ProcessId)
  , DllCall("Advapi32.dll\OpenProcessToken", "Ptr", hProcess, "UInt", 0x0020|0x00000008, "UIntP", hToken)
  , PrivilegesLuid := [], i := 0
  Loop, Parse, % Privileges, `,
    DllCall("Advapi32.dll\LookupPrivilegeValueW", "Ptr", 0, "Str", A_LoopField, "Int64P", lpLuid)
    , PrivilegesLuid[A_Index] := lpLuid
  for k, v in PrivilegesLuid
    ti := "", VarSetCapacity(ti, 16, 0), NumPut(1, ti, 0, "UInt"), NumPut(v, ti, 4, "Int64"), NumPut(2, ti, 12, "UInt")
    , i := i+(DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", hToken, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)?1:0)
  DllCall("kernel32.dll\CloseHandle", "Ptr", hToken), DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess)
  return i, ErrorLevel := PrivilegesLuid.MaxIndex()!=i
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。