home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / progs / ite / ite10d1.exe / DATA.Z / REGISTRY.JS < prev    next >
Encoding:
Text File  |  1996-09-07  |  11.0 KB  |  253 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. * Registry.js  --  Windows 32 System Registry Class                          *
  4. *                                                                            *
  5. * Registry.js contains the class definition for the Registry class. Using    *
  6. * this class you can read and write values in the Windows 32 registry.       *
  7. *                                                                            *
  8. * Syntax:                                                                    *
  9. *                                                                            *
  10. *    new Registry(<openKey>, <subKey>);                                      *
  11. *                                                                            *
  12. *    where <openKey> is a numeric value containing the handle to open        *
  13. *                    registry key. This will typically be one of the         *
  14. *                    system keys defined in WINREG.H.                        *
  15. *          <subKey> is a character string containing the name of a subkey    *
  16. *                   of <openKey>.                                            *
  17. *                                                                            *
  18. * Properties:                                                                *
  19. *                                                                            *
  20. *    error         -  Contains the Windows error number if an error occured  *
  21. *                     during the last registry operation. Contains 0 if not  *
  22. *                     error occured.                                         *
  23. *    newlyCreated  -  Set during instantiation. True if this is a new key,   *
  24. *                     false otherwise.                                       *
  25. *                                                                            *
  26. * Methods:                                                                   *
  27. *                                                                            *
  28. *    queryKeyName() - Returns the name of the current registry key.          * 
  29. *                                                                            *
  30. *    queryValue(<name>) - Returns the value associated with <name>. The      *
  31. *                         <name> parameter is required, but may be blank.    *
  32. *                         If blank the default value for the key is          *
  33. *                         returned.                                          *
  34. *                                                                            *
  35. *    setValue(<name>,<value>[,<type>])                                       *
  36. *                             - Sets the value of <name> to <value>. Both    *
  37. *                               parameters are required, but <name> may be   *
  38. *                               blank to set the default value for the key.  *
  39. *                               Returns a logical true or false to indicate  *
  40. *                               success or failure, respectively. If no type *
  41. *                               is indicated, the value is saved. The types  *
  42. *                               are defined in WINREG.H. The most common     *
  43. *                               type is REG_DWORD.                           *
  44. *                                                                            *
  45. * Example:                                                                   *
  46. *                                                                            *
  47. *    #include "winreg.h"                                                     *
  48. *    _sys.scripts.load("REGISTRY.JS");                                       *
  49. *    reg = new Registry(HKEY_LOCAL_MACHINE,                                  *
  50. *              "SOFTWARE\\Borland\\IntraBuilder\\1.0\\Server");              *
  51. *    if (reg.error == 0) {                                                   *
  52. *       oldSessions = reg.queryValue("MaxSessions");                         *
  53. *       if (reg.setValue("MaxSessions","15")) {                              *
  54. *          _sys.scriptOut.writeln("MaxSessions changed");                    *
  55. *          reg.setValue("MaxSessions",oldSessions);                          *
  56. *       }                                                                    *
  57. *       else {                                                               *
  58. *          _sys.scriptOut.writeln("error while changing MaxSessions");       *
  59. *       }                                                                    *
  60. *    }                                                                       *
  61. *                                                                            *
  62. * Updated 9/4/96 by IntraBuilder Samples Group                               *
  63. * $Revision:   1.1  $                                                        *
  64. *                                                                            *
  65. * Copyright (c) 1996, Borland International, Inc. All rights reserved.       *
  66. *                                                                            *
  67. \****************************************************************************/
  68. // Define Windows data types for use by the extern command
  69. #include "windef.h"
  70. #include "winreg.h"
  71.  
  72. class Registry(openKey, subKey) {
  73.    this.openKey = openKey;
  74.    this.subKey = subKey;
  75.    this.key = 0;
  76.    this.isOpen = false;
  77.    this.error = 0;
  78.    this.newlyCreated = false;
  79.  
  80.    class::prototype();
  81.  
  82.    var nKey = 0, nDisposition = -1, nResult;
  83.    nResult = RegCreateKeyEx(this.openKey, this.subKey, 0, 0, REG_OPTION_NON_VOLATILE, 
  84.              KEY_ALL_ACCESS, 0, nKey, nDisposition );
  85.    // store the handle to this key
  86.    this.key = nKey;
  87.    // registry keys should not be held open. If we got a key open
  88.    // close it for now.
  89.    if (nResult == ERROR_SUCCESS) {
  90.       if (nDisposition == REG_CREATED_NEW_KEY) {
  91.          this.newlyCreated = true;
  92.       }
  93.       this.close();
  94.    }
  95.    else {
  96.       this.error = nResult;
  97.    }
  98.  
  99.    function close() 
  100.    {
  101.       this.isOpen = false;
  102.       return RegCloseKey( this.key );
  103.    }
  104.  
  105.    function open() 
  106.    {
  107.       var nResult, nReturn;
  108.       nReturn = 0;            // handle of new key
  109.       nResult = RegOpenKeyEx(this.openKey, this.subKey, 0, 
  110.                 KEY_ALL_ACCESS, nReturn);
  111.       if (nResult == ERROR_SUCCESS) {
  112.          this.key = nReturn;
  113.          this.isOpen = true;
  114.       }
  115.       else {
  116.          this.error = (nResult);
  117.       }
  118.       return (nReturn);
  119.    }
  120.  
  121.    function prototype() 
  122.    {
  123.  
  124.       extern LONG    RegCloseKey( HKEY ) ADVAPI32;
  125.       extern LONG    RegCreateKeyEx( HKEY, LPCTSTR, DWORD, LPTSTR, DWORD, 
  126.                      REGSAM, LPSTRUCTURE, PHKEY, LPDWORD ) ADVAPI32 
  127.                      from "RegCreateKeyExA";
  128.       extern LONG    RegDeleteKey( HKEY, LPCTSTR ) ADVAPI32 
  129.                      from "RegDeleteKeyA";
  130.       extern LONG    RegDeleteValue( HKEY, LPTSTR ) ADVAPI32 
  131.                      from "RegDeleteValueA";
  132.       extern LONG    RegFlushKey( HKEY ) ADVAPI32;
  133.       extern LONG    RegOpenKeyEx( HKEY, LPCTSTR, DWORD, REGSAM, PHKEY ) 
  134.                      ADVAPI32 from "RegOpenKeyExA";
  135.       extern LONG    RegQueryValueEx( HKEY, LPTSTR, DWORD, LPDWORD, void *, 
  136.                      LPDWORD ) ADVAPI32 from "RegQueryValueExA";
  137.       extern LONG    RegSetValueEx( HKEY, LPCTSTR, DWORD, DWORD, void *, DWORD ) 
  138.                      ADVAPI32 from "RegSetValueExA";
  139.  
  140.    }
  141.  
  142.    function queryKeyName() {
  143.       var keyName="";
  144.       switch (this.openKey) {
  145.          case HKEY_CLASSES_ROOT:
  146.             keyName = "HKEY_CLASSES_ROOT\\";
  147.             break;
  148.          case HKEY_CURRENT_USER:
  149.             keyName = "HKEY_CURRENT_USER\\";
  150.             break;
  151.          case HKEY_LOCAL_MACHINE:
  152.             keyName = "HKEY_LOCAL_MACHINE\\";
  153.             break;
  154.          case HKEY_USERS:
  155.             keyName = "HKEY_USERS\\";
  156.             break;
  157.          case HKEY_PERFORMANCE_DATA:
  158.             keyName = "HKEY_PERFORMANCE_DATA\\";
  159.             break;
  160.          case HKEY_CURRENT_CONFIG:
  161.             keyName = "HKEY_CURRENT_CONFIG\\";
  162.             break;
  163.          case HKEY_DYN_DATA:
  164.             keyName = "HKEY_DYN_DATA\\";
  165.             break;
  166.          default:
  167.             keyName = "UNKNOWN_KEY\\";
  168.       }
  169.       return (keyName + this.subKey);
  170.    }
  171.  
  172.    function queryValue(keyName) {
  173.       var nResult = 0, 
  174.           nType = 0,
  175.           nLen = 80,
  176.           keyValue = false;
  177.  
  178.       var strEx = new StringEx();
  179.       var cData = strEx.replicate(" ", 80);
  180.  
  181.       // reset the error property
  182.       this.error = 0;
  183.  
  184.       // open up the key
  185.       this.open();
  186.  
  187.       if (this.isOpen) {
  188.          // query the value
  189.          nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
  190.  
  191.          // ERROR_MORE_DATA means we need to pass a larger cData
  192.          if (nResult == ERROR_MORE_DATA) {
  193.             cData = strEx.replicate(" ", nLen);
  194.             nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
  195.          }
  196.  
  197.          strEx.string = cData;
  198.          if (nResult == ERROR_SUCCESS) {
  199.             if (nType == REG_DWORD)
  200.                keyValue = strEx.asc(strEx.substring(0, 1)) * Math.pow(256,0) +
  201.                           strEx.asc(strEx.substring(1, 2)) * Math.pow(256,1) +
  202.                           strEx.asc(strEx.substring(2, 3)) * Math.pow(256,2) +
  203.                           strEx.asc(strEx.substring(3, 4)) * Math.pow(256,3);
  204.             else
  205.                keyValue = strEx.substring(0,nLen-1);
  206.          }
  207.          else
  208.             this.error = nResult;
  209.       }
  210.       return (keyValue);
  211.    }
  212.  
  213.    function setValue( valueName, value, type ) {
  214.  
  215.       var bReturn = false;
  216.       var strEx = new StringEx();
  217.       var nType = (setValue.arguments.length == 3) ? type : REG_SZ;
  218.       var xValue = value;
  219.  
  220.       // reset the error property
  221.       this.error = 0;
  222.  
  223.       // open the key
  224.       this.open();
  225.  
  226.       if (this.isOpen) {
  227.          // reformat data if necessary
  228.          if (nType == REG_DWORD) {
  229.             xValue = parseInt(value);
  230.             xValue = strEx.chr(parseInt(xValue/Math.pow(256,0)) % 256) +
  231.                      strEx.chr(parseInt(xValue/Math.pow(256,1)) % 256) +
  232.                      strEx.chr(parseInt(xValue/Math.pow(256,2)) % 256) +
  233.                      strEx.chr(parseInt(xValue/Math.pow(256,3)) % 256);
  234.          }
  235.          else {
  236.             xValue = value + "";
  237.             if (xValue.indexOf(strEx.chr(0)) >= 0) 
  238.                xValue = xValue.substring(0,xValue.indexOf(strEx.chr(0)+1));
  239.             else
  240.                xValue = xValue + strEx.chr(0);
  241.          }
  242.  
  243.          // Write the data to the registry
  244.          nLen = xValue.length;
  245.          nResult = RegSetValueEx(this.key, valueName, 0, nType, xValue, nLen);
  246.          if (nResult == ERROR_SUCCESS)
  247.             lReturn = true;
  248.          else
  249.             this.error = (lnResult);
  250.       }
  251.       return (lReturn);
  252.    }
  253. }