Sign in to follow this  
Followers 0
James

RAMPro

15 posts in this topic

#1 ·  Posted (edited)

Hey all,

So over the past months, I've been working on and off on a script; RAMPro. You can read about RAMPro on my blog, updates to the previously closed source can all be found here. RAMPro is used at my college, by the technicians themselves, and also deployed to every machine, as they are only supplied with 1GB of RAM. Running RAMPro has been efficient to the computers and especially to the poor users who have to use the college machines. Thankfully I have been granted permission to use my laptop and thus no more college machines for me!

Anyway, I thought it could be a good idea to release the code to the AutoIt community. It shows how the "EmptyWorkingSet" function can be used (thank you to whoever wrote the ReduceMemory function), using list views with icons, rasims CPUMon code, running code at startup (probably the worst method ever), minimizing to the tray, $CMDLine, AdlibRegister (only converted to 3.3.2.0 today) and overall, how bad of a coder I am Posted Image

Posted Image

The startup code (there are plenty of better ways I'm sure) works like it does, to cope with the tedious server issues they have at college. The most efficient way there is to create a shortcut to the actual location in the users profile area.

I've also added a window which allows you to select a single process and then either; Close or Free the process.

Anyway, I've only just added rasims CPUMon code today, so there might be a couple of hiccups, who knows...

To the code!

OK the script file is available for download as an attached file, so go ahead and get it! Also attached is the icon used by RAMPro.

Thanks,

James Posted Image

Edit: First hiccup - Forgot to update a variable, so that the image for the CPU item is updated... Fixed!

Edit two: Second hiccup - I left an _ArrayDisplay line in the code... Fixed!

Sys-Program.ico

RAMPro.au3

Edited by JamesBrooks

Share this post


Link to post
Share on other sites



very nice program

Share this post


Link to post
Share on other sites

Very nice utility. I am struggling to add in _GUICtrlListView_SortItems to the Select Process GUI. It would be nice to be able to sort those items.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Very nice utility. I am struggling to add in _GUICtrlListView_SortItems to the Select Process GUI. It would be nice to be able to sort those items.

I too seem to be having a problem implementing the code for this, however once I have figured it out, I shall release the update. You're very right, it would be nice to sort the items. Perhaps removing services too, since they are generally vital for Windows to run.

So after some testing, I seem to be able to use a WM_NOTIFY message to tell the list view it needs to sort, however, upon clicking it orders the items, but then promptly reverses the order. And upon further testing, it seemed to completely ignore callbacks afterwards.

Global $hProcesses ; Place this at the top of the script

Func _FreeSelected()
    $hMyProcess = GUICreate("Free RAM from selected process", 354, 262, -1, -1)
    $hProcesses = GUICtrlCreateListView("Processes|Type|Memory", 8, 8, 337, 214, BitOR($LVS_EDITLABELS, $LVS_REPORT))
    ;$hProcesses = _GUICtrlListView_Create($hMyProcess, "Processes|Type|Memory", 8, 8, 337, 214, BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($hProcesses, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER ))
    $hList = ProcessList()
    For $iList = 1 To $hList[0][0] ; Populate list with processes
        $iStats = ProcessGetStats($hList[$iList][1], 0)
        If IsArray($iStats) Then
            _GUICtrlListView_AddItem($hProcesses, $hList[$iList][0], -1, 1)
            _GUICtrlListView_AddSubItem($hProcesses, $iList - 1, $iStats[0] / 1024, 2) ; WorkingSetSize
        Else
            _GUICtrlListView_AddItem($hProcesses, $hList[$iList][0], -1, 2)
            _GUICtrlListView_AddSubItem($hProcesses, $iList - 1, "<SYSTEM>", 1)
        EndIf
    Next

    _GUICtrlListView_SetColumnWidth($hProcesses, 0, $LVSCW_AUTOSIZE_USEHEADER) ; Re-size the process list for "Processes"
    _GUICtrlListView_SetColumnWidth($hProcesses, 1, $LVSCW_AUTOSIZE_USEHEADER) ; Re-size the process list for "Type"
    _GUICtrlListView_SetColumnWidth($hProcesses, 2, $LVSCW_AUTOSIZE_USEHEADER) ; Re-size the process list for "Memory"

    $hKill = GUICtrlCreateButton("&Kill Process", 128, 224, 105, 33)
    $hFreeSelected = GUICtrlCreateButton("Free &Selected", 240, 224, 105, 33, 0)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)

    _GUICtrlListView_RegisterSortCallBack($hProcesses)

    While WinExists($hMyProcess)
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case -3
                GUIDelete($hMyProcess)
            Case $hFreeSelected
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $f = 1 To $hIndic[0]
                    $hRet = _ReduceMemory($hList[$hIndic[$f]][1]) ; Reduce the process memory
                Next
            Case $hKill
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $k = 1 To $hIndic[0]
                    ProcessClose($hList[$hIndic[$k]][1]) ; Close the process
                Next
            Case $hProcesses
                ; Kick off the sort callback
                _GUICtrlListView_SortItems($hProcesses, GUICtrlGetState($hProcesses))
        EndSwitch
    WEnd

    _GUICtrlListView_UnRegisterSortCallBack($hProcesses)
EndFunc   ;==>_FreeSelected

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hProcesses
    If Not IsHWnd($hProcesses) Then $hWndListView = GUICtrlGetHandle($hProcesses)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SortItems($hProcesses, DllStructGetData($tInfo, "SubItem"))
            EndSwitch
    EndSwitch

    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Try it, it confuses me Posted Image

Edited by JamesBrooks

Share this post


Link to post
Share on other sites

All that I can think of, as to why the _GUICtrlListView_SortItems() isn't working, is that another function must be messing up the functionality somewhere Posted Image

As to which function, where and how, I have no idea. Perhaps someone smarter than I can take a quick look? Posted Image

Share this post


Link to post
Share on other sites

Everytime someone missuses the EmptyWorkingSet function an angels gets raped. Posted Image


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Everytime someone missuses the EmptyWorkingSet function an angels gets raped. Posted Image

You should have seen this script before he edited it, he had EmptyWorkingSet in the mainloop so it ran ALL THE TIME! ;)

Share this post


Link to post
Share on other sites

Everytime someone missuses the EmptyWorkingSet function an angels gets raped. Posted Image

It's not that bad Posted Image

You should have seen this script before he edited it, he had EmptyWorkingSet in the mainloop so it ran ALL THE TIME! ;)

And it's definitely not that bad Posted Image

Share this post


Link to post
Share on other sites

I've updated RAMPro to V3.1.0.12 on my blog. No source code for the recent updates though.

New features:

  • Stress memory testing - does seem to make the CPU work its arse off, then cool down again :graduated:
  • Install as service - not 100% convinced this works
  • Command line arguments now work again
  • Titlebar right click menu
  • Exit by tray has been removed for now
  • Resize process window
  • Process window now displays the relative path
http://www.jbrooksuk.eu/projects/rampro/rampro-v3-1-0-5/

http://www.jbrooksuk.eu/projects/rampro/rampro-3-1-0-12-release/

James

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

error

Edited by mikell

Share this post


Link to post
Share on other sites

'No source code for the recent updates though' ... not nice.

I had a quick glance at your last version, the _GUICtrlListView_SortItems() problem is still not solved

You can skirt this by sorting the array instead of the listview

This allows you to sort by names, memory amount etc

Try this (include <array.au3> and declare some variables at the top of the script)

Func _FreeSelected()
    $hMyProcess = GUICreate("Free RAM from selected process", 354, 362, -1, -1)
    $hKill = GUICtrlCreateButton("&Kill Process", 128, 324, 105, 33)
    $hFreeSelected = GUICtrlCreateButton("Free &Selected", 240, 324, 105, 33, 0)

    $hProcesses = GUICtrlCreateListView("PID|Processes|Type|Memory", 8, 8, 337, 314, BitOR($LVS_EDITLABELS, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($hProcesses, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

    $hList = ProcessList()
    Dim $MemArray[$hList[0][0]][4]
    For $iList = 1 To $hList[0][0]
        $iStats = ProcessGetStats($hList[$iList][1], 0)
        $MemArray[$iList-1][0] = $hList[$iList][1]
        $MemArray[$iList-1][1] = $hList[$iList][0]
        If IsArray($iStats) Then
            $MemArray[$iList-1][3] = $iStats[0]/ 1024
        Else
            $MemArray[$iList-1][3] = "" 
            $MemArray[$iList-1][2] = "<SYSTEM>"
        EndIf
    Next
    $hList = $MemArray   ; <==== new $hList array with [PID][process][type][memory]

_GUICtrlListView_AddArray($hProcesses, $hList)

For $a = 0 to 3
_GUICtrlListView_SetColumnWidth($hProcesses, $a, $LVSCW_AUTOSIZE_USEHEADER)
Next

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)

    While WinExists($hMyProcess)
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case -3;, $hClose
                GUIDelete($hMyProcess)
            Case $hFreeSelected
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $f = 1 To $hIndic[0]
                    $hRet = _ReduceMemory($hList[$hIndic[$f]][0]) ; Reduce the process memory
                Next
            Case $hKill
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $k = 1 To $hIndic[0]
                    ProcessClose($hList[$hIndic[$k]][0]) ; Close the process
                Next
        EndSwitch
    WEnd

EndFunc   ;==>_FreeSelected


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hProcesses
    If Not IsHWnd($hProcesses) Then $hWndListView = GUICtrlGetHandle($hProcesses)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            $iCol = DllStructGetData($tInfo, "SubItem")   ; column clicked

            _GUICtrlListView_DeleteAllItems($hWndListView)
            If $iCol = 1 Then
                If $sortedDescP = 1 Then
                    _ArraySort($hList, 1, 1, $hList[0][0], $iCol)
                    $sortedDescP = 0
                Else
                    _ArraySort($hList, 0, 1, $hList[0][0], $iCol)
                    $sortedDescP = 1
                EndIf
            ElseIf $iCol = 3 Then
                If $sortedDescM = 1 Then
                    _ArraySort($hList, 1, 1, $hList[0][0], $iCol)
                    $sortedDescM = 0
                Else
                    _ArraySort($hList, 0, 1, $hList[0][0], $iCol)
                    $sortedDescM = 1
                EndIf
            EndIf
            _GUICtrlListView_AddArray($hProcesses, $hList)

            EndSwitch
    EndSwitch

    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Share this post


Link to post
Share on other sites

'No source code for the recent updates though' ... not nice.

I had a quick glance at your last version, the _GUICtrlListView_SortItems() problem is still not solved

Sorry. I used to be an open source kind of guy, maybe I'll release the source some time.

I know that some columns are not sortable, that's actually on purpose, but I guess they should be really.

Share this post


Link to post
Share on other sites

Didn't you try the piece of script ?

You only have to sort the array then update the Listview, so ALL columns become sortable

By the same way you can do your memory column refresh in real time (every 500 ms or so) etc

Heres the whole script :)

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Sys-Program.ico
#AutoIt3Wrapper_Res_Comment=Free empty working space
#AutoIt3Wrapper_Res_Description=RAMPro
#AutoIt3Wrapper_Res_Fileversion=2.4
#AutoIt3Wrapper_Res_LegalCopyright=James Brooks
://////=__=://.=-brooks.net
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <array.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)

TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "OpenFromTray")

AdlibRegister("_Auto")

Global $hList ; = ProcessList()
Global $StartIdle, $StartKernel, $StartUser ; CPU
Global $EndIdle, $EndKernel, $EndUser ; CPU
Global $hProcesses, $sortedDescP, $sortedDescM, $iCol
Dim $hMem = MemGetStats(), $bAuto = False
Dim $hTimer = TimerInit() ; We use this to reset the warning box if we need
Dim $iVersion = 2.4 ; Version number
Dim $iCPUTot
Dim $IDLETIME, $KERNELTIME, $USERTIME ; CPU

$IDLETIME = DllStructCreate("dword;dword")
$KERNELTIME = DllStructCreate("dword;dword")
$USERTIME = DllStructCreate("dword;dword")

$hGUI = GUICreate("RAMPro", 373, 165, -1, -1)
$hInfo = _GUICtrlListView_Create($hGUI, "", 8, 8, 177, 149)
_GUICtrlListView_SetExtendedListViewStyle($hInfo, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

$hImage = _GUIImageList_Create() ; Create the solid bitmaps for the icons
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0xFF0000, 16, 16)) ; Drive Space is bad   0
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0xFF9900, 16, 16)) ; Drive space is ok    1
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0x00FF00, 16, 16)) ; Drive space is good  2

_GUICtrlListView_InsertColumn($hInfo, 0, "Type")
_GUICtrlListView_InsertColumn($hInfo, 1, "Info")
_GUICtrlListView_AddItem($hInfo, "Total RAM", 3)
_GUICtrlListView_AddSubItem($hInfo, 0, $hMem[1], 1)
_GUICtrlListView_AddItem($hInfo, "Available RAM", _DecideStatus(1)) ; Decide what icon should be used
_GUICtrlListView_AddSubItem($hInfo, 1, $hMem[2], 1)
_GUICtrlListView_AddItem($hInfo, "Used RAM", _DecideStatus(2)) ; Decide what icon should be used
_GUICtrlListView_AddSubItem($hInfo, 2, $hMem[3] & "%", 1)
_GUICtrlListView_SetImageList($hInfo, $hImage, 1)

_GUICtrlListView_AddItem($hInfo, "", 3) ; Blank between RAM and CPU

_GUICtrlListView_AddItem($hInfo, "CPU Usage", _DecideStatus(3)) ; Decide what icon should be used
_GUICtrlListView_AddSubItem($hInfo, 4, "", 1)
_GUICtrlListView_SetImageList($hInfo, $hImage, 1)

_GUICtrlListView_SetColumnWidth($hInfo, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($hInfo, 1, $LVSCW_AUTOSIZE)

$hFree = GUICtrlCreateButton("&Free RAM", 192, 8, 177, 25, 0)
$hSelect = GUICtrlCreateButton("Select &Process", 192, 40, 177, 25, 0)
$hAuto = GUICtrlCreateButton("Enable &Auto Free", 192, 72, 177, 25, 0)

$hStartup = GUICtrlCreateCheckbox("&Run RAMPro at startup", 192, 102, 177, 25)
If FileExists(@StartupDir & "\RAMPro.lnk") Then GUICtrlSetState(-1, 1)

$hAbout = GUICtrlCreateButton("About", 192, 128, 177, 25, 0)

If @Compiled Then
    If $CmdLine[0] = 0 Then
        GUISetState(@SW_SHOW)
    ElseIf StringUpper($CmdLine[1]) == "-AUTEN" Then ; They turned on auto enable from startup!
        $bAuto = True
        GUICtrlSetData($hAuto, "Disable &Auto Free")
        GUISetState(@SW_HIDE)
        TraySetState(1)
    EndIf
Else
    GUISetState(@SW_SHOW) ; When not running from EXE we just want the GUI
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3
            Exit
        Case -4
            GUISetState(@SW_HIDE)
            TraySetState(1)
        Case $hFree
            _ReduceMemory_All()
        Case $hAbout
            MsgBox(0, "RAMPro", "RAMPro Version " & $iVersion & " by James Brooks " & @YEAR & @CRLF & @CRLF & "Visit http://www.james-brooks.net for more programs!")
        Case $hAuto
            If $bAuto = False Then
                $bAuto = True
                GUICtrlSetData($hAuto, "Disable &Auto Free")
            Else
                $bAuto = False
                GUICtrlSetData($hAuto, "Enable &Auto Free")
            EndIf
        Case $hSelect
            _FreeSelected()
        Case $hStartup
            $iState = GUICtrlRead($hStartup)
            If $iState = 1 Then
                _RunStartup(True)
            ElseIf $iState = 4 Then
                _RunStartup(False)
            EndIf
    EndSwitch
WEnd

Func _Auto()
    If $hMem[2] <= $hMem[1] / 4 Then _TrayTipWait("RAMPro", "Warning!" & @CRLF & "Low RAM available!", 40, 2)
    ; RAM
    $hMem = MemGetStats()
    _GUICtrlListView_SetItemText($hInfo, 1, $hMem[2], 1)
    _GUICtrlListView_SetItemImage($hInfo, 1, _DecideStatus(1))
    _GUICtrlListView_SetItemText($hInfo, 2, $hMem[0] & "%", 1)
    _GUICtrlListView_SetItemImage($hInfo, 2, _DecideStatus(2))
    ; CPU
    _GetSysTime($EndIdle, $EndKernel, $EndUser)
    _CPUCalc()
    _GetSysTime($StartIdle, $StartKernel, $StartUser)
    ; Should we auto reduce?
    If $bAuto = True Then _ReduceMemory_All()
    TraySetToolTip($hMem[0] & "% used!")
EndFunc   ;==>_Auto

Func _ReduceMemory_All()
    $list = ProcessList()
    For $i = 1 To $list[0][0]
        Local $ai_Handle = DllCall("kernel32.dll", "int", "OpenProcess", "int", 0x1f0fff, "int", False, "int", $list[$i][1])
        If @error Then _CritErr("Unable to call OpenProcess in kernel32.dll")
        Local $ai_Return = DllCall("psapi.dll", "int", "EmptyWorkingSet", "int", $ai_Handle[0])
        If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
        DllCall("kernel32.dll", "int", "CloseHandle", "int", $ai_Handle[0])
        If @error Then _CritErr("Unable to call CloseHandle in kernel32.dll")
    Next
EndFunc   ;==>_ReduceMemory_All

Func _ReduceMemory($i_PID)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        If @error Then _CritErr("Unable to call OpenProcess in kernel32.dll")
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
        If @error Then _CritErr("Unable to call CloseHandle in kernel32.dll")
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
        If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
    EndIf

    Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

Func OpenFromTray()
    TraySetState(2)
    GUISetState(@SW_SHOW)
EndFunc   ;==>OpenFromTray

Func _TrayTipWait($s_TrayTitle, $s_TrayText, $i_TimeOut, $i_Option = 0)
    Local $i_PrevMatchMode = Opt("WinTitleMatchMode", 4)
    Local $i_StartTimer, $aWindows, $h_TrayTip, $b_Clicked = 0

    If $s_TrayText = "" Then
        TrayTip("", "", 30)
    Else
        $i_StartTimer = TimerInit()
        TrayTip($s_TrayTitle, $s_TrayText, 30, $i_Option)

        $aWindows = WinList("[CLASS:tooltips_class32]")
        For $iX = 1 To $aWindows[0][0]
            If BitAND(WinGetState($aWindows[$iX][1]), 2) Then
                $h_TrayTip = $aWindows[$iX][1]
                ExitLoop
            EndIf
        Next

        While BitAND(WinGetState($h_TrayTip), 2) And (TimerDiff($i_StartTimer) < (1000 * $i_TimeOut))
            Sleep(100)
        WEnd
        If TimerDiff($i_StartTimer) < (1000 * $i_TimeOut) Then
            $b_Clicked = 1
        Else
            TrayTip("", "", 10)
            $b_Clicked = 0
        EndIf
    EndIf
    Opt("WinTitleMatchMode", $i_PrevMatchMode)
    Return $b_Clicked
EndFunc   ;==>_TrayTipWait


Func _FreeSelected()
    $hMyProcess = GUICreate("Free RAM from selected process", 354, 362, -1, -1)
    $hKill = GUICtrlCreateButton("&Kill Process", 128, 324, 105, 33)
    $hFreeSelected = GUICtrlCreateButton("Free &Selected", 240, 324, 105, 33, 0)
    ;$refresh = GUICtrlCreateButton("Refresh", 8, 324, 105, 33, 0)

    $hProcesses = GUICtrlCreateListView("PID|Processes|Type|Memory", 8, 8, 337, 314, BitOR($LVS_EDITLABELS, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($hProcesses, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

    $hList = ProcessList()
    Dim $MemArray[$hList[0][0]][4]
    For $iList = 1 To $hList[0][0]
        $iStats = ProcessGetStats($hList[$iList][1], 0)
        $MemArray[$iList-1][0] = $hList[$iList][1]
        $MemArray[$iList-1][1] = $hList[$iList][0]
        If IsArray($iStats) Then
            $MemArray[$iList-1][3] = $iStats[0]/ 1024
        Else
            $MemArray[$iList-1][3] = "" 
            $MemArray[$iList-1][2] = "<SYSTEM>"
        EndIf
    Next
    $hList = $MemArray   ; <==== new $hList array with [PID][process][type][memory]

_GUICtrlListView_AddArray($hProcesses, $hList)

For $a = 0 to 3
_GUICtrlListView_SetColumnWidth($hProcesses, $a, $LVSCW_AUTOSIZE_USEHEADER)
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
AdlibRegister("_Refresh", 500)

    While WinExists($hMyProcess)
        $iMsg = GUIGetMsg()
        Switch $iMsg
            Case -3;, $hClose
                AdlibUnRegister("_Refresh")
                GUIDelete($hMyProcess)
            Case $hFreeSelected
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $f = 1 To $hIndic[0]
                    $hRet = _ReduceMemory($hList[$hIndic[$f]][0]) ; Reduce the process memory
                Next
            Case $hKill
                $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
                For $k = 1 To $hIndic[0]
                    ProcessClose($hList[$hIndic[$k]][0]) ; Close the process
                Next
        EndSwitch
    WEnd
EndFunc   ;==>_FreeSelected


Func _Refresh()
For $m = 0 to UBound($hList)-1
$iStats = ProcessGetStats($hList[$m][0], 0)
    If IsArray($iStats) Then
_GUICtrlListView_SetItemText($hProcesses, $m, $iStats[0]/1024, 3)
    Else
_GUICtrlListView_SetItemText($hProcesses, $m, "", 3)
    EndIf
Next
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hProcesses
    If Not IsHWnd($hProcesses) Then $hWndListView = GUICtrlGetHandle($hProcesses)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            $iCol = DllStructGetData($tInfo, "SubItem")   ; column clicked

            _GUICtrlListView_DeleteAllItems($hWndListView)
            If $iCol = 1 Then
                If $sortedDescP = 1 Then
                    _ArraySort($hList, 1, 1, $hList[0][0], $iCol)
                    $sortedDescP = 0
                Else
                    _ArraySort($hList, 0, 1, $hList[0][0], $iCol)
                    $sortedDescP = 1
                EndIf
            ElseIf $iCol = 3 Then
                If $sortedDescM = 1 Then
                    _ArraySort($hList, 1, 1, $hList[0][0], $iCol)
                    $sortedDescM = 1
                Else
                    _ArraySort($hList, 0, 1, $hList[0][0], $iCol)
                    $sortedDescM = 0
                EndIf
            EndIf
            _GUICtrlListView_AddArray($hProcesses, $hList)

            EndSwitch
    EndSwitch

    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY


Func _DecideStatus($iType)
    Switch $iType
        Case 1 ; Available RAM
            If $hMem[2] <= $hMem[1] / 4 Then
                Return 0 ; Bad
            ElseIf $hMem[2] <= $hMem[1] / 2 Then
                Return 1 ; Ok
            Else
                Return 2 ; Good
            EndIf
        Case 2 ; Used RAM
            If $hMem[0] <= 25 Then
                Return 2 ; Bad
            ElseIf $hMem[0] <= 50 Then
                Return 1 ; Ok
            ElseIf $hMem[0] <= 75 Then
                Return 0 ; Good
            EndIf
        Case 3 ; CPU Usage
            If $iCPUTot <= 25 Then
                Return 2 ; Bad
            ElseIf $iCPUTot <= 50 Then
                Return 1 ; Ok
            Else
                Return 0 ; Good
            EndIf
    EndSwitch
EndFunc   ;==>_DecideStatus

Func _CritErr($sText)
    $sErrFile = FileOpen(@ScriptDir & '\err.log', 1)
    FileWrite($sErrFile, $sText & " @ " & @MON & "/" & @MDAY & "/" & @YEAR)
    MsgBox(16 + 4096, "RAMPro Critical Error", $sText & @LF & @LF & "Please refer to err.log for further details.")
    FileClose($sErrFile)
    Exit 1
EndFunc   ;==>_CritErr

Func _RunStartup($booShouldRun)
    If $booShouldRun = True Then
        FileCreateShortcut(@ScriptFullPath, @StartupDir & "\RAMPro.lnk", @StartupDir, "-auten")
    ElseIf $booShouldRun = False Then
        FileDelete(@StartupDir & "\RAMPro.lnk")
    EndIf
EndFunc   ;==>_RunStartup

;; CPU Functions by rasim (http://www.autoitscript.com/forum/index.php?showtopic=72689)
Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
    DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), _
            "ptr", DllStructGetPtr($KERNELTIME), _
            "ptr", DllStructGetPtr($USERTIME))

    $sIdle = DllStructGetData($IDLETIME, 1)
    $sKernel = DllStructGetData($KERNELTIME, 1)
    $sUser = DllStructGetData($USERTIME, 1)
EndFunc   ;==>_GetSysTime

Func _CPUCalc()
    Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser

    $iCalcIdle = ($EndIdle - $StartIdle)
    $iCalcKernel = ($EndKernel - $StartKernel)
    $iCalcUser = ($EndUser - $StartUser)

    $iSystemTime = ($iCalcKernel + $iCalcUser)
    $iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime)) & "%"

    $iCPUTot = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime))

    If _GUICtrlListView_GetItemText($hInfo, 4, 1) <> $iTotal Then
        _GUICtrlListView_SetItemText($hInfo, 4, $iTotal, 1)
        _GUICtrlListView_SetItemImage($hInfo, 4, _DecideStatus(3))
    EndIf
EndFunc   ;==>_CPUCalc

;; Visual Style Stuff
Func ActivateWindowTheme($hWnd)
    $dll = DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hWnd, "wstr", "", "wstr", "")
    If Not @error Then Return $dll
EndFunc   ;==>ActivateWindowTheme

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0