veblush

FlushFileCache

Oct 12th, 2012
1,578
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <stdio.h>
  3.  
  4. // Project Build Configuration:
  5. // - Linker / Manifest File / UAC Execution Level : requireAdministrator
  6.  
  7. //------------------------------------------------------------------------ NTDLL
  8.  
  9. typedef enum _SYSTEM_INFORMATION_CLASS
  10. {
  11.     SystemFileCacheInformation = 21,
  12.     SystemMemoryListInformation = 80
  13. };
  14.  
  15. typedef struct _SYSTEM_FILECACHE_INFORMATION
  16. {
  17.     SIZE_T CurrentSize;
  18.     SIZE_T PeakSize;
  19.     ULONG PageFaultCount;
  20.     SIZE_T MinimumWorkingSet;
  21.     SIZE_T MaximumWorkingSet;
  22.     SIZE_T CurrentSizeIncludingTransitionInPages;
  23.     SIZE_T PeakSizeIncludingTransitionInPages;
  24.     ULONG TransitionRePurposeCount;
  25.     ULONG Flags;
  26. } SYSTEM_FILECACHE_INFORMATION, *PSYSTEM_FILECACHE_INFORMATION;
  27.  
  28. typedef enum _SYSTEM_MEMORY_LIST_COMMAND
  29. {
  30.     MemoryCaptureAccessedBits,
  31.     MemoryCaptureAndResetAccessedBits,
  32.     MemoryEmptyWorkingSets,
  33.     MemoryFlushModifiedList,
  34.     MemoryPurgeStandbyList,
  35.     MemoryPurgeLowPriorityStandbyList,
  36.     MemoryCommandMax
  37. } SYSTEM_MEMORY_LIST_COMMAND;
  38.  
  39. NTSTATUS (NTAPI *NtSetSystemInformation)(
  40.     __in _SYSTEM_INFORMATION_CLASS SystemInformationClass,
  41.     __in_bcount_opt(SystemInformationLength) PVOID SystemInformation,
  42.     __in ULONG SystemInformationLength);
  43.  
  44. //-------------------------------------------------------------------- Privilege
  45.  
  46. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
  47. {
  48.     TOKEN_PRIVILEGES tp;
  49.     LUID luid;
  50.  
  51.     if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
  52.         return FALSE;
  53.  
  54.     tp.PrivilegeCount = 1;
  55.     tp.Privileges[0].Luid = luid;
  56.     tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0;
  57.  
  58.     if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
  59.         return FALSE;
  60.  
  61.     if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
  62.         return FALSE;
  63.  
  64.     return TRUE;
  65. }
  66.  
  67. //------------------------------------------------------------------------- Main
  68.  
  69. int wmain(int argc, const wchar_t* argv[])
  70. {
  71.     // Get NtSetSystemInformation
  72.  
  73.     HMODULE ntdll = LoadLibrary(L"NTDLL.DLL");
  74.     if (ntdll == NULL)
  75.         return 1;
  76.     *(void**)(&NtSetSystemInformation) = GetProcAddress(ntdll, "NtSetSystemInformation");
  77.     if (NtSetSystemInformation == NULL)
  78.         return 2;
  79.    
  80.     // Get Process Token
  81.  
  82.     HANDLE processToken;
  83.     if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &processToken) == FALSE)
  84.         return 3;
  85.  
  86.     // Clear FileCache WorkingSet
  87.  
  88.     if (SetPrivilege(processToken, L"SeIncreaseQuotaPrivilege", TRUE))
  89.     {
  90.         SYSTEM_FILECACHE_INFORMATION info;
  91.         ZeroMemory(&info, sizeof(info));
  92.         info.MinimumWorkingSet = -1;
  93.         info.MaximumWorkingSet = -1;
  94.         NTSTATUS ret = NtSetSystemInformation(SystemFileCacheInformation, &info, sizeof(info));
  95.         if (ret >= 0)
  96.             printf("Flush FileCache WorkingSet : ok\n");
  97.     }      
  98.  
  99.     // Purge Memory Standby
  100.  
  101.     if (SetPrivilege(processToken, L"SeProfileSingleProcessPrivilege", TRUE))
  102.     {
  103.         SYSTEM_MEMORY_LIST_COMMAND command = MemoryPurgeStandbyList;
  104.         NTSTATUS ret = NtSetSystemInformation(SystemMemoryListInformation, &command, sizeof(command));
  105.         if (ret >= 0)
  106.             printf("Purge Memory Standby : ok\n");
  107.     }
  108.  
  109.     return 0;
  110. }
RAW Paste Data