Advertisement
williamjcm

Registry access fail

Sep 26th, 2014
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. /***********************************
  2. NOTE: All app and variable names are
  3. changed.
  4. ------------------------------------
  5. Included files: iostream, string,
  6. winreg.h (through windows.h)
  7. IDE: Visual C++ 2008 Express
  8. ***********************************/
  9.  
  10. /***********************************
  11. The data structure that can be seen
  12. below looks like this:
  13.  
  14. struct apps
  15. {
  16.     string installPath;
  17.     string exePath;
  18.     string iniPath;
  19. };
  20. ***********************************/
  21.  
  22.  
  23. #define BUFFER 8192
  24.  
  25. void getApps()
  26. {
  27.     char ValueBuffer[255];
  28.     HKEY hKey;
  29.     DWORD BufferSize = BUFFER;
  30.  
  31.     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\AppVendor\\APPKEY1", 0, KEY_READ, &hKey) == ERROR_SUCCESS) //This one succeeds
  32.     {
  33.         RegGetValue(hKey, NULL, "PATH", RRF_RT_ANY, NULL, (PVOID)&ValueBuffer, &BufferSize);
  34.         cout << "1: Application 1" << endl;
  35.  
  36.         string bufferConvert(ValueBuffer);
  37.  
  38.         app1.installPath = bufferConvert;
  39.         app1.exePath = app1.installPath + "app1.exe";
  40.         app1.iniPath = app1.installPath + "app1.ini";
  41.  
  42.         memset(ValueBuffer, '\0', 255 );
  43.     }
  44.     else
  45.     {
  46.         cerr << "ERROR opening APPKEY1" << endl; //
  47.     }
  48.     RegCloseKey(hKey);
  49.  
  50.     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\AppVendor\\APPKEY2", 0, KEY_READ, &hKey) == ERROR_SUCCESS) //Strangely, this key opens...
  51.     {
  52.         RegGetValue(hKey, NULL, "PATH", RRF_RT_ANY, NULL, (PVOID)&ValueBuffer, &BufferSize); //...But this function fails (Error 234: ERROR_MORE_DATA, according to MSDN)
  53.         string bufferConvert(ValueBuffer);
  54.         app2.installPath = bufferConvert;
  55.         memset(ValueBuffer, '\0', 255 );
  56.  
  57.         //This "if...else if" structure is there because there are two mutually exclusive versions of the app
  58.         RegGetValue(hKey, NULL, "EXEFILE", RRF_RT_ANY, NULL, (PVOID)&ValueBuffer, &BufferSize); //This one also fails
  59.         if(ValueBuffer == "app2verA.exe")
  60.         {
  61.             cout << "2: Application 2 Version A" << endl;
  62.             app2.exePath = app2.installPath + "app2verA.exe";
  63.             app2.iniPath = app2.installPath + "app2verA.ini";
  64.         }
  65.         else if(ValueBuffer == "app2verB.exe")
  66.         {
  67.             cout << "2: Application 2 Version B" << endl;
  68.             app2.exePath = app2.installPath + "app2verB.exe";
  69.             app2.iniPath = app2.installPath + "app2verB.ini";
  70.         }
  71.  
  72.         memset(ValueBuffer, '\0', 255 );
  73.     }
  74.     else
  75.     {
  76.         cerr << "ERROR opening APPKEY2" << endl;
  77.     }
  78.     RegCloseKey(hKey);
  79.  
  80.     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\AppVendor\\APPKEY3", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
  81.     {
  82.         RegGetValue(HKEY_LOCAL_MACHINE, NULL, "PATH", RRF_RT_ANY, NULL, (PVOID)&ValueBuffer, &BufferSize); //Works, though it's the same as before
  83.         cout << "3: Application 3" << endl;
  84.  
  85.         RegGetValue(HKEY_LOCAL_MACHINE, NULL, "PATH", NULL, NULL, &ValueBuffer, &BufferSize);
  86.         string bufferConvert(ValueBuffer);
  87.         app3.installPath = bufferConvert;
  88.         app3.exePath = app3.installPath + "app3.exe";
  89.         app3.iniPath = app3.installPath + "app3.ini";
  90.  
  91.         memset(ValueBuffer, '\0', 255 );
  92.     }
  93.     else
  94.     {
  95.         cerr << "ERROR opening APPKEY3" << endl;
  96.     }
  97.     RegCloseKey(hKey);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement