Advertisement
DoubleV

C++ | MMC example

Jul 31st, 2016
1,992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CMemoryManager
  2. {
  3. private: // Private members: We are going to setup some getters later on!
  4.     HANDLE m_hProcess; // The HANDLE to the process to attach
  5.     DWORD m_dwProcessId; // The Process Id of the process to attach
  6.     std::vector<MODULEENTRY32> m_Modules; // std::vector containing all the modules we grab from the process
  7. public:
  8.     // Attach to a process based on strProcessName
  9.     // Returns true on success, false on failure
  10.     bool Attach(const std::string& strProcessName)
  11.     {
  12.         // First of all we create a snapshot handle specific for processes
  13.         // (notice the usage of TH32CS_SNAPPROCESS) so we are able to call Process32First/Next
  14.         // Remeber to close it when you don't use it anymore!
  15.         HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  16.         // Check if the snapshot created is valid
  17.         if (hSnapshot == INVALID_HANDLE_VALUE) return false;
  18.        
  19.         // Create the helper struct that will contain all the infos about the current process
  20.         // while we loop through all the running processes
  21.         PROCESSENTRY32 ProcEntry;
  22.         // Remember to set the dwSize member of ProcEntry to sizeof(PROCESSENTRY32)
  23.         ProcEntry.dwSize = sizeof(PROCESSENTRY32);
  24.        
  25.         // Call Process32First
  26.         if (Process32First(hSnapshot, &ProcEntry))
  27.         {
  28.             // Notice that you have to enable Multi-Byte character set in order
  29.             // to avoid converting everything.
  30.             // strcmp is not the only way to compare 2 strings ofc, work with your imagination
  31.             if (!strcmp(ProcEntry.szExeFile, ProcessName.c_str()))
  32.             {
  33.                 // If we are here it means that the process has been found, we can
  34.                 // open an handle to it and return it
  35.                 // But first of all we have to close the snapshot handle!
  36.                 CloseHandle(hSnapshot);
  37.                 // Open an handle and set the m_hProcess member using OpenProcess
  38.                 // (Notice the usage of PROCESS_ALL_ACCESS flag in order to grant read/write privileges)
  39.                 m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
  40.                 // Store the process id into m_dwProcessId
  41.                 m_dwProcessId = ProcEntry.th32ProcessID);
  42.                 // Return true meaning success
  43.                 return true;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             // If the Process32First call failed, it means that there is no
  49.             // process running in the first place, we can return directly.
  50.             CloseHandle(hSnapshot);
  51.             return false;
  52.         }
  53.        
  54.         // If we are here it means that the Process32First call returned TRUE, but the first process
  55.         // wasn't the process that we were searching for. Now we can loop through the processes
  56.         // using Process32Next
  57.         while (Process32Next(hSnapshot, &ProcEntry))
  58.         {
  59.             // We do the same check we did for Process32First
  60.             if (!strcmp(ProcEntry.szExeFile, ProcessName.c_str()))
  61.             {
  62.                 // If we are here it means that the process has been found, we can
  63.                 // open an handle to it and set the m_hProcess member using OpenProcess
  64.                 // (Notice the usage of PROCESS_ALL_ACCESS flag in order to grant read/write privileges)
  65.                 m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
  66.                 // Store the process id into m_dwProcessId
  67.                 m_dwProcessId = ProcEntry.th32ProcessID);
  68.                 // Return true meaning success
  69.                 return true;
  70.             }
  71.         }
  72.         // Continue loop while the Process32Next call returns TRUE meaning that there are still processes to check
  73.        
  74.         // If we are here it means that the process has not been found or that there are no processes to scan for anymore.
  75.         // We can close the snapshot handle and return false.
  76.         CloseHandle(hSnapshot);
  77.         return false;
  78.     }
  79.    
  80.     // Grabs a module and adds it to m_Modules if found based on strModuleName
  81.     // Returns true on success, false on failure
  82.     bool GrabModule(const std::string& strModuleName)
  83.     {
  84.         // First of all we create a snapshot handle specific for modules
  85.         // (notice the usage of TH32CS_SNAPMODULE) so we are able to call Module32First/Next
  86.         // Remeber to close it when you don't use it anymore!
  87.         HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId);
  88.         // Check if the snapshot created is valid
  89.         if (hSnapshot == INVALID_HANDLE_VALUE) return false;
  90.        
  91.         // Create the helper struct that will contain all the infos about the current module
  92.         // while we loop through all the loaded modules
  93.         MODULEENTRY32 ModEntry;
  94.         // Remember to set the dwSize member of ModEntry to sizeof(MODULEENTRY32)
  95.         ModEntry.dwSize = sizeof(MODULEENTRY32);
  96.        
  97.         // Call Module32First
  98.         if (Module32First(hSnapshot, &ModEntry))
  99.         {
  100.             // Notice that you have to enable Multi-Byte character set in order
  101.             // to avoid converting everything.
  102.             // strcmp is not the only way to compare 2 strings ofc, work with your imagination
  103.             if (!strcmp(ModEntry.szModule, ModuleName.c_str()))
  104.             {
  105.                 // If we are here it means that the module has been found, we can add the module to the vector
  106.                 // But first of all we have to close the snapshot handle!
  107.                 CloseHandle(hSnapshot);
  108.                 // Add ModEntry to the m_Modules vector
  109.                 m_Modules.push_back(ModEntry); // You can add a check here to see if the module is already there ;)
  110.                 // Return true meaning success
  111.                 return true;
  112.             }
  113.         }
  114.         else
  115.         {
  116.             // If the Process32First call failed, it means that there is no
  117.             // process running in the first place, we can return directly.
  118.             CloseHandle(hSnapshot);
  119.             return false;
  120.         }
  121.        
  122.         // If we are here it means that the Module32First call returned TRUE, but the first module
  123.         // wasn't the module that we were searching for. Now we can loop through the modules
  124.         // using Module32Next
  125.         while (Module32Next(hSnapshot, &ModEntry))
  126.         {
  127.             // We do the same check we did for Module32First
  128.             if (!strcmp(ModEntry.szModule, ModuleName.c_str()))
  129.             {
  130.                 // If we are here it means that the module has been found, we can add the module to the vector
  131.                 // But first of all we have to close the snapshot handle!
  132.                 CloseHandle(hSnapshot);
  133.                 // Add ModEntry to the m_Modules vector
  134.                 m_Modules.push_back(ModEntry); // You can add a check here to see if the module is already there ;)
  135.                 // Return true meaning success
  136.                 return true;
  137.             }
  138.         }
  139.         // Continue loop while the Module32Next call returns TRUE meaning that there are still modules to check
  140.        
  141.         // If we are here it means that the module has not been found or that there are no modules to scan for anymore.
  142.         // We can close the snapshot handle and return false.
  143.         CloseHandle(hSnapshot);
  144.         return false;
  145.     }
  146.    
  147.     // Default constructor: won't attach to any process
  148.     CMemoryManager()
  149.     {
  150.         // Init members
  151.         m_hProcess = INVALID_HANDLE_VALUE;
  152.         m_dwProcessId = 0;
  153.         // Just for safety, I clear out the modules vector
  154.         m_Modules.clear();
  155.     }
  156.     // This constructor will attach to a specific process (default CS:GO)
  157.     CMemoryManager(const std::string& strProcessName = "csgo.exe")
  158.     {
  159.         // Init members
  160.         m_hProcess = INVALID_HANDLE_VALUE;
  161.         m_dwProcessId = 0;
  162.         // Just for safety, I clear out the modules vector
  163.         m_Modules.clear();
  164.         // Attach and throw if the function failed so we can manage the fail
  165.         if (!Attach(strProcessName))
  166.             throw;
  167.     }
  168.    
  169.     // RPM/WPM wrappers
  170.    
  171.     // Read a value from memory and put it in Value
  172.     // Returns true on success, false on failure
  173.     template <class T>
  174.     inline bool Read(DWORD dwAddress, T& Value)
  175.     {
  176.         return ReadProcessMemory(m_hProcess, reinterpret_cast<LPVOID>(dwAddress), Value, sizeof(T), NULL) == TRUE;
  177.     }
  178.     // Write a Value in memory
  179.     // Returns true on success, false on failure
  180.     template <class T>
  181.     inline bool Write(DWORD dwAddress, const T& Value)
  182.     {
  183.         return WriteProcessMemory(m_hProcess, reinterpret_cast<LPVOID>(dwAddress), Value, sizeof(T), NULL) == TRUE;
  184.     }
  185.    
  186.     // Getters
  187.     HANDLE GetHandle() { return m_hProcess; }
  188.     DWORD GetProcId() { return m_dwProcessId; }
  189.     std::vector<MODULEENTRY32> GetModules() { return m_Modules; }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement