RAMPro UDF


SUBMITTED BY: Guest

DATE: May 30, 2013, 8 a.m.

FORMAT: Text only

SIZE: 12.5 kB

HITS: 945

  1. #NoTrayIcon
  2. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  3. #AutoIt3Wrapper_icon=Sys-Program.ico
  4. #AutoIt3Wrapper_Res_Comment=Free empty working space
  5. #AutoIt3Wrapper_Res_Description=RAMPro
  6. #AutoIt3Wrapper_Res_Fileversion=2.4
  7. #AutoIt3Wrapper_Res_LegalCopyright=James Brooks
  8. #AutoIt3Wrapper_Res_Field=Website|http://www.james-brooks.net
  9. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  10. #include <GUIListView.au3>
  11. #include <GuiImageList.au3>
  12. #include <WindowsConstants.au3>
  13. #include <Constants.au3>
  14. Opt("TrayOnEventMode", 1)
  15. Opt("TrayMenuMode", 1)
  16. TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "OpenFromTray")
  17. AdlibRegister("_Auto")
  18. Global $StartIdle, $StartKernel, $StartUser ; CPU
  19. Global $EndIdle, $EndKernel, $EndUser ; CPU
  20. Dim $hMem = MemGetStats(), $bAuto = False
  21. Dim $hTimer = TimerInit() ; We use this to reset the warning box if we need
  22. Dim $iVersion = 2.4 ; Version number
  23. Dim $iCPUTot
  24. Dim $IDLETIME, $KERNELTIME, $USERTIME ; CPU
  25. $IDLETIME = DllStructCreate("dword;dword")
  26. $KERNELTIME = DllStructCreate("dword;dword")
  27. $USERTIME = DllStructCreate("dword;dword")
  28. $hGUI = GUICreate("RAMPro", 373, 165, -1, -1)
  29. $hInfo = _GUICtrlListView_Create($hGUI, "", 8, 8, 177, 149)
  30. _GUICtrlListView_SetExtendedListViewStyle($hInfo, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
  31. $hImage = _GUIImageList_Create() ; Create the solid bitmaps for the icons
  32. _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0xFF0000, 16, 16)) ; Drive Space is bad 0
  33. _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0xFF9900, 16, 16)) ; Drive space is ok 1
  34. _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hInfo, 0x00FF00, 16, 16)) ; Drive space is good 2
  35. _GUICtrlListView_InsertColumn($hInfo, 0, "Type")
  36. _GUICtrlListView_InsertColumn($hInfo, 1, "Info")
  37. _GUICtrlListView_AddItem($hInfo, "Total RAM", 3)
  38. _GUICtrlListView_AddSubItem($hInfo, 0, $hMem[1], 1)
  39. _GUICtrlListView_AddItem($hInfo, "Available RAM", _DecideStatus(1)) ; Decide what icon should be used
  40. _GUICtrlListView_AddSubItem($hInfo, 1, $hMem[2], 1)
  41. _GUICtrlListView_AddItem($hInfo, "Used RAM", _DecideStatus(2)) ; Decide what icon should be used
  42. _GUICtrlListView_AddSubItem($hInfo, 2, $hMem[3] & "%", 1)
  43. _GUICtrlListView_SetImageList($hInfo, $hImage, 1)
  44. _GUICtrlListView_AddItem($hInfo, "", 3) ; Blank between RAM and CPU
  45. _GUICtrlListView_AddItem($hInfo, "CPU Usage", _DecideStatus(3)) ; Decide what icon should be used
  46. _GUICtrlListView_AddSubItem($hInfo, 4, "", 1)
  47. _GUICtrlListView_SetImageList($hInfo, $hImage, 1)
  48. _GUICtrlListView_SetColumnWidth($hInfo, 0, $LVSCW_AUTOSIZE)
  49. _GUICtrlListView_SetColumnWidth($hInfo, 1, $LVSCW_AUTOSIZE)
  50. $hFree = GUICtrlCreateButton("&Free RAM", 192, 8, 177, 25, 0)
  51. $hSelect = GUICtrlCreateButton("Select &Process", 192, 40, 177, 25, 0)
  52. $hAuto = GUICtrlCreateButton("Enable &Auto Free", 192, 72, 177, 25, 0)
  53. $hStartup = GUICtrlCreateCheckbox("&Run RAMPro at startup", 192, 102, 177, 25)
  54. If FileExists(@StartupDir & "\RAMPro.lnk") Then GUICtrlSetState(-1, 1)
  55. $hAbout = GUICtrlCreateButton("About", 192, 128, 177, 25, 0)
  56. If @Compiled Then
  57. If $CmdLine[0] = 0 Then
  58. GUISetState(@SW_SHOW)
  59. ElseIf StringUpper($CmdLine[1]) == "-AUTEN" Then ; They turned on auto enable from startup!
  60. $bAuto = True
  61. GUICtrlSetData($hAuto, "Disable &Auto Free")
  62. GUISetState(@SW_HIDE)
  63. TraySetState(1)
  64. EndIf
  65. Else
  66. GUISetState(@SW_SHOW) ; When not running from EXE we just want the GUI
  67. EndIf
  68. While 1
  69. $nMsg = GUIGetMsg()
  70. Switch $nMsg
  71. Case -3
  72. Exit
  73. Case -4
  74. GUISetState(@SW_HIDE)
  75. TraySetState(1)
  76. Case $hFree
  77. _ReduceMemory_All()
  78. Case $hAbout
  79. MsgBox(0, "RAMPro", "RAMPro Version " & $iVersion & " by James Brooks " & @YEAR & @CRLF & @CRLF & "Visit http://www.james-brooks.net for more programs!")
  80. Case $hAuto
  81. If $bAuto = False Then
  82. $bAuto = True
  83. GUICtrlSetData($hAuto, "Disable &Auto Free")
  84. Else
  85. $bAuto = False
  86. GUICtrlSetData($hAuto, "Enable &Auto Free")
  87. EndIf
  88. Case $hSelect
  89. _FreeSelected()
  90. Case $hStartup
  91. $iState = GUICtrlRead($hStartup)
  92. If $iState = 1 Then
  93. _RunStartup(True)
  94. ElseIf $iState = 4 Then
  95. _RunStartup(False)
  96. EndIf
  97. EndSwitch
  98. WEnd
  99. Func _Auto()
  100. If $hMem[2] <= $hMem[1] / 4 Then _TrayTipWait("RAMPro", "Warning!" & @CRLF & "Low RAM available!", 40, 2)
  101. ; RAM
  102. $hMem = MemGetStats()
  103. _GUICtrlListView_SetItemText($hInfo, 1, $hMem[2], 1)
  104. _GUICtrlListView_SetItemImage($hInfo, 1, _DecideStatus(1))
  105. _GUICtrlListView_SetItemText($hInfo, 2, $hMem[0] & "%", 1)
  106. _GUICtrlListView_SetItemImage($hInfo, 2, _DecideStatus(2))
  107. ; CPU
  108. _GetSysTime($EndIdle, $EndKernel, $EndUser)
  109. _CPUCalc()
  110. _GetSysTime($StartIdle, $StartKernel, $StartUser)
  111. ; Should we auto reduce?
  112. If $bAuto = True Then _ReduceMemory_All()
  113. TraySetToolTip($hMem[0] & "% used!")
  114. EndFunc ;==>_Auto
  115. Func _ReduceMemory_All()
  116. $list = ProcessList()
  117. For $i = 1 To $list[0][0]
  118. Local $ai_Handle = DllCall("kernel32.dll", "int", "OpenProcess", "int", 0x1f0fff, "int", False, "int", $list[$i][1])
  119. If @error Then _CritErr("Unable to call OpenProcess in kernel32.dll")
  120. Local $ai_Return = DllCall("psapi.dll", "int", "EmptyWorkingSet", "int", $ai_Handle[0])
  121. If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
  122. DllCall("kernel32.dll", "int", "CloseHandle", "int", $ai_Handle[0])
  123. If @error Then _CritErr("Unable to call CloseHandle in kernel32.dll")
  124. Next
  125. EndFunc ;==>_ReduceMemory_All
  126. Func _ReduceMemory($i_PID)
  127. If $i_PID <> -1 Then
  128. Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
  129. If @error Then _CritErr("Unable to call OpenProcess in kernel32.dll")
  130. Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
  131. If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
  132. DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
  133. If @error Then _CritErr("Unable to call CloseHandle in kernel32.dll")
  134. Else
  135. Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
  136. If @error Then _CritErr("Unable to call EmptyWorkingSet in psapi.dll")
  137. EndIf
  138. Return $ai_Return[0]
  139. EndFunc ;==>_ReduceMemory
  140. Func OpenFromTray()
  141. TraySetState(2)
  142. GUISetState(@SW_SHOW)
  143. EndFunc ;==>OpenFromTray
  144. Func _TrayTipWait($s_TrayTitle, $s_TrayText, $i_TimeOut, $i_Option = 0)
  145. Local $i_PrevMatchMode = Opt("WinTitleMatchMode", 4)
  146. Local $i_StartTimer, $aWindows, $h_TrayTip, $b_Clicked = 0
  147. If $s_TrayText = "" Then
  148. TrayTip("", "", 30)
  149. Else
  150. $i_StartTimer = TimerInit()
  151. TrayTip($s_TrayTitle, $s_TrayText, 30, $i_Option)
  152. $aWindows = WinList("[CLASS:tooltips_class32]")
  153. For $iX = 1 To $aWindows[0][0]
  154. If BitAND(WinGetState($aWindows[$iX][1]), 2) Then
  155. $h_TrayTip = $aWindows[$iX][1]
  156. ExitLoop
  157. EndIf
  158. Next
  159. While BitAND(WinGetState($h_TrayTip), 2) And (TimerDiff($i_StartTimer) < (1000 * $i_TimeOut))
  160. Sleep(100)
  161. WEnd
  162. If TimerDiff($i_StartTimer) < (1000 * $i_TimeOut) Then
  163. $b_Clicked = 1
  164. Else
  165. TrayTip("", "", 10)
  166. $b_Clicked = 0
  167. EndIf
  168. EndIf
  169. Opt("WinTitleMatchMode", $i_PrevMatchMode)
  170. Return $b_Clicked
  171. EndFunc ;==>_TrayTipWait
  172. Func _FreeSelected()
  173. $hMyProcess = GUICreate("Free RAM from selected process", 354, 262, -1, -1)
  174. $hProcesses = _GUICtrlListView_Create($hMyProcess, "Processes|Type|Memory", 8, 8, 337, 214, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS))
  175. _GUICtrlListView_SetExtendedListViewStyle($hProcesses, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
  176. $hList = ProcessList()
  177. For $iList = 1 To $hList[0][0] ; Populate list with processes
  178. $iStats = ProcessGetStats($hList[$iList][1], 0)
  179. If IsArray($iStats) Then
  180. _GUICtrlListView_AddItem($hProcesses, $hList[$iList][0])
  181. _GUICtrlListView_AddSubItem($hProcesses, $iList - 1, $iStats[0] / 1024, 2) ; WorkingSetSize
  182. Else
  183. _GUICtrlListView_AddItem($hProcesses, $hList[$iList][0])
  184. _GUICtrlListView_AddSubItem($hProcesses, $iList - 1, "<SYSTEM>", 1)
  185. EndIf
  186. Next
  187. _GUICtrlListView_SetColumnWidth($hProcesses, 0, $LVSCW_AUTOSIZE) ; Re-size the process list
  188. $hKill = GUICtrlCreateButton("&Kill Process", 128, 224, 105, 33)
  189. $hFreeSelected = GUICtrlCreateButton("Free &Selected", 240, 224, 105, 33, 0)
  190. GUISetState(@SW_SHOW)
  191. While WinExists($hMyProcess)
  192. $iMsg = GUIGetMsg()
  193. Switch $iMsg
  194. Case -3;, $hClose
  195. GUIDelete($hMyProcess)
  196. Case $hFreeSelected
  197. $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
  198. For $f = 1 To $hIndic[0]
  199. $hRet = _ReduceMemory($hList[$hIndic[$f]][1]) ; Reduce the process memory
  200. Next
  201. Case $hKill
  202. $hIndic = _GUICtrlListView_GetSelectedIndices($hProcesses, True) ; Get the selected item
  203. For $k = 1 To $hIndic[0]
  204. ProcessClose($hList[$hIndic[$k]][1]) ; Close the process
  205. Next
  206. EndSwitch
  207. WEnd
  208. EndFunc ;==>_FreeSelected
  209. Func _DecideStatus($iType)
  210. Switch $iType
  211. Case 1 ; Available RAM
  212. If $hMem[2] <= $hMem[1] / 4 Then
  213. Return 0 ; Bad
  214. ElseIf $hMem[2] <= $hMem[1] / 2 Then
  215. Return 1 ; Ok
  216. Else
  217. Return 2 ; Good
  218. EndIf
  219. Case 2 ; Used RAM
  220. If $hMem[0] <= 25 Then
  221. Return 2 ; Bad
  222. ElseIf $hMem[0] <= 50 Then
  223. Return 1 ; Ok
  224. ElseIf $hMem[0] <= 75 Then
  225. Return 0 ; Good
  226. EndIf
  227. Case 3 ; CPU Usage
  228. If $iCPUTot <= 25 Then
  229. Return 2 ; Bad
  230. ElseIf $iCPUTot <= 50 Then
  231. Return 1 ; Ok
  232. Else
  233. Return 0 ; Good
  234. EndIf
  235. EndSwitch
  236. EndFunc ;==>_DecideStatus
  237. Func _CritErr($sText)
  238. $sErrFile = FileOpen(@ScriptDir & '\err.log', 1)
  239. FileWrite($sErrFile, $sText & " @ " & @MON & "/" & @MDAY & "/" & @YEAR)
  240. MsgBox(16 + 4096, "RAMPro Critical Error", $sText & @LF & @LF & "Please refer to err.log for further details.")
  241. FileClose($sErrFile)
  242. Exit 1
  243. EndFunc ;==>_CritErr
  244. Func _RunStartup($booShouldRun)
  245. If $booShouldRun = True Then
  246. FileCreateShortcut(@ScriptFullPath, @StartupDir & "\RAMPro.lnk", @StartupDir, "-auten")
  247. ElseIf $booShouldRun = False Then
  248. FileDelete(@StartupDir & "\RAMPro.lnk")
  249. EndIf
  250. EndFunc ;==>_RunStartup
  251. ;; CPU Functions by rasim (http://www.autoitscript.com/forum/index.php?showtopic=72689)
  252. Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
  253. DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), _
  254. "ptr", DllStructGetPtr($KERNELTIME), _
  255. "ptr", DllStructGetPtr($USERTIME))
  256. $sIdle = DllStructGetData($IDLETIME, 1)
  257. $sKernel = DllStructGetData($KERNELTIME, 1)
  258. $sUser = DllStructGetData($USERTIME, 1)
  259. EndFunc ;==>_GetSysTime
  260. Func _CPUCalc()
  261. Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser
  262. $iCalcIdle = ($EndIdle - $StartIdle)
  263. $iCalcKernel = ($EndKernel - $StartKernel)
  264. $iCalcUser = ($EndUser - $StartUser)
  265. $iSystemTime = ($iCalcKernel + $iCalcUser)
  266. $iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime)) & "%"
  267. $iCPUTot = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime))
  268. If _GUICtrlListView_GetItemText($hInfo, 4, 1) <> $iTotal Then
  269. _GUICtrlListView_SetItemText($hInfo, 4, $iTotal, 1)
  270. _GUICtrlListView_SetItemImage($hInfo, 4, _DecideStatus(3))
  271. EndIf
  272. EndFunc ;==>_CPUCalc
  273. ;; Visual Style Stuff
  274. Func ActivateWindowTheme($hWnd)
  275. $dll = DllCall("uxtheme.dll", "int", "SetWindowTheme", "hwnd", $hWnd, "wstr", "", "wstr", "")
  276. If Not @error Then Return $dll
  277. EndFunc ;==>ActivateWindowTheme

comments powered by Disqus