Advertisement
Guest User

Memory Pager

a guest
Aug 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #define WINVER _WIN32_WINNT_WIN7
  2. #include <iostream>
  3. #include <list>
  4. #include <windows.h>
  5. #include <psapi.h>
  6.  
  7.  
  8.  
  9.  
  10. class Process
  11. {
  12. private:
  13. void* hndl = INVALID_HANDLE_VALUE;
  14.  
  15. public:
  16. void* get() const noexcept
  17. { return hndl; }
  18.  
  19. bool invalid() const noexcept
  20. { return get() == nullptr; }
  21.  
  22. bool valid() const noexcept
  23. { return !invalid(); }
  24.  
  25. // Operators
  26. HANDLE& operator=(HANDLE& h) noexcept
  27. {
  28. if (hndl != INVALID_HANDLE_VALUE) CloseHandle(hndl);
  29. hndl = h;
  30. return hndl;
  31. }
  32.  
  33. operator void*() const noexcept
  34. { return get(); }
  35.  
  36. explicit operator bool() const noexcept
  37. { return valid(); }
  38.  
  39. // Constructors
  40. Process() = default;
  41. Process(HANDLE h) : hndl(h){};
  42.  
  43. ~Process()
  44. { CloseHandle(hndl); }
  45. };
  46.  
  47.  
  48. bool PSAPI_EnumProcesses(
  49. std::list<unsigned int>& listProcessIDs,
  50. unsigned int dwMaxProcessCount)
  51. { // stolen from stackoverflow
  52. DWORD dwRet = NO_ERROR;
  53. listProcessIDs.clear();
  54. DWORD *pProcessIds = new DWORD[dwMaxProcessCount];
  55. DWORD cb = dwMaxProcessCount * sizeof(DWORD);
  56. DWORD dwBytesReturned = 0;
  57.  
  58. if (::EnumProcesses(pProcessIds, cb, &dwBytesReturned))
  59. {
  60. const int nSize = dwBytesReturned / sizeof(DWORD);
  61. for(int nIndex = 0; nIndex < nSize; nIndex++)
  62. { listProcessIDs.push_back(pProcessIds[nIndex]); }
  63. } else dwRet = ::GetLastError();
  64. delete[]pProcessIds;
  65.  
  66. return dwRet == NO_ERROR;
  67. }
  68.  
  69.  
  70. bool flush(unsigned int pid)
  71. {
  72. Process hndl(OpenProcess(
  73. PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA | PROCESS_VM_READ,
  74. false,
  75. pid));
  76.  
  77. if (hndl)
  78. {
  79. if (EmptyWorkingSet(hndl))
  80. {
  81. char name[MAX_PATH];
  82. std::cout
  83. << " Success: "
  84. << (GetModuleFileNameEx(hndl,0,name,MAX_PATH) ? name : "???")
  85. << " (" << pid << ")"
  86. << std::endl;
  87. return true;
  88. } else std::cout << " FAILURE: EmptyWorkingSet() " << pid << std::endl;
  89. } else std::cout << " FAILURE: OpenProcess() " << pid << std::endl;
  90. return false;
  91. }
  92.  
  93.  
  94. void flush_all()
  95. {
  96. std::list<unsigned int> listProcessIDs;
  97. const unsigned int MaxProcessCount = 1024;
  98.  
  99. if (PSAPI_EnumProcesses(listProcessIDs, MaxProcessCount))
  100. { for (const unsigned int& pid : listProcessIDs) flush(pid); }
  101. else std::cout << "PSAPI_EnumProcesses() failed." << std::endl;
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107. using namespace std;
  108.  
  109. cout
  110. << "Emptying the working sets of all open processes.\n"
  111. << "This may cause some weirdness...\n"
  112. << endl;
  113.  
  114. flush_all();
  115.  
  116. cout << "\n\n\nFlushing Complete." << endl;
  117.  
  118. return 0;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. /* ------------ Dylan's C code for "reference" (read: stealing)
  125. #define WINVER 0x0600
  126. #include <stdio.h>
  127. #include <windows.h>
  128. #include <psapi.h>
  129. #include <stdint.h>
  130.  
  131. int main()
  132. {
  133. uint32_t pid;
  134.  
  135. do
  136. {
  137. HANDLE pidHandle;
  138.  
  139. fprintf(stderr, "Enter the PID to flush pages: ");
  140. scanf("%u", &pid);
  141.  
  142. if ((pidHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA, 0, pid)) != NULL)
  143. {
  144. if (EmptyWorkingSet(pidHandle))
  145. fprintf(stderr, "Pages flushed successfully.\n");
  146. else
  147. fprintf(stderr, "Pages couldn't be flushed...access permissions?\n");
  148. }
  149. else
  150. fprintf(stderr, "nope.jpg...probably invalid pid.\n");
  151.  
  152. CloseHandle(pidHandle);
  153. } while (pid != 0);
  154.  
  155. return 0;
  156.  
  157. }
  158. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement