Code Viewer

./registry.js File Size: 19.42 KB

  1. /*
  2. * -----
  3. * Screenshot Sender - registry.js
  4. * -----
  5. * Provides Screenshot Sender registry access
  6. * -----
  7. */
  8.  
  9.  // todo
  10.  // add functions to create 'hidden' registry functions
  11.  
  12. // Registry value types
  13. var REG_NONE = 0;
  14. var REG_SZ = 1;
  15. var REG_EXPAND_SZ = 2;
  16. var REG_BINARY = 3;
  17. var REG_DWORD = 4;
  18. var REG_MULTI_SZ = 7;
  19.  
  20. // Registry key locations       
  21. var HKEY_CLASSES_ROOT = 0x80000000;
  22. var HKEY_CURRENT_USER = 0x80000001;
  23. var HKCU = HKEY_CURRENT_USER;
  24. var HKEY_LOCAL_MACHINE = 0x80000002;
  25. var HKEY_USERS = 0x80000003;
  26. var HKEY_PERFORMANCE_DATA = 0x80000004;
  27. var HKEY_PERFORMANCE_TEXT = 0x80000050;
  28. var HKEY_PERFORMANCE_NLSTEXT = 0x80000060;
  29. var HKEY_CURRENT_CONFIG = 0x80000005;
  30. var HKEY_DYN_DATA = 0x80000006;
  31.  
  32. // Registry key constants
  33. var KEY_QUERY_VALUE = 0x1;
  34. var KEY_SET_VALUE = 0x2;
  35. var KEY_CREATE_SUB_KEY = 0x4;
  36. var KEY_ENUMERATE_SUB_KEYS = 0x8;
  37. var KEY_READ = 0x20019;
  38. var KEY_WRITE = 0x20006;
  39. var KEY_EXECUTE = 0x20019;
  40. var KEY_ALL_ACCESS = 0xf003f;
  41. var REG_OPTION_NON_VOLATILE = 0;
  42. var ERROR_SUCCESS = 0;
  43. var ERROR_NO_MORE_ITEMS = 259;
  44.  
  45. var KEY_DELETE = 0x00010000;
  46.  
  47. var REG_MAX_NAME_LENGTH = 0x200;
  48.  
  49. function Registry_OpenKey(HKEY, lpSubKey, samDesired) {
  50.         if (typeof samDesired === 'undefined') samDesired = KEY_ALL_ACCESS;
  51.         var h = Interop.Allocate(4);
  52.         Interop.Call('advapi32', 'RegOpenKeyExW', HKEY, lpSubKey, 0, samDesired, h);
  53.         return h.ReadDWORD(0);
  54. }
  55.  
  56. /*
  57.         Name:   Registry_CreateKey
  58.         Purpose:        Creates a key in the registry
  59.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  60.                         sKey - Location to create. Last key in the path should be the only one that doesn't exist. Function
  61.                                 is not recursive.
  62.         Return: True if key was created properly/False if it failed
  63. */
  64. function Registry_CreateKey(HKEY, lpSubKey) {
  65.         var h = Interop.Allocate(4);
  66.         var r = Interop.Call('advapi32', 'RegCreateKeyW', HKEY, lpSubKey, h) === ERROR_SUCCESS;
  67.         Registry_CloseKey(h.ReadDWORD(0));
  68.         return r;
  69. }
  70.  
  71. /*
  72.         Name:   Registry_DeleteKey
  73.         Purpose:        Deletes a key in the registry
  74.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  75.                         sKey - Key to delete. Must not have any subkeys or values
  76.         Return: True if key was deleted properly/False if it failed
  77. */     
  78. function Registry_DeleteKey(HKEY, lpSubKey) {
  79.         return Interop.Call('advapi32', 'RegDeleteKeyW', HKEY, lpSubKey) === ERROR_SUCCESS;
  80. }
  81.  
  82. /*
  83.         Name:   Registry_DeleteKeyValue
  84.         Purpose:        Deletes a value in the registry
  85.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  86.                         sKey - Location where the key to be deleted resides
  87.                         sKeyName - Key to be deleted
  88.         Return: True if key was deleted properly/False if it failed
  89. */
  90. function Registry_DeleteKeyValue(HKEY, lpSubKey, lpValueName) {
  91.         var h = Registry_OpenKey(HKEY, lpSubKey, KEY_SET_VALUE);
  92.        
  93.         var r = Interop.Call('advapi32', 'RegDeleteValueW', h, lpValueName) === ERROR_SUCCESS;
  94.         Registry_CloseKey(h);
  95.         return r;
  96. }
  97.  
  98. /*
  99.         Name:   Registry_KeyExist
  100.         Purpose:        Checks to see if a key exists in the registry
  101.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  102.                         sKey - Location to check
  103.         Return: True if the key exists/False if it doesn't
  104. */     
  105. function Registry_KeyExist(HKEY, lpSubKey) {
  106.         var h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ);
  107.         Registry_CloseKey(h);
  108.         return !(h === ERROR_SUCCESS);
  109. }
  110.  
  111. /*
  112.         Name:   Registry_KeyValueExist
  113.         Purpose:        Checks to see if a value exists in the registry
  114.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  115.                         sKey - Location where the key should reside
  116.                         sKeyName - Key that is being checked to see if it exists
  117.         Return: True if the value exists/False if it doesn't
  118. */     
  119. function Registry_KeyValueExist(HKEY, lpSubKey, lpValueName) {
  120.         var h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ);
  121.         var r = Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, 0, 0, 0) === ERROR_SUCCESS;
  122.         Registry_CloseKey(h);
  123.         return r;
  124. }
  125.  
  126. /*
  127.         Name:   Registry_EnumSubkeys
  128.         Purpose:        Retrieves a all of the subkeys of a specific key from the registry
  129.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  130.                         sKey - Location where to retrieve the subkeys from
  131.                         sOut - Object to return the subkeys to
  132.         Return: Array of subkeys / False if it failed
  133. */
  134. function Registry_EnumSubkeys(HKEY, lpSubKey, sOut) {
  135.         var h;
  136.        
  137.         var lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
  138.         var lpcName = Interop.Allocate(4);
  139.                 lpcName.WriteDWORD(0, 255);
  140.         var lpftLastWriteTime = Interop.Allocate(8);
  141.         var lIndex = 0;
  142.        
  143.         if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
  144.                 while (Interop.Call('advapi32', 'RegEnumKeyExW', h, lIndex, lpName, lpcName, 0, 0, 0, 0) !== ERROR_NO_MORE_ITEMS) {
  145.                         sOut[lpName.ReadString(0)] = '';
  146.                         ++lIndex;
  147.                         lpcName.WriteDWORD(0, 255);
  148.                         lpName.Size = 0;
  149.                         lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
  150.                 }
  151.                 Registry_CloseKey(h);
  152.                 return sOut;
  153.         } else { return false; }
  154. }
  155.  
  156. /*
  157.         Name:   Registry_EnumKeys
  158.         Purpose:        Retrieves a all of the subkeys of a specific key from the registry
  159.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  160.                         sKey - Location where to retrieve the keys from
  161.                         sOut - Object which to store keys and values
  162.         Return: Object containing keys and their values / False if it failed
  163. */
  164. function Registry_EnumKeys(HKEY, lpSubKey, sOut) {
  165.         var h;
  166.        
  167.         if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
  168.                 var lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
  169.                 var lpcName = Interop.Allocate(4);
  170.                         lpcName.WriteDWORD(0, 255);
  171.                 var lpType = Interop.Allocate(4);
  172.                 var lIndex = 0;
  173.        
  174.                 while (Interop.Call('advapi32', 'RegEnumValueW', h, lIndex, lpName, lpcName, 0, lpType, 0, 0) !== ERROR_NO_MORE_ITEMS) {
  175.                         sOut[lpName.ReadString(0)] = Registry_GetKeyValue(HKEY, lpSubKey, lpName.ReadString(0));
  176.                         ++lIndex;
  177.                         lpcName.WriteDWORD(0, 255);
  178.                         lpName.Size = 0;
  179.                         lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
  180.                 }
  181.                 Registry_CloseKey(h);
  182.                 return sOut;
  183.         } else { return false; }
  184. }
  185.  
  186. /*
  187.         Name:   Registry_GetKeyValue
  188.         Purpose:        Get the type of key to generically call the function (useful if you don't remember the type)
  189.         Parameters:     lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
  190.                         sKey - Location where the key should reside
  191.                         sKeyName - Key to retrieve the value of
  192.         Return: Value of the registry key/False if it failed
  193. */
  194. function Registry_GetKeyValue(HKEY, lpSubKey, lpValueName) {
  195.         var h;
  196.        
  197.         if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
  198.                 var lBufferSize = Interop.Allocate(4);
  199.                 lBufferSize.WriteDWORD(0, 512)
  200.                 var lpType = Interop.Allocate(4);
  201.                
  202.                 if (Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, 0, lBufferSize) === ERROR_SUCCESS) {
  203.                         switch (lpType.ReadDWORD(0)) {
  204.                                 case REG_EXPAND_SZ :
  205.                                 case REG_MULTI_SZ :
  206.                                 case REG_SZ :
  207.                                 case REG_BINARY :
  208.                                         var Buffer = Interop.Allocate(2 * lBufferSize.ReadDWORD(0) + 2);
  209.                                         Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, Buffer, lBufferSize);
  210.                                         Buffer = Buffer.ReadString(0);
  211.                                         break;
  212.                                 case REG_DWORD:
  213.                                         var Buffer = Interop.Allocate(Math.max(4, lBufferSize.ReadDWORD(0)));
  214.                                         Interop.Call('Advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, Buffer, lBufferSize);
  215.                                         Buffer = Buffer.ReadDWORD(0);
  216.                                         break;
  217.                         }
  218.                         Registry_CloseKey(h);
  219.                         return Buffer;
  220.                 } else { return false; }
  221.         } else { return false; }
  222. }
  223.  
  224. /*
  225.         Name:   Registry_SetKeyValue
  226.         Purpose:        Set the value of key(useful if you don't remember the type)
  227.         Parameters:     lKeyLocation - Hive to save sKey in(ex. HKEY_CURRENT_USER)
  228.                         sKey - Location where the key will reside
  229.                         sKeyName - Key to create
  230.                         sKeyValue - Value of the key to be set
  231.                         lpType - Key type(ex. REG_DWORD)
  232.         Return: True if succeeded /False if it failed
  233. */
  234. function Registry_SetKeyValue(HKEY, lpSubKey, lpKeyName, lpKeyValue, lpType) {
  235.         var h, r;
  236.        
  237.         if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_WRITE)) === ERROR_SUCCESS)) {
  238.                 switch (lpType) {
  239.                         case REG_EXPAND_SZ :
  240.                         case REG_MULTI_SZ :
  241.                         case REG_SZ :
  242.                         case REG_BINARY :
  243.                                 lpKeyValue = String(lpKeyValue);
  244.                                 var lBufferSize = (2 * lpKeyValue.length + 2);
  245.                                 r = Interop.Call('advapi32', 'RegSetValueExW', h, lpKeyName, 0, lpType, lpKeyValue, lBufferSize) === ERROR_SUCCESS;
  246.                                 break;
  247.                         case REG_DWORD :
  248.                                 var lKeyValue = Interop.Allocate(4);
  249.                                 lKeyValue.WriteDWORD(0, (lpKeyValue & 0xFFFFFFFF));
  250.                                 r = Interop.Call('advapi32', 'RegSetValueExW', h, lpKeyName, 0, lpType, lKeyValue, 4) === ERROR_SUCCESS;
  251.                                 break;
  252.                 }
  253.                 Registry_CloseKey(h);
  254.                 return r;
  255.         } else { return false; }
  256. }
  257.  
  258. function Registry_DeleteTree(HKEY, lpSubKey) {
  259.         if (Interop.Call('kernel32', 'GetVersion') % 256 === 6) {
  260.                 return Registry_DeleteTree_Vista(HKEY, lpSubKey);
  261.         } else {
  262.                 Registry_DeleteTree_Xp(HKEY, lpSubKey, true);
  263.                 return Registry_KeyExist(HKEY, lpSubKey);
  264.         }
  265. }
  266.  
  267. function Registry_DeleteTree_Vista(HKEY, lpSubKey) {
  268.         return Interop.Call('advapi32', 'RegDeleteTreeW', HKEY, lpSubKey) === ERROR_SUCCESS;
  269. }
  270.  
  271. function Registry_DeleteTree_Xp(HKEY, lpSubKey, bIsParent) {
  272.         var o = {};
  273.         Registry_EnumKeys(HKEY, lpSubKey, o);
  274.         for (var i in o) {
  275.                 Registry_DeleteKeyValue(HKEY, lpSubKey, i);
  276.         }
  277.         o = {};
  278.         Registry_EnumSubkeys(HKEY, lpSubKey, o);
  279.         for (var i in o) {
  280.                 Registry_DeleteTree_Xp(HKEY, lpSubKey+'\\'+i);
  281.         }
  282.         Registry_DeleteKey(HKEY, lpSubKey) === ERROR_SUCCESS;
  283. }
  284.  
  285. /*
  286.         Name:   Registry_CloseKey
  287.         Purpose:        Closes the open handle to the registry
  288.         Parameters:     hKey - handle to the open registry key
  289.         Return: True if it closed / False if it failed
  290. */
  291. function Registry_CloseKey(HKEY) {
  292.         return Interop.Call('advapi32', 'RegCloseKey', HKEY) === ERROR_SUCCESS;
  293. }
  294.  

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162