home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
borland
/
ib
/
setups
/
intrabld
/
data.z
/
REGISTRY.JS
< prev
next >
Wrap
Text File
|
1996-12-11
|
15KB
|
401 lines
/****************************************************************************\
* *
* 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: *
* *
* deleteValue([<name>]) - Delete the named value from the current key. If *
* no name is passed, then the default value for *
* this key is deleted. *
* *
* enumValue() - Returns an array containing the names of each value *
* contained in the current key. *
* *
* 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 10/23/96 by IntraBuilder Samples Group *
* $Revision: 1.2 $ *
* *
* Copyright (c) 1996, Borland International, Inc. All rights reserved. *
* *
\****************************************************************************/
//
// These next two lines are used for debugging purposes. To trace the
// results of the API calls, uncomment the #define DEBUG line. The
// results are written to the ScriptPad window.
//
//#define DEBUG
#define so(x) _sys.scriptOut.writeln(x)
// 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 );
#ifdef DEBUG
so("constructor - " + nResult);
so("disposition - " + nDisposition);
#endif
// 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;
}
#ifdef DEBUG
so("created - " + this.newlyCreated);
#endif
this.close();
}
else {
this.error = nResult;
}
function close()
{
this.isOpen = false;
var nResult = RegCloseKey( this.key );
#ifdef DEBUG
so("close - " + nResult);
#endif
return (nResult);
}
function deleteValue(keyName)
{
var bReturn = false;
// reset the error property
this.error = 0;
// open up the key
this.open();
if (this.isOpen) {
bReturn = true;
// if one parameter is passed, delete that value
if (deleteValue.arguments.length == 1) {
var nResult = RegDeleteValue(this.key, keyName);
}
// if no parameter is passed, delete default value
else {
var nResult = RegDeleteValue(this.key, "");
}
#ifdef DEBUG
so("delete - " + nResult);
#endif
this.close();
}
return (bReturn);
}
function enumValue()
{
var aReturn = new Array(),
nResult = ERROR_SUCCESS,
nCount = 0,
sValue = "",
nLen = 0,
string80 = new StringEx().replicate(" ", 80);
// reset the error property
this.error = 0;
// open up the key
this.open();
if (this.isOpen) {
while (nResult == ERROR_SUCCESS) {
sValue = string80;
nLen = sValue.length;
nResult = RegEnumValue(this.key, nCount, sValue, nLen, 0, 0, 0, 0);
#ifdef DEBUG
so("enum - " + nCount + " - " + nResult);
#endif
if (nResult == ERROR_SUCCESS)
aReturn.add(sValue.substring(0,nLen));
else if (nResult != ERROR_NO_MORE_ITEMS)
this.error = nResult;
nCount ++
}
this.close();
}
return (aReturn);
}
function open()
{
var nResult, nReturn;
nReturn = 0; // handle of new key
nResult = RegOpenKeyEx(this.openKey, this.subKey, 0,
KEY_ALL_ACCESS, nReturn);
#ifdef DEBUG
so("open - " + nResult);
#endif
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 RegDeleteValue( HKEY, LPTSTR ) ADVAPI32
from "RegDeleteValueA";
extern LONG RegEnumValue( HKEY, DWORD, LPTSTR, LPDWORD, DWORD,
LPDWORD, LPBYTE, LPDWORD) ADVAPI32 from "RegEnumValueA";
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";
#ifdef __asian__
extern LONG RegQueryValueExChar( HKEY, LPTSTR, DWORD, LPDWORD, char*,
LPDWORD ) ADVAPI32 from "RegQueryValueExA";
extern LONG RegSetValueExChar( HKEY, LPCTSTR, DWORD, DWORD, char*, DWORD )
ADVAPI32 from "RegSetValueExA";
#endif
}
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);
#ifdef DEBUG
so("query - " + nResult);
#endif
// 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);
#ifdef DEBUG
so("requery - " + nResult);
#endif
}
#ifdef __asian__
// The Asian version uses Unicode strings. Call RegQueryValueExChar, which
// is prototyped to char*, which converts the string to multi-byte.
if (nResult == ERROR_SUCCESS && nType == REG_SZ) {
cData = strEx.replicate(" ", nLen);
nResult = RegQueryValueExChar(this.key, keyName, 0, nType, cData, nLen);
#ifdef DEBUG
so("UNICODE requery - " + nResult);
#endif
}
#endif
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;
this.close();
}
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;
#ifdef __asian__
if (nType == REG_SZ) {
nResult = RegSetValueExChar(this.key, valueName, 0, nType, xValue, nLen);
}
else {
#endif
nResult = RegSetValueEx(this.key, valueName, 0, nType, xValue, nLen);
#ifdef __asian__
}
#endif
#ifdef DEBUG
so("setvalue - " + nResult);
#endif
if (nResult == ERROR_SUCCESS)
lReturn = true;
else
this.error = (lnResult);
this.close();
}
return (lReturn);
}
}