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
}