Skip to content

Instantly share code, notes, and snippets.

@jNizM
Created May 9, 2016 08:41
Show Gist options
  • Save jNizM/79acc96acbd2cb1d060ffcb9776014f6 to your computer and use it in GitHub Desktop.
Save jNizM/79acc96acbd2cb1d060ffcb9776014f6 to your computer and use it in GitHub Desktop.
[AHK] ProcessExplorer (v0.07 beta)
; ===============================================================================================================================
; Title .........: ProcessExplorer
; AHK Version ...: 1.1.23.05 x64 Unicode
; Win Version ...: Windows 10 Professional - x64
; Description ...: Log Remote Session Information
; Version .......: v0.07 Beta
; Modified ......: 2016.04.20-1653
; Author(s) .....: jNizM
; ===============================================================================================================================
;@Ahk2Exe-SetName ProcessExplorer
;@Ahk2Exe-SetDescription ProcessExplorer
;@Ahk2Exe-SetVersion v0.07
;@Ahk2Exe-SetCopyright Copyright (c) 2016-2016`, jNizM
;@Ahk2Exe-SetOrigFilename ProcessExplorer.ahk
; ===============================================================================================================================
; https://code.msdn.microsoft.com/windowsdesktop/Use-PerformanceCounter-to-272d57a1
; https://msdn.microsoft.com/en-us/library/aa965225(v=vs.85).aspx (Memory Performance Information)
; GLOBAL SETTINGS ===============================================================================================================
;#Warn
#NoEnv
#SingleInstance Force
#Persistent
;#NoTrayIcon
SetBatchLines -1
global LV_Header := "Process Name|CPU|Private Bytes|Working Set|PID|User Name|Handles|Threads|Peak Working Set|PagefileUsage|PeakPagefileUsage|CPU Time|UserTime"
global LV_Size := ["150", "50 Right", "80 Right", "80 Right", "40 Right", "200", "60 Right", "60 Right", "90 Right", "90 Right", "90 Right", "90 Right", "90 Right"]
; GUI ===========================================================================================================================
Gui, Margin, 5, 5
Gui, Add, ListView, xm ym w1200 h600, % LV_Header
loop % LV_Size.MaxIndex()
LV_ModifyCol(A_Index, LV_Size[A_Index])
Gui, Add, Edit, xm y+5 w200 h21 hWndhSearch vprcsearch
DllCall("SendMessage", "ptr", hSearch, "uint", 0x1501, "ptr", 1, "str", "Prozess Name here", "ptr")
;Gui, Add, CheckBox, x+25 yp h21 gREFRESH_TIME vCB_RT, % "Refresh Time in ms:"
;Gui, Add, Edit, x+1 yp w60 h21 0x2002 vREFRESH_TIME, % "2000"
Gui, Add, Button, x+25 yp-1 w95 gSTART, % "Start"
Gui, Add, StatusBar
SB_SetParts(100, 100, 100, 100, 120)
Gui, Show, AutoSize, % "Process Explorer"
;REFRESH_TIME:
; Gui, Submit, NoHide
; GuiControl, % CB_RT = "1" ? "Disable" : "Enable", REFRESH_TIME
return
; SCRIPT ========================================================================================================================
START:
Gui, Submit, NoHide
WTS := ProcessExplorer.WTSEnumerateProcessesEx()
MEM := ProcessExplorer.GlobalMemoryStatusEx().MemoryLoad
PTH := ProcessExplorer.GetPerformanceInfo()
LV_Delete(), HC := TC := 0
loop % WTS.MaxIndex()
{
if (InStr(WTS[A_Index, "ProcessName"], prcsearch))
{
LV_Add("", WTS[A_Index, "ProcessName"]
, ProcessExplorer.GetProcessTimes(ProsessID := WTS[A_Index, "ProsessID"]) " %"
, ProcessExplorer.GetProcessMemoryInfo(ProsessID).PrivateUsage " K"
, WTS[A_Index, "WorkingSetSize"] " K"
, ProsessID
, WTS[A_Index, "UserName"] = "" ? "" : WTS[A_Index, "DomainName"] "/" WTS[A_Index, "UserName"]
, HC := WTS[A_Index, "HandleCount"] + HC
, TC := WTS[A_Index, "NumberOfThreads"] + TC
, WTS[A_Index, "PeakWorkingSetSize"] " K"
, WTS[A_Index, "PagefileUsage"] " K"
, WTS[A_Index, "PeakPagefileUsage"] " K"
, WTS[A_Index, "KernelTime"]
, WTS[A_Index, "UserTime"] )
tempA := OKT, tempB := OUT
}
}
SB_SetText("CPU Usage: " "%", 1)
SB_SetText("Processes: " PTH.ProcessCount, 2)
SB_SetText("Threads: " PTH.ThreadCount, 3)
SB_SetText("Handles: " PTH.HandleCount, 4)
SB_SetText("Physical Usage: " MEM "%", 5)
return
; CLASS =========================================================================================================================
Class ProcessExplorer
{
static ClassNew := ProcessExplorer.ClassInit()
static ClassDel := OnExit(ObjBindMethod(ProcessExplorer, "_Delete"))
; ===============================================================================================================================
ClassInit()
{
hADVAPI := DllCall("LoadLibrary", "str", "advapi32.dll", "uptr")
hNTDLL := DllCall("LoadLibrary", "str", "ntdll.dll", "uptr")
hPSAPI := DllCall("LoadLibrary", "str", "psapi.dll", "uptr")
hWTSAPI := DllCall("LoadLibrary", "str", "wtsapi32.dll", "uptr")
;WinVer := ProcessExplorer.RtlGetVersion()
}
; ===============================================================================================================================
WTSEnumerateProcessesEx() ; https://msdn.microsoft.com/en-us/library/ee621013(v=vs.85).aspx
{
local PI := PI_EX := TTL := 0 ; aktuell nur 64-Bit umgesetzt | size (56 | 64)
if !(DllCall("wtsapi32\WTSEnumerateProcessesEx", "ptr", 0, "uint*", 1, "uint", 0xFFFFFFFE, "ptr*", PI_EX, "uint*", TTL))
return (ErrorLevel := 1) & 0
PI := PI_EX, WTS := []
loop % TTL
{
WTS[A_Index, "SessionID"] := NumGet(PI+0, "uint")
WTS[A_Index, "ProsessID"] := NumGet(PI+4, "uint")
WTS[A_Index, "ProcessName"] := StrGet(NumGet(PI+8, "ptr"))
WTS[A_Index, "UserSID"] := SID := NumGet(PI+16, "ptr"), LAS := this.LookupAccountSid(SID)
WTS[A_Index, "UserName"] := LAS.Name
WTS[A_Index, "DomainName"] := LAS.Domain
WTS[A_Index, "NumberOfThreads"] := NumGet(PI+24, "uint")
WTS[A_Index, "HandleCount"] := NumGet(PI+28, "uint")
WTS[A_Index, "PagefileUsage"] := this.GetNumberFormatEx(Round(NumGet(PI+32, "uint") / 1024))
WTS[A_Index, "PeakPagefileUsage"] := this.GetNumberFormatEx(Round(NumGet(PI+36, "uint") / 1024))
WTS[A_Index, "WorkingSetSize"] := this.GetNumberFormatEx(Round(NumGet(PI+40, "uint") / 1024))
WTS[A_Index, "PeakWorkingSetSize"] := this.GetNumberFormatEx(Round(NumGet(PI+44, "uint") / 1024))
WTS[A_Index, "UserTime"] := this.GetDurationFormatEx(NumGet(PI+48, "int64"))
WTS[A_Index, "KernelTime"] := this.GetDurationFormatEx(NumGet(PI+56, "int64"))
PI += 64
}
return WTS, this.WTSFreeMemoryEx(PI_EX, TTL)
}
; ===============================================================================================================================
WTSFreeMemoryEx(buf, cnt) ; https://msdn.microsoft.com/en-us/library/ee621015(v=vs.85).aspx
{
if !(DllCall("wtsapi32\WTSFreeMemoryEx", "int", 1, "ptr", buf, "uint", cnt))
return (ErrorLevel := 1) & 0
return 1
}
; ===============================================================================================================================
LookupAccountSid(SID) ; https://msdn.microsoft.com/en-us/library/aa379166(v=vs.85).aspx
{
static SNU := "", LAS := {}
DllCall("advapi32\LookupAccountSid", "ptr", 0, "ptr", SID, "ptr", 0, "uint*", sn, "ptr", 0, "uint*", sd, "uint*", SNU)
VarSetCapacity(n, sn * (A_IsUnicode ? 2 : 1), 0), VarSetCapacity(d, sd * (A_IsUnicode ? 2 : 1), 0)
if !(DllCall("advapi32\LookupAccountSid", "ptr", 0, "ptr", SID, "str", n, "uint*", sn, "str", d, "uint*", sd, "uint*", SNU))
return (ErrorLevel := 1) & 0
LAS.Name := n, LAS.Domain := d
return LAS
}
; ===============================================================================================================================
GetDurationFormatEx(VarIn) ; https://msdn.microsoft.com/en-us/library/dd318092(v=vs.85).aspx
{
static format := "hh:mm:ss.fff", locale := "!x-sys-default-locale"
if !(size := DllCall("GetDurationFormatEx", "ptr", &locale, "uint", 0, "ptr", 0, "int64", VarIn, "wstr", format, "ptr", 0, "int", 0))
return (ErrorLevel := 1) & 0
VarSetCapacity(buf, size * (A_IsUnicode ? 2 : 1), 0)
if !(DllCall("GetDurationFormatEx", "ptr", &locale, "uint", 0, "ptr", 0, "int64", VarIn, "wstr", format, "wstr", buf, "int", size))
return (ErrorLevel := 2) & 0
return buf
}
; ===============================================================================================================================
GetNumberFormatEx(VarIn) ; https://msdn.microsoft.com/en-us/library/dd318113(v=vs.85).aspx
{
static locale := "!x-sys-default-locale"
if !(size := DllCall("GetNumberFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "ptr", 0, "int", 0))
return (ErrorLevel := 1) & 0
VarSetCapacity(buf, size * (A_IsUnicode ? 2 : 1), 0)
if !(DllCall("GetNumberFormatEx", "ptr", &locale, "uint", 0, "ptr", &VarIn, "ptr", 0, "str", buf, "int", size))
return (ErrorLevel := 2) & 0
return SubStr(buf, 1, - 3)
}
; ===============================================================================================================================
GetPerformanceInfo() ; https://msdn.microsoft.com/en-us/library/ms683210(v=vs.85).aspx
{
static PI, size := NumPut(VarSetCapacity(PI, (A_PtrSize = 4) ? 56 : 104, 0), PI, "uint") ; 56 | 104
if !(DllCall("GetPerformanceInfo", "ptr", &PI, "uint", size))
if !(DllCall("psapi\GetPerformanceInfo", "ptr", &PI, "uint", size))
return (ErrorLevel := 1) & 0
GPI := {}
GPI.CommitTotal := NumGet(PI, o := A_PtrSize, "uptr") ; 4 | 8
GPI.CommitLimit := NumGet(PI, o += A_PtrSize, "uptr") ; 8 | 16
GPI.CommitPeak := NumGet(PI, o += A_PtrSize, "uptr") ; 12 | 24
GPI.PhysicalTotal := NumGet(PI, o += A_PtrSize, "uptr") ; 16 | 32
GPI.PhysicalAvailable := NumGet(PI, o += A_PtrSize, "uptr") ; 20 | 40
GPI.SystemCache := NumGet(PI, o += A_PtrSize, "uptr") ; 24 | 48
GPI.KernelTotal := NumGet(PI, o += A_PtrSize, "uptr") ; 28 | 56
GPI.KernelPaged := NumGet(PI, o += A_PtrSize, "uptr") ; 32 | 64
GPI.KernelNonpaged := NumGet(PI, o += A_PtrSize, "uptr") ; 34 | 72
GPI.PageSize := NumGet(PI, o += A_PtrSize, "uptr") ; 40 | 80
GPI.HandleCount := NumGet(PI, o += A_PtrSize, "uint") ; 44 | 88
GPI.ProcessCount := NumGet(PI, o += 4 , "uint") ; 48 | 92
GPI.ThreadCount := NumGet(PI, o += 4 , "uint") ; 52 | 96
return GPI
}
; ===============================================================================================================================
GetProcessMemoryInfo(ProcessID) ; https://msdn.microsoft.com/en-us/library/ms683219(v=vs.85).aspx
{
static PMC_EX, size := NumPut(VarSetCapacity(PMC_EX, 8 + A_PtrSize * 9, 0), PMC_EX, "uint") ; 44 | 80
hProcess := this.OpenProcess(ProcessID, 0x1000) ; ==> Probleme mit 0x0400 & 0x0410
if !(DllCall("GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
if !(DllCall("psapi\GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
return (ErrorLevel := 1) & 0
GPMI := {}
GPMI.PageFaultCount := NumGet(PMC_EX, 4, "uint") ; 4 | 4
GPMI.PeakWorkingSetSize := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o := 8, "uptr") / 1024)) ; 8 | 8
GPMI.WorkingSetSize := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 12 | 16
GPMI.QuotaPeakPagedPoolUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 16 | 24
GPMI.QuotaPagedPoolUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 20 | 32
GPMI.QuotaPeakNonPagedPoolUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 24 | 40
GPMI.QuotaNonPagedPoolUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 28 | 48
GPMI.PagefileUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 32 | 56
GPMI.PeakPagefileUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 36 | 64
GPMI.PrivateUsage := this.GetNumberFormatEx(Round(NumGet(PMC_EX, o += A_PtrSize, "uptr") / 1024)) ; 40 | 72
return GPMI, this.CloseHandle(hProcess)
}
; ===============================================================================================================================
GetProcessTimes(ProcessID) ; https://msdn.microsoft.com/en-us/library/ms683223(v=vs.85).aspx
{
static OKT, OUT, NKT, NUT ; ==> Noch Probleme bei CPU Berechnung
hProcess := this.OpenProcess(ProcessID, 0x1000)
OKT := NKT, OUT := NUT
if !(DllCall("GetProcessTimes", "ptr", hProcess, "int64*", CT, "int64*", ET, "int64*", NKT, "int64*", NUT))
return (ErrorLevel := 1) & 0
return (NKT - OKT + NUT - OUT) * 1.e-5, this.CloseHandle(hProcess)
}
; ===============================================================================================================================
RtlGetVersion() ; https://msdn.microsoft.com/en-us/library/ff561910(v=vs.85).aspx
{
; 0x0A00 - Windows 10
; 0x0603 - Windows 8.1
; 0x0602 - Windows 8 / Windows Server 2012
; 0x0601 - Windows 7 / Windows Server 2008 R2
; 0x0600 - Windows Vista / Windows Server 2008
; 0x0502 - Windows XP 64-Bit Edition / Windows Server 2003 / Windows Server 2003 R2
; 0x0501 - Windows XP
static RTL_OSV_EX, init := NumPut(VarSetCapacity(RTL_OSV_EX, A_IsUnicode ? 284 : 156, 0), RTL_OSV_EX, "uint")
if (DllCall("ntdll\RtlGetVersion", "ptr", &RTL_OSV_EX) != 0)
return (ErrorLevel := 1) & 0
return ((NumGet(RTL_OSV_EX, 4, "uint") << 8) | NumGet(RTL_OSV_EX, 8, "uint"))
}
; ===============================================================================================================================
GlobalMemoryStatusEx() ; https://msdn.microsoft.com/en-us/library/aa366589(v=vs.85).aspx
{
static MS_EX, init := NumPut(VarSetCapacity(MS_EX, 64, 0), MS_EX, "uint")
if !(DllCall("GlobalMemoryStatusEx", "ptr", &MS_EX))
return (ErrorLevel := 1) & 0
GMSEX := {}
GMSEX.MemoryLoad := NumGet(MS_EX, 4, "uint")
GMSEX.TotalPhys := NumGet(MS_EX, 8, "uint64")
GMSEX.AvailPhys := NumGet(MS_EX, 16, "uint64")
GMSEX.TotalPageFile := NumGet(MS_EX, 24, "uint64")
GMSEX.AvailPageFile := NumGet(MS_EX, 32, "uint64")
GMSEX.TotalVirtual := NumGet(MS_EX, 40, "uint64")
GMSEX.AvailVirtual := NumGet(MS_EX, 48, "uint64")
GMSEX.AvailExtendedVirtual := NumGet(MS_EX, 56, "uint64")
return GMSEX
}
; ===============================================================================================================================
OpenProcess(ProcessID, Access := 0x400) ; https://msdn.microsoft.com/en-us/library/ms684320(v=vs.85).aspx
{
if !(hProcess := DllCall("OpenProcess", "uint", Access, "int", 0, "uint", ProcessID))
return (ErrorLevel := 1) & 0
return hProcess
}
; ===============================================================================================================================
CloseHandle(hObject) ; https://msdn.microsoft.com/en-us/library/ms724211(v=vs.85).aspx
{
if !(DllCall("CloseHandle", "ptr", hObject))
return (ErrorLevel := 1) & 0
return 1
}
; ===============================================================================================================================
_Delete()
{
DllCall("FreeLibrary", "ptr", ProcessExplorer.hWTSAPI)
DllCall("FreeLibrary", "ptr", ProcessExplorer.hPSAPI)
DllCall("FreeLibrary", "ptr", ProcessExplorer.hNTDLL)
DllCall("FreeLibrary", "ptr", ProcessExplorer.hADVAPI)
}
}
; EXIT ==========================================================================================================================
GuiEscape:
GuiClose:
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment