#define WINVER 0x0500 #define WIN32_LEAN_AND_MEAN #include #include #pragma comment(lib, "advapi32.lib") // These are internal. #include #include extern "C" { typedef NTSTATUS (WINAPI *PNtSetSystemInformation)(IN SYSTEM_INFORMATION_CLASS, OUT PVOID, IN ULONG); #define SystemCacheInformation 0x15 typedef struct _SYSTEM_CACHE_INFORMATION { unsigned long CurrentSize; unsigned long PeakSize; unsigned long PageFaultCount; unsigned long MinimumWorkingSet; unsigned long MaximumWorkingSet; unsigned long Unused1; unsigned long Unused2; unsigned long Unused3; unsigned long Unused4; } SYSTEM_CACHE_INFORMATION, *PSYSTEM_CACHE_INFORMATION; } // End of internal. int main(int argc, const char* argv[]) { // Open the process' token so we can enable the privilege needed. HANDLE hToken; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { unsigned int err = GetLastError(); printf("OpenProcessToken error: %u\n", err); return err; } // Map the privilege name to it's logon-local ID. LUID luid; if (!LookupPrivilegeValue(NULL, SE_INCREASE_QUOTA_NAME, &luid)) { unsigned int err = GetLastError(); printf("LookupPrivilegeValue error: %u\n", err); return err; } // Try to enable the privilege. If we're not in Administrators, this will // almost certainly fail. TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) { unsigned int err = GetLastError(); printf("AdjustTokenPrivileges error: %u\n", err); return err; } if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { printf("The token does not have the specified privilege.\n"); return GetLastError(); } // Get the internal API we're going to use and set the working set values // to the special values that tell it to clear the file system cache. PNtSetSystemInformation NtSetSystemInformation; NtSetSystemInformation = (PNtSetSystemInformation)GetProcAddress(LoadLibrary("ntdll.dll"), "NtSetSystemInformation"); SYSTEM_CACHE_INFORMATION info; info.MinimumWorkingSet = 0xFFFFFFFF; info.MaximumWorkingSet = 0xFFFFFFFF; unsigned int rv = NtSetSystemInformation((SYSTEM_INFORMATION_CLASS)SystemCacheInformation, &info, sizeof(info)); if (rv != 0) { printf("NtSetSystemInformation error: %u\n", rv); return rv; } return 0; }