Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WINVER _WIN32_WINNT_WIN7
- #include <iostream>
- #include <list>
- #include <windows.h>
- #include <psapi.h>
- class Process
- {
- private:
- void* hndl = INVALID_HANDLE_VALUE;
- public:
- void* get() const noexcept
- { return hndl; }
- bool invalid() const noexcept
- { return get() == nullptr; }
- bool valid() const noexcept
- { return !invalid(); }
- // Operators
- HANDLE& operator=(HANDLE& h) noexcept
- {
- if (hndl != INVALID_HANDLE_VALUE) CloseHandle(hndl);
- hndl = h;
- return hndl;
- }
- operator void*() const noexcept
- { return get(); }
- explicit operator bool() const noexcept
- { return valid(); }
- // Constructors
- Process() = default;
- Process(HANDLE h) : hndl(h){};
- ~Process()
- { CloseHandle(hndl); }
- };
- bool PSAPI_EnumProcesses(
- std::list<unsigned int>& listProcessIDs,
- unsigned int dwMaxProcessCount)
- { // stolen from stackoverflow
- DWORD dwRet = NO_ERROR;
- listProcessIDs.clear();
- DWORD *pProcessIds = new DWORD[dwMaxProcessCount];
- DWORD cb = dwMaxProcessCount * sizeof(DWORD);
- DWORD dwBytesReturned = 0;
- if (::EnumProcesses(pProcessIds, cb, &dwBytesReturned))
- {
- const int nSize = dwBytesReturned / sizeof(DWORD);
- for(int nIndex = 0; nIndex < nSize; nIndex++)
- { listProcessIDs.push_back(pProcessIds[nIndex]); }
- } else dwRet = ::GetLastError();
- delete[]pProcessIds;
- return dwRet == NO_ERROR;
- }
- bool flush(unsigned int pid)
- {
- Process hndl(OpenProcess(
- PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA | PROCESS_VM_READ,
- false,
- pid));
- if (hndl)
- {
- if (EmptyWorkingSet(hndl))
- {
- char name[MAX_PATH];
- std::cout
- << " Success: "
- << (GetModuleFileNameEx(hndl,0,name,MAX_PATH) ? name : "???")
- << " (" << pid << ")"
- << std::endl;
- return true;
- } else std::cout << " FAILURE: EmptyWorkingSet() " << pid << std::endl;
- } else std::cout << " FAILURE: OpenProcess() " << pid << std::endl;
- return false;
- }
- void flush_all()
- {
- std::list<unsigned int> listProcessIDs;
- const unsigned int MaxProcessCount = 1024;
- if (PSAPI_EnumProcesses(listProcessIDs, MaxProcessCount))
- { for (const unsigned int& pid : listProcessIDs) flush(pid); }
- else std::cout << "PSAPI_EnumProcesses() failed." << std::endl;
- }
- int main()
- {
- using namespace std;
- cout
- << "Emptying the working sets of all open processes.\n"
- << "This may cause some weirdness...\n"
- << endl;
- flush_all();
- cout << "\n\n\nFlushing Complete." << endl;
- return 0;
- }
- /* ------------ Dylan's C code for "reference" (read: stealing)
- #define WINVER 0x0600
- #include <stdio.h>
- #include <windows.h>
- #include <psapi.h>
- #include <stdint.h>
- int main()
- {
- uint32_t pid;
- do
- {
- HANDLE pidHandle;
- fprintf(stderr, "Enter the PID to flush pages: ");
- scanf("%u", &pid);
- if ((pidHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA, 0, pid)) != NULL)
- {
- if (EmptyWorkingSet(pidHandle))
- fprintf(stderr, "Pages flushed successfully.\n");
- else
- fprintf(stderr, "Pages couldn't be flushed...access permissions?\n");
- }
- else
- fprintf(stderr, "nope.jpg...probably invalid pid.\n");
- CloseHandle(pidHandle);
- } while (pid != 0);
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement