home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / ib / setups / intrabld / data.z / REGISTRY.JS < prev    next >
Text File  |  1996-12-11  |  15KB  |  401 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. *    deleteValue([<name>]) - Delete the named value from the current key. If *
  29. *                            no name is passed, then the default value for   *
  30. *                            this key is deleted.                            *
  31. *                                                                            *
  32. *    enumValue() - Returns an array containing the names of each value       *
  33. *                  contained in the current key.                             *
  34. *                                                                            *
  35. *    queryKeyName() - Returns the name of the current registry key.          * 
  36. *                                                                            *
  37. *    queryValue(<name>) - Returns the value associated with <name>. The      *
  38. *                         <name> parameter is required, but may be blank.    *
  39. *                         If blank the default value for the key is          *
  40. *                         returned.                                          *
  41. *                                                                            *
  42. *    setValue(<name>,<value>[,<type>])                                       *
  43. *                             - Sets the value of <name> to <value>. Both    *
  44. *                               parameters are required, but <name> may be   *
  45. *                               blank to set the default value for the key.  *
  46. *                               Returns a logical true or false to indicate  *
  47. *                               success or failure, respectively. If no type *
  48. *                               is indicated, the value is saved. The types  *
  49. *                               are defined in WINREG.H. The most common     *
  50. *                               type is REG_DWORD.                           *
  51. *                                                                            *
  52. * Example:                                                                   *
  53. *                                                                            *
  54. *    #include "winreg.h"                                                     *
  55. *    _sys.scripts.load("REGISTRY.JS");                                       *
  56. *    reg = new Registry(HKEY_LOCAL_MACHINE,                                  *
  57. *              "SOFTWARE\\Borland\\IntraBuilder\\1.0\\Server");              *
  58. *    if (reg.error == 0) {                                                   *
  59. *       oldSessions = reg.queryValue("MaxSessions");                         *
  60. *       if (reg.setValue("MaxSessions","15")) {                              *
  61. *          _sys.scriptOut.writeln("MaxSessions changed");                    *
  62. *          reg.setValue("MaxSessions",oldSessions);                          *
  63. *       }                                                                    *
  64. *       else {                                                               *
  65. *          _sys.scriptOut.writeln("error while changing MaxSessions");       *
  66. *       }                                                                    *
  67. *    }                                                                       *
  68. *                                                                            *
  69. * Updated 10/23/96 by IntraBuilder Samples Group                             *
  70. * $Revision:   1.2  $                                                        *
  71. *                                                                            *
  72. * Copyright (c) 1996, Borland International, Inc. All rights reserved.       *
  73. *                                                                            *
  74. \****************************************************************************/
  75. //
  76. //  These next two lines are used for debugging purposes. To trace the
  77. //  results of the API calls, uncomment the #define DEBUG line. The
  78. //  results are written to the ScriptPad window.
  79. //
  80. //#define DEBUG
  81. #define so(x) _sys.scriptOut.writeln(x)
  82.  
  83. // Define Windows data types for use by the extern command
  84. #include "windef.h"
  85. #include "winreg.h"
  86.  
  87. class Registry(openKey, subKey) {
  88.    this.openKey = openKey;
  89.    this.subKey = subKey;
  90.    this.key = 0;
  91.    this.isOpen = false;
  92.    this.error = 0;
  93.    this.newlyCreated = false;
  94.  
  95.    class::prototype();
  96.  
  97.    var nKey = 0, nDisposition = -1, nResult;
  98.    nResult = RegCreateKeyEx(this.openKey, this.subKey, 0, 0, REG_OPTION_NON_VOLATILE, 
  99.              KEY_ALL_ACCESS, 0, nKey, nDisposition );
  100.  
  101. #ifdef DEBUG
  102. so("constructor - " + nResult);
  103. so("disposition - " + nDisposition);
  104. #endif
  105.  
  106.    // store the handle to this key
  107.    this.key = nKey;
  108.    // registry keys should not be held open. If we got a key open
  109.    // close it for now.
  110.    if (nResult == ERROR_SUCCESS) {
  111.       if (nDisposition == REG_CREATED_NEW_KEY) {
  112.          this.newlyCreated = true;
  113.       }
  114.  
  115. #ifdef DEBUG
  116. so("created - " + this.newlyCreated);
  117. #endif
  118.  
  119.       this.close();
  120.    }
  121.    else {
  122.       this.error = nResult;
  123.    }
  124.  
  125.    function close() 
  126.    {
  127.       this.isOpen = false;
  128.       var nResult = RegCloseKey( this.key );
  129.  
  130. #ifdef DEBUG
  131. so("close - " + nResult);
  132. #endif
  133.  
  134.       return (nResult);
  135.    }
  136.  
  137.    function deleteValue(keyName)
  138.    {
  139.       var bReturn = false;
  140.  
  141.       // reset the error property
  142.       this.error = 0;
  143.  
  144.       // open up the key
  145.       this.open();
  146.  
  147.       if (this.isOpen) {
  148.          bReturn = true;
  149.          // if one parameter is passed, delete that value
  150.          if (deleteValue.arguments.length == 1) {
  151.             var nResult = RegDeleteValue(this.key, keyName);
  152.          }
  153.          // if no parameter is passed, delete default value
  154.          else {
  155.             var nResult = RegDeleteValue(this.key, "");
  156.          }
  157.  
  158. #ifdef DEBUG
  159. so("delete - " + nResult);
  160. #endif
  161.  
  162.          this.close();
  163.       }
  164.       return (bReturn);
  165.    }
  166.  
  167.    function enumValue()
  168.    {
  169.       var aReturn = new Array(),
  170.           nResult = ERROR_SUCCESS,
  171.           nCount = 0,
  172.           sValue = "",
  173.           nLen = 0,
  174.           string80 = new StringEx().replicate(" ", 80);
  175.  
  176.       // reset the error property
  177.       this.error = 0;
  178.  
  179.       // open up the key
  180.       this.open();
  181.  
  182.       if (this.isOpen) {
  183.          while (nResult == ERROR_SUCCESS) {
  184.             sValue = string80;
  185.             nLen = sValue.length;
  186.             nResult = RegEnumValue(this.key, nCount, sValue, nLen, 0, 0, 0, 0);
  187.  
  188. #ifdef DEBUG
  189. so("enum - " + nCount + " - " + nResult);
  190. #endif
  191.  
  192.             if (nResult == ERROR_SUCCESS)
  193.                aReturn.add(sValue.substring(0,nLen));
  194.             else if (nResult != ERROR_NO_MORE_ITEMS)
  195.                this.error = nResult;
  196.             nCount ++               
  197.          }           
  198.          this.close(); 
  199.       }
  200.       return (aReturn);
  201.    }
  202.  
  203.    function open() 
  204.    {
  205.       var nResult, nReturn;
  206.       nReturn = 0;            // handle of new key
  207.       nResult = RegOpenKeyEx(this.openKey, this.subKey, 0, 
  208.                 KEY_ALL_ACCESS, nReturn);
  209.  
  210. #ifdef DEBUG
  211. so("open - " + nResult);
  212. #endif
  213.  
  214.       if (nResult == ERROR_SUCCESS) {
  215.          this.key = nReturn;
  216.          this.isOpen = true;
  217.       }
  218.       else {
  219.          this.error = (nResult);
  220.       }
  221.       return (nReturn);
  222.    }
  223.  
  224.    function prototype() 
  225.    {
  226.  
  227.       extern LONG    RegCloseKey( HKEY ) ADVAPI32;
  228.       extern LONG    RegCreateKeyEx( HKEY, LPCTSTR, DWORD, LPTSTR, DWORD, 
  229.                      REGSAM, LPSTRUCTURE, PHKEY, LPDWORD ) ADVAPI32 
  230.                      from "RegCreateKeyExA";
  231.       extern LONG    RegDeleteValue( HKEY, LPTSTR ) ADVAPI32 
  232.                      from "RegDeleteValueA";
  233.       extern LONG    RegEnumValue( HKEY, DWORD, LPTSTR, LPDWORD, DWORD,
  234.                      LPDWORD, LPBYTE, LPDWORD) ADVAPI32 from "RegEnumValueA";
  235.       extern LONG    RegOpenKeyEx( HKEY, LPCTSTR, DWORD, REGSAM, PHKEY ) 
  236.                      ADVAPI32 from "RegOpenKeyExA";
  237.       extern LONG    RegQueryValueEx( HKEY, LPTSTR, DWORD, LPDWORD, void*, 
  238.                      LPDWORD ) ADVAPI32 from "RegQueryValueExA";
  239.       extern LONG    RegSetValueEx( HKEY, LPCTSTR, DWORD, DWORD, void*, DWORD ) 
  240.                      ADVAPI32 from "RegSetValueExA";
  241. #ifdef __asian__
  242.       extern LONG    RegQueryValueExChar( HKEY, LPTSTR, DWORD, LPDWORD, char*, 
  243.                      LPDWORD ) ADVAPI32 from "RegQueryValueExA";
  244.       extern LONG    RegSetValueExChar( HKEY, LPCTSTR, DWORD, DWORD, char*, DWORD ) 
  245.                      ADVAPI32 from "RegSetValueExA";
  246. #endif
  247.  
  248.    }
  249.  
  250.    function queryKeyName() {
  251.       var keyName="";
  252.       switch (this.openKey) {
  253.          case HKEY_CLASSES_ROOT:
  254.             keyName = "HKEY_CLASSES_ROOT\\";
  255.             break;
  256.          case HKEY_CURRENT_USER:
  257.             keyName = "HKEY_CURRENT_USER\\";
  258.             break;
  259.          case HKEY_LOCAL_MACHINE:
  260.             keyName = "HKEY_LOCAL_MACHINE\\";
  261.             break;
  262.          case HKEY_USERS:
  263.             keyName = "HKEY_USERS\\";
  264.             break;
  265.          case HKEY_PERFORMANCE_DATA:
  266.             keyName = "HKEY_PERFORMANCE_DATA\\";
  267.             break;
  268.          case HKEY_CURRENT_CONFIG:
  269.             keyName = "HKEY_CURRENT_CONFIG\\";
  270.             break;
  271.          case HKEY_DYN_DATA:
  272.             keyName = "HKEY_DYN_DATA\\";
  273.             break;
  274.          default:
  275.             keyName = "UNKNOWN_KEY\\";
  276.       }
  277.       return (keyName + this.subKey);
  278.    }
  279.  
  280.    function queryValue(keyName) {
  281.       var nResult = 0, 
  282.           nType = 0,
  283.           nLen = 80,
  284.           keyValue = false;
  285.  
  286.       var strEx = new StringEx();
  287.       var cData = strEx.replicate(" ", 80);
  288.  
  289.       // reset the error property
  290.       this.error = 0;
  291.  
  292.       // open up the key
  293.       this.open();
  294.  
  295.       if (this.isOpen) {
  296.          // query the value
  297.          nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
  298.  
  299. #ifdef DEBUG
  300. so("query - " + nResult);
  301. #endif
  302.  
  303.          // ERROR_MORE_DATA means we need to pass a larger cData
  304.          if (nResult == ERROR_MORE_DATA) {
  305.             cData = strEx.replicate(" ", nLen);
  306.             nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
  307.  
  308. #ifdef DEBUG
  309. so("requery - " + nResult);
  310. #endif
  311.  
  312.          }
  313.  
  314. #ifdef __asian__
  315.          // The Asian version uses Unicode strings. Call RegQueryValueExChar, which 
  316.          // is prototyped to char*, which converts the string to multi-byte.
  317.          if (nResult == ERROR_SUCCESS && nType == REG_SZ) {
  318.             cData = strEx.replicate(" ", nLen);
  319.             nResult = RegQueryValueExChar(this.key, keyName, 0, nType, cData, nLen);
  320.  
  321. #ifdef DEBUG
  322. so("UNICODE requery - " + nResult);
  323. #endif
  324.  
  325.          }
  326. #endif
  327.  
  328.          strEx.string = cData;
  329.          if (nResult == ERROR_SUCCESS) {
  330.             if (nType == REG_DWORD)
  331.                keyValue = strEx.asc(strEx.substring(0, 1)) * Math.pow(256,0) +
  332.                           strEx.asc(strEx.substring(1, 2)) * Math.pow(256,1) +
  333.                           strEx.asc(strEx.substring(2, 3)) * Math.pow(256,2) +
  334.                           strEx.asc(strEx.substring(3, 4)) * Math.pow(256,3);
  335.             else {
  336.                keyValue = strEx.substring(0,nLen-1);
  337.             }
  338.          }
  339.          else
  340.             this.error = nResult;
  341.          this.close();
  342.       }
  343.       return (keyValue);
  344.    }
  345.  
  346.    function setValue( valueName, value, type ) {
  347.  
  348.       var bReturn = false;
  349.       var strEx = new StringEx();
  350.       var nType = (setValue.arguments.length == 3) ? type : REG_SZ;
  351.       var xValue = value;
  352.  
  353.       // reset the error property
  354.       this.error = 0;
  355.  
  356.       // open the key
  357.       this.open();
  358.  
  359.       if (this.isOpen) {
  360.          // reformat data if necessary
  361.          if (nType == REG_DWORD) {
  362.             xValue = parseInt(value);
  363.             xValue = strEx.chr(parseInt(xValue/Math.pow(256,0)) % 256) +
  364.                      strEx.chr(parseInt(xValue/Math.pow(256,1)) % 256) +
  365.                      strEx.chr(parseInt(xValue/Math.pow(256,2)) % 256) +
  366.                      strEx.chr(parseInt(xValue/Math.pow(256,3)) % 256);
  367.          }
  368.          else {
  369.             xValue = value + "";
  370.             if (xValue.indexOf(strEx.chr(0)) >= 0) 
  371.                xValue = xValue.substring(0,xValue.indexOf(strEx.chr(0)+1));
  372.             else
  373.                xValue = xValue + strEx.chr(0);
  374.          }
  375.  
  376.          // Write the data to the registry
  377.          nLen = xValue.length;
  378. #ifdef __asian__
  379.          if (nType == REG_SZ) {
  380.             nResult = RegSetValueExChar(this.key, valueName, 0, nType, xValue, nLen);
  381.          }
  382.          else {
  383. #endif
  384.             nResult = RegSetValueEx(this.key, valueName, 0, nType, xValue, nLen);
  385. #ifdef __asian__
  386.          }
  387. #endif
  388.  
  389. #ifdef DEBUG
  390. so("setvalue - " + nResult);
  391. #endif
  392.  
  393.          if (nResult == ERROR_SUCCESS)
  394.             lReturn = true;
  395.          else
  396.             this.error = (lnResult);
  397.          this.close();
  398.       }
  399.       return (lReturn);
  400.    }
  401. }