AutoHotkey Community

It is currently May 27th, 2012, 12:56 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: EmptyWorkingSet help
PostPosted: December 20th, 2009, 12:38 pm 
Offline

Joined: January 21st, 2009, 1:06 pm
Posts: 63
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%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 20th, 2009, 1:20 pm 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
msdn wrote:
BOOL WINAPI EmptyWorkingSet(
__in HANDLE hProcess
);

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 24th, 2009, 6:36 am 
Offline

Joined: January 21st, 2009, 1:06 pm
Posts: 63
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 :cry:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2009, 12:48 pm 
Offline

Joined: January 21st, 2009, 1:06 pm
Posts: 63
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
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher, WillTroll, XstatyK and 30 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group