home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************\
- * *
- * Registry.js -- Windows 32 System Registry Class *
- * *
- * Registry.js contains the class definition for the Registry class. Using *
- * this class you can read and write values in the Windows 32 registry. *
- * *
- * Syntax: *
- * *
- * new Registry(<openKey>, <subKey>); *
- * *
- * where <openKey> is a numeric value containing the handle to open *
- * registry key. This will typically be one of the *
- * system keys defined in WINREG.H. *
- * <subKey> is a character string containing the name of a subkey *
- * of <openKey>. *
- * *
- * Properties: *
- * *
- * error - Contains the Windows error number if an error occured *
- * during the last registry operation. Contains 0 if not *
- * error occured. *
- * newlyCreated - Set during instantiation. True if this is a new key, *
- * false otherwise. *
- * *
- * Methods: *
- * *
- * queryKeyName() - Returns the name of the current registry key. *
- * *
- * queryValue(<name>) - Returns the value associated with <name>. The *
- * <name> parameter is required, but may be blank. *
- * If blank the default value for the key is *
- * returned. *
- * *
- * setValue(<name>,<value>[,<type>]) *
- * - Sets the value of <name> to <value>. Both *
- * parameters are required, but <name> may be *
- * blank to set the default value for the key. *
- * Returns a logical true or false to indicate *
- * success or failure, respectively. If no type *
- * is indicated, the value is saved. The types *
- * are defined in WINREG.H. The most common *
- * type is REG_DWORD. *
- * *
- * Example: *
- * *
- * #include "winreg.h" *
- * _sys.scripts.load("REGISTRY.JS"); *
- * reg = new Registry(HKEY_LOCAL_MACHINE, *
- * "SOFTWARE\\Borland\\IntraBuilder\\1.0\\Server"); *
- * if (reg.error == 0) { *
- * oldSessions = reg.queryValue("MaxSessions"); *
- * if (reg.setValue("MaxSessions","15")) { *
- * _sys.scriptOut.writeln("MaxSessions changed"); *
- * reg.setValue("MaxSessions",oldSessions); *
- * } *
- * else { *
- * _sys.scriptOut.writeln("error while changing MaxSessions"); *
- * } *
- * } *
- * *
- * Updated 9/4/96 by IntraBuilder Samples Group *
- * $Revision: 1.1 $ *
- * *
- * Copyright (c) 1996, Borland International, Inc. All rights reserved. *
- * *
- \****************************************************************************/
- // Define Windows data types for use by the extern command
- #include "windef.h"
- #include "winreg.h"
-
- class Registry(openKey, subKey) {
- this.openKey = openKey;
- this.subKey = subKey;
- this.key = 0;
- this.isOpen = false;
- this.error = 0;
- this.newlyCreated = false;
-
- class::prototype();
-
- var nKey = 0, nDisposition = -1, nResult;
- nResult = RegCreateKeyEx(this.openKey, this.subKey, 0, 0, REG_OPTION_NON_VOLATILE,
- KEY_ALL_ACCESS, 0, nKey, nDisposition );
- // store the handle to this key
- this.key = nKey;
- // registry keys should not be held open. If we got a key open
- // close it for now.
- if (nResult == ERROR_SUCCESS) {
- if (nDisposition == REG_CREATED_NEW_KEY) {
- this.newlyCreated = true;
- }
- this.close();
- }
- else {
- this.error = nResult;
- }
-
- function close()
- {
- this.isOpen = false;
- return RegCloseKey( this.key );
- }
-
- function open()
- {
- var nResult, nReturn;
- nReturn = 0; // handle of new key
- nResult = RegOpenKeyEx(this.openKey, this.subKey, 0,
- KEY_ALL_ACCESS, nReturn);
- if (nResult == ERROR_SUCCESS) {
- this.key = nReturn;
- this.isOpen = true;
- }
- else {
- this.error = (nResult);
- }
- return (nReturn);
- }
-
- function prototype()
- {
-
- extern LONG RegCloseKey( HKEY ) ADVAPI32;
- extern LONG RegCreateKeyEx( HKEY, LPCTSTR, DWORD, LPTSTR, DWORD,
- REGSAM, LPSTRUCTURE, PHKEY, LPDWORD ) ADVAPI32
- from "RegCreateKeyExA";
- extern LONG RegDeleteKey( HKEY, LPCTSTR ) ADVAPI32
- from "RegDeleteKeyA";
- extern LONG RegDeleteValue( HKEY, LPTSTR ) ADVAPI32
- from "RegDeleteValueA";
- extern LONG RegFlushKey( HKEY ) ADVAPI32;
- extern LONG RegOpenKeyEx( HKEY, LPCTSTR, DWORD, REGSAM, PHKEY )
- ADVAPI32 from "RegOpenKeyExA";
- extern LONG RegQueryValueEx( HKEY, LPTSTR, DWORD, LPDWORD, void *,
- LPDWORD ) ADVAPI32 from "RegQueryValueExA";
- extern LONG RegSetValueEx( HKEY, LPCTSTR, DWORD, DWORD, void *, DWORD )
- ADVAPI32 from "RegSetValueExA";
-
- }
-
- function queryKeyName() {
- var keyName="";
- switch (this.openKey) {
- case HKEY_CLASSES_ROOT:
- keyName = "HKEY_CLASSES_ROOT\\";
- break;
- case HKEY_CURRENT_USER:
- keyName = "HKEY_CURRENT_USER\\";
- break;
- case HKEY_LOCAL_MACHINE:
- keyName = "HKEY_LOCAL_MACHINE\\";
- break;
- case HKEY_USERS:
- keyName = "HKEY_USERS\\";
- break;
- case HKEY_PERFORMANCE_DATA:
- keyName = "HKEY_PERFORMANCE_DATA\\";
- break;
- case HKEY_CURRENT_CONFIG:
- keyName = "HKEY_CURRENT_CONFIG\\";
- break;
- case HKEY_DYN_DATA:
- keyName = "HKEY_DYN_DATA\\";
- break;
- default:
- keyName = "UNKNOWN_KEY\\";
- }
- return (keyName + this.subKey);
- }
-
- function queryValue(keyName) {
- var nResult = 0,
- nType = 0,
- nLen = 80,
- keyValue = false;
-
- var strEx = new StringEx();
- var cData = strEx.replicate(" ", 80);
-
- // reset the error property
- this.error = 0;
-
- // open up the key
- this.open();
-
- if (this.isOpen) {
- // query the value
- nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
-
- // ERROR_MORE_DATA means we need to pass a larger cData
- if (nResult == ERROR_MORE_DATA) {
- cData = strEx.replicate(" ", nLen);
- nResult = RegQueryValueEx(this.key, keyName, 0, nType, cData, nLen);
- }
-
- strEx.string = cData;
- if (nResult == ERROR_SUCCESS) {
- if (nType == REG_DWORD)
- keyValue = strEx.asc(strEx.substring(0, 1)) * Math.pow(256,0) +
- strEx.asc(strEx.substring(1, 2)) * Math.pow(256,1) +
- strEx.asc(strEx.substring(2, 3)) * Math.pow(256,2) +
- strEx.asc(strEx.substring(3, 4)) * Math.pow(256,3);
- else
- keyValue = strEx.substring(0,nLen-1);
- }
- else
- this.error = nResult;
- }
- return (keyValue);
- }
-
- function setValue( valueName, value, type ) {
-
- var bReturn = false;
- var strEx = new StringEx();
- var nType = (setValue.arguments.length == 3) ? type : REG_SZ;
- var xValue = value;
-
- // reset the error property
- this.error = 0;
-
- // open the key
- this.open();
-
- if (this.isOpen) {
- // reformat data if necessary
- if (nType == REG_DWORD) {
- xValue = parseInt(value);
- xValue = strEx.chr(parseInt(xValue/Math.pow(256,0)) % 256) +
- strEx.chr(parseInt(xValue/Math.pow(256,1)) % 256) +
- strEx.chr(parseInt(xValue/Math.pow(256,2)) % 256) +
- strEx.chr(parseInt(xValue/Math.pow(256,3)) % 256);
- }
- else {
- xValue = value + "";
- if (xValue.indexOf(strEx.chr(0)) >= 0)
- xValue = xValue.substring(0,xValue.indexOf(strEx.chr(0)+1));
- else
- xValue = xValue + strEx.chr(0);
- }
-
- // Write the data to the registry
- nLen = xValue.length;
- nResult = RegSetValueEx(this.key, valueName, 0, nType, xValue, nLen);
- if (nResult == ERROR_SUCCESS)
- lReturn = true;
- else
- this.error = (lnResult);
- }
- return (lReturn);
- }
- }