Guest User

Untitled

a guest
Mar 7th, 2021
210
297 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Prevent Mystic Lights from turning off when the
  3.  * screen turns off by setting DisplayPower to ON
  4.  */
  5.  
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <windows.h>
  9. #include <process.h>
  10. #include <tlhelp32.h>
  11. #include <winbase.h>
  12. #include <string.h>
  13.  
  14. LPCSTR key1String = "SOFTWARE\\WOW6432Node\\MSI\\One Dragon Center\\Component\\Mystic_Light\\LED";
  15. LPCSTR key2String = "SOFTWARE\\WOW6432Node\\Wow6432Node\\MSI\\MSI Center\\Component\\Mystic Light\\LED";
  16.  
  17. const char* appname1 = "DCv2.exe";
  18. const char* appname2 = "LEDKeeper2.exe";
  19.  
  20. int counterLoop = 0;
  21.  
  22. std::string vk1, vk2;
  23. char setValue1[] = "ON";
  24. char setValue2[] = "ON";
  25.  
  26. // Open the keys
  27.  
  28. HKEY key1Handle;
  29. HKEY key2Handle;
  30.  
  31. LSTATUS key1Status = RegOpenKeyExA(
  32.     HKEY_LOCAL_MACHINE, key1String, 0, KEY_ALL_ACCESS, &key1Handle
  33. );
  34.  
  35. LSTATUS key2Status = RegOpenKeyExA(
  36.     HKEY_LOCAL_MACHINE, key2String, 0, KEY_ALL_ACCESS, &key2Handle
  37. );
  38.  
  39.  
  40. // Get value from registry
  41.  
  42. LSTATUS getValue(HKEY keyHandle, LPCSTR index, std::string &value) {
  43.  
  44.     std::string default_value = "NO_VALUE_FOUND";
  45.     CHAR buffer[32];
  46.     DWORD sbuffer = sizeof(buffer);
  47.  
  48.     LSTATUS query = RegGetValueA(
  49.         keyHandle, NULL, index, RRF_RT_ANY, NULL, (LPBYTE)buffer, &sbuffer
  50.     );
  51.  
  52.     value = (ERROR_SUCCESS == query) ? buffer : default_value;
  53.    
  54.     return query;
  55.    
  56. }
  57.  
  58.  
  59. // Set value in registry
  60.  
  61. LSTATUS setValue(HKEY keyHandle, LPCSTR index, LPBYTE input) {
  62.  
  63.     DWORD sValue = sizeof(input);
  64.  
  65.     LSTATUS query = RegSetValueExA(
  66.         keyHandle, index, 0, REG_SZ, input, sValue
  67.     );
  68.    
  69.     return query;
  70.    
  71. }
  72.  
  73.  
  74. // Kill app by name
  75.  
  76. void killProcessByName(const char* filename) {
  77.  
  78.     HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  79.     PROCESSENTRY32 pEntry;
  80.  
  81.     pEntry.dwSize = sizeof(pEntry);
  82.     BOOL hRes = Process32First(hSnapShot, &pEntry);
  83.  
  84.     while(hRes) {
  85.         if(strcmp(pEntry.szExeFile, filename) == 0) {
  86.             HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, (DWORD)pEntry.th32ProcessID);
  87.             if(hProcess != NULL)
  88.             {
  89.                 TerminateProcess(hProcess, 9);
  90.                 CloseHandle(hProcess);
  91.             }
  92.         }
  93.         hRes = Process32Next(hSnapShot, &pEntry);
  94.     }
  95.  
  96.     CloseHandle(hSnapShot);
  97.  
  98. }
  99.  
  100.  
  101. int main() {
  102.  
  103.     // No access, return
  104.     if(key1Status == 5 || key2Status == 5) {
  105.         std::cout << "Access denied. Run as admin." << std::endl;
  106.         system("pause");
  107.         return 0;
  108.     }
  109.    
  110.     system("pause");
  111.  
  112.     std::cout << "Stopping programs..." << std::endl;
  113.  
  114.     killProcessByName(appname1);
  115.     killProcessByName(appname2);
  116.    
  117.     system("pause");
  118.  
  119.     std::cout << "Checking register values..." << std::endl;
  120.  
  121.     LSTATUS q1 = getValue(key1Handle, "DisplayPower", vk1);
  122.     LSTATUS q2 = getValue(key2Handle, "DisplayPower", vk2);
  123.    
  124.     // One of the get queries returned an windows error
  125.     if(q1 != 0 || q2 != 0) {
  126.         std::cout << "Windows system-error [key1, key2]: " << q1 << ", "<< q2 << std::endl;
  127.     }
  128.    
  129.     // Check current values and update if not correct
  130.     if(vk1 != "ON") {
  131.         LSTATUS set1 = setValue(key1Handle, "DisplayPower", (LPBYTE)setValue1);
  132.         if(set1 != 0) {
  133.             std::cout << "[ERROR (key1)]: " << set1 << std::endl;
  134.         } else {
  135.             std::cout << "[OK] Updated key1: " << key1String << std::endl;
  136.         }
  137.     }
  138.     if(vk2 != "ON") {
  139.         LSTATUS set2 = setValue(key2Handle, "DisplayPower", (LPBYTE)setValue2);
  140.         if(set2 != 0) {
  141.             std::cout << "[ERROR (key2)]: " << set2 << std::endl;
  142.         } else {
  143.             std::cout << "[OK] Updated key2: " << key2String << std::endl;
  144.         }
  145.     }
  146.    
  147.     system("pause");
  148.  
  149.     return 0;
  150.  
  151. }
  152.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×