sum values in an array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

sum values in an array

23 Aug 2017, 16:45

Hi everyone,

I am using this script and would like a single addition:

Can you add a text field on the bottom that sums every "workingsetsize" in the array? This is too hard for me. You can use the linked script directly and this is the only change I would request.

Thank you!
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: sum values in an array

24 Aug 2017, 07:50

This could be it. Let me know, the program has no lines in the ListView on my pc so a value of 0KB is understandable. Hope it is a bit more dynamic on your system. I added the lines with the EditDT comment.

Code: Select all


SetBatchLines -1
ProcessPrivilege(DllCall("Kernel32.dll\GetCurrentProcessId"), "SeDebugPrivilege")

Gui, Add, ListView, x0 y0 w615 h320 Sort gMyListView, PID|Name|PrivateUsage|WorkingSetSize
GuiControl, -Redraw, SysListView321
IL_Destroy(ImageList)
, ImageList := IL_Create()
, LV_Delete()
, LV_SetImageList(ImageList)
for k, v in ProcessEnum()
{
  if instr(v,"chrome")
  {
	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")
		
		TotalSize += i.WorkingSetSize/1024	; EditDT.
	}
}

GUI, Add, Text, xm w500 vTotalSizeTxt, % TotalSize " KB"	; EditDT.

 LV_ModifyCol(1, "AutoHdr"), LV_ModifyCol(2, "AutoHdr")
GuiControl, +Redraw, SysListView321
Gui, Show, w618 h343, Process Memory Usage
SetTimer, Update, 2000
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo,1)  
    msgbox,260,,%rowtext% kill application?
    IfMsgBox, yes
    {
    Process, close,%rowtext%
    sleep 2000

    }
}
return 

GuiEscape:
GuiClose:
ExitApp
return

Update:
SetTimer,, Off
TotalSize := 0
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")
	
	TotalSize += i.WorkingSetSize/1024	; EditDT.
}
GUIControl, Text, TotalSizeTxt, % TotalSize " KB"	; EditDT.
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
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, Google [Bot], gsxr1300, HiSoKa and 276 guests