SHARE
TWEET

C++ | OpenHandle function example

DoubleV Jul 29th, 2016 (edited) 1,102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example of algorithm to open an handle to a specific process based on his name.
  2. // Returns the HANDLE to the process. (INVALID_HANDLE_VALUE if not found)
  3. HANDLE OpenHandle(const std::string& ProcessName)
  4. {
  5.     // First of all we create a snapshot handle specific for processes
  6.     // (notice the usage of TH32CS_SNAPPROCESS) so we are able to call Process32First/Next
  7.     // Remeber to close it when you don't use it anymore!
  8.     HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  9.     // Check if the snapshot created is valid
  10.     if (hSnapshot == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
  11.    
  12.     // Create the helper struct that will contain all the infos about the current process
  13.     // while we loop through all the running processes
  14.     PROCESSENTRY32 ProcEntry;
  15.     // Remember to set the dwSize member of ProcEntry to sizeof(PROCESSENTRY32)
  16.     ProcEntry.dwSize = sizeof(PROCESSENTRY32);
  17.    
  18.     // Call Process32First
  19.     if (Process32First(hSnapshot, &ProcEntry))
  20.     {
  21.         // Notice that you have to enable Multi-Byte character set in order
  22.         // to avoid converting everything.
  23.         // strcmp is not the only way to compare 2 strings ofc, work with your imagination
  24.         if (!strcmp(ProcEntry.szExeFile, ProcessName.c_str()))
  25.         {
  26.             // If we are here it means that the process has been found, we can
  27.             // open an handle to it and return it
  28.             // But first of all we have to close the snapshot handle!
  29.             CloseHandle(hSnapshot);
  30.             // Return the handle opened to the process with OpenProcess
  31.             // (Notice the usage of PROCESS_ALL_ACCESS flag in order to grant read/write privileges)
  32.             return OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
  33.         }
  34.     }
  35.     else
  36.     {
  37.         // If the Process32First call failed, it means that there is no
  38.         // process running in the first place, we can return directly.
  39.         CloseHandle(hSnapshot);
  40.         return INVALID_HANDLE_VALUE;
  41.     }
  42.    
  43.     // If we are here it means that the Process32First call returned TRUE, but the first process
  44.     // wasn't the process that we were searching for. Now we can loop through the processes
  45.     // using Process32Next
  46.     while (Process32Next(hSnapshot, &ProcEntry))
  47.     {
  48.         // We do the same check we did for Process32First
  49.         if (!strcmp(ProcEntry.szExeFile, ProcessName.c_str()))
  50.         {
  51.             // If we are here it means that the process has been found, we can
  52.             // open an handle to it and return it
  53.             // But first of all we have to close the snapshot handle!
  54.             CloseHandle(hSnapshot);
  55.             // Return the handle opened to the process with OpenProcess
  56.             // (Notice the usage of PROCESS_ALL_ACCESS flag in order to grant read/write privileges)
  57.             return OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
  58.         }
  59.     }
  60.     // Continue loop while the Process32Next call returns TRUE meaning that there are still processes to check
  61.    
  62.     // If we are here it means that the process has not been found or that there are no processes to scan for anymore.
  63.     // We can close the snapshot handle and return fail value.
  64.     CloseHandle(hSnapshot);
  65.     return INVALID_HANDLE_VALUE;
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top