AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

EmptyWorkingSet help

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
strictlyfocused02



Joined: 21 Jan 2009
Posts: 63

PostPosted: Sun Dec 20, 2009 11:38 am    Post subject: EmptyWorkingSet help Reply with quote

Im trying to write a script that will send all my active processes to the EmptyWorkingSet API. Im really struggling to figure out how to get the Var "ProcArray2" (var with the PID) sent to the DLL here.

Any help will be greatly appreciated

Code:
#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

ProcList := CMDret("tasklist /fo CSV")  ;Get a CSV tasklist using cmdret.dll
Loop, Parse, ProcList, `n
{
   If A_Index < 4  ;The first 4 lines are always junk
      Continue
   StringSplit, ProcArray, A_LoopField, `,  ;Split apart using a comma delimeter
   StringReplace, ProcArray1, ProcArray1, `",, 1  ; Remove quotations
   StringReplace, ProcArray2, ProcArray2, `",, 1
   DllCall("psapi.dll\EmptyWorkingSet", "Int", -1, "Int")  ;THIS IS WHERE I NEED HELP
}

;A Dll file designed for retrieving output from console based applications and saving the output in a variable
CMDret(CMD)  ;by corrupt (http://www.autohotkey.com/forum/topic3687.html)
{
   VarSetCapacity(StrOut, 10000)
   RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
   Return, %StrOut%
}
Back to top
View user's profile Send private message
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Sun Dec 20, 2009 12:20 pm    Post subject: Reply with quote

msdn wrote:
BOOL WINAPI EmptyWorkingSet(
__in HANDLE hProcess
);

So instead of -1, should be this?
Code:
   DllCall("psapi.dll\EmptyWorkingSet", "Int", ProcArray2, "Int")
Back to top
View user's profile Send private message Visit poster's website
strictlyfocused02



Joined: 21 Jan 2009
Posts: 63

PostPosted: Thu Dec 24, 2009 5:36 am    Post subject: Reply with quote

a_h_k wrote:
msdn wrote:
BOOL WINAPI EmptyWorkingSet(
__in HANDLE hProcess
);

So instead of -1, should be this?
Code:
   DllCall("psapi.dll\EmptyWorkingSet", "Int", ProcArray2, "Int")


Doesnt work Crying or Very sad
Back to top
View user's profile Send private message
strictlyfocused02



Joined: 21 Jan 2009
Posts: 63

PostPosted: Wed Dec 30, 2009 11:48 am    Post subject: Reply with quote

Just for reference purposes, here is what I ended up doing (with some help) to make it work.

Code:
;A function that retreives the process handle from a PID and sends it to the EmptyWorkingSet API.  Returns 1 for success, NULL (blank) on failure
CleanMemory(PID)  ;Written with help from "Temp01" on the AHK IRC chat (thank you again, temp01!!!)
{
   Process, Exist  ;Sets ErrorLevel to the PID of this running script
   h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ErrorLevel)  ;Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400)
   DllCall("Advapi32.dll\OpenProcessToken", "UInt", h, "UInt", 32, "UIntP", t)  ;Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32)
   VarSetCapacity(ti, 16, 0)  ;Structure of privileges
   NumPut(1, ti, 0)  ;One entry in the privileges array...
   DllCall("Advapi32.dll\LookupPrivilegeValueA", "UInt", 0, "Str", "SeDebugPrivilege", "Int64P", luid)  ;Retrieves the locally unique identifier of the debug privilege:
   NumPut(luid, ti, 4, "int64")
   NumPut(2, ti, 12)  ;Enable this privilege: SE_PRIVILEGE_ENABLED = 2
   DllCall("Advapi32.dll\AdjustTokenPrivileges", "UInt", t, "Int", false, "UInt", &ti, "UInt", 0, "UInt", 0, "UInt", 0)  ;Update the privileges of this process with the new access token:
   DllCall("CloseHandle", "UInt", h)  ;Close this process handle to save memory

   hModule := DllCall("LoadLibrary", "Str", "Psapi.dll")  ;Increase performance by preloading the libaray
   h := DllCall("OpenProcess", "UInt", 0x400|0x100, "Int", false, "UInt", pid)  ;Open process with: PROCESS_QUERY_INFORMATION (0x0400) | PROCESS_SET_QUOTA (0x100)
   e := DllCall("psapi.dll\EmptyWorkingSet", "UInt", h)
   DllCall("CloseHandle", "UInt", h)  ;Close process handle to save memory
   DllCall("FreeLibrary", "UInt", hModule)  ;Unload the library to free memory
   Return e
}
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group