/*
* -----
* Screenshot Sender - registry.js
* -----
* Provides Screenshot Sender registry access
* -----
*/
// todo
// add functions to create 'hidden' registry functions
// Registry value types
var REG_NONE = 0;
var REG_SZ = 1;
var REG_EXPAND_SZ = 2;
var REG_BINARY = 3;
var REG_DWORD = 4;
var REG_MULTI_SZ = 7;
// Registry key locations
var HKEY_CLASSES_ROOT = 0x80000000;
var HKEY_CURRENT_USER = 0x80000001;
var HKCU = HKEY_CURRENT_USER;
var HKEY_LOCAL_MACHINE = 0x80000002;
var HKEY_USERS = 0x80000003;
var HKEY_PERFORMANCE_DATA = 0x80000004;
var HKEY_PERFORMANCE_TEXT = 0x80000050;
var HKEY_PERFORMANCE_NLSTEXT = 0x80000060;
var HKEY_CURRENT_CONFIG = 0x80000005;
var HKEY_DYN_DATA = 0x80000006;
// Registry key constants
var KEY_QUERY_VALUE = 0x1;
var KEY_SET_VALUE = 0x2;
var KEY_CREATE_SUB_KEY = 0x4;
var KEY_ENUMERATE_SUB_KEYS = 0x8;
var KEY_READ = 0x20019;
var KEY_WRITE = 0x20006;
var KEY_EXECUTE = 0x20019;
var KEY_ALL_ACCESS = 0xf003f;
var REG_OPTION_NON_VOLATILE = 0;
var ERROR_SUCCESS = 0;
var ERROR_NO_MORE_ITEMS = 259;
var KEY_DELETE = 0x00010000;
var REG_MAX_NAME_LENGTH = 0x200;
function Registry_OpenKey(HKEY, lpSubKey, samDesired) {
if (typeof samDesired === 'undefined') samDesired = KEY_ALL_ACCESS;
var h = Interop.Allocate(4);
Interop.Call('advapi32', 'RegOpenKeyExW', HKEY, lpSubKey, 0, samDesired, h);
return h.ReadDWORD(0);
}
/*
Name: Registry_CreateKey
Purpose: Creates a key in the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location to create. Last key in the path should be the only one that doesn't exist. Function
is not recursive.
Return: True if key was created properly/False if it failed
*/
function Registry_CreateKey(HKEY, lpSubKey) {
var h = Interop.Allocate(4);
var r = Interop.Call('advapi32', 'RegCreateKeyW', HKEY, lpSubKey, h) === ERROR_SUCCESS;
Registry_CloseKey(h.ReadDWORD(0));
return r;
}
/*
Name: Registry_DeleteKey
Purpose: Deletes a key in the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Key to delete. Must not have any subkeys or values
Return: True if key was deleted properly/False if it failed
*/
function Registry_DeleteKey(HKEY, lpSubKey) {
return Interop.Call('advapi32', 'RegDeleteKeyW', HKEY, lpSubKey) === ERROR_SUCCESS;
}
/*
Name: Registry_DeleteKeyValue
Purpose: Deletes a value in the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location where the key to be deleted resides
sKeyName - Key to be deleted
Return: True if key was deleted properly/False if it failed
*/
function Registry_DeleteKeyValue(HKEY, lpSubKey, lpValueName) {
var h = Registry_OpenKey(HKEY, lpSubKey, KEY_SET_VALUE);
var r = Interop.Call('advapi32', 'RegDeleteValueW', h, lpValueName) === ERROR_SUCCESS;
Registry_CloseKey(h);
return r;
}
/*
Name: Registry_KeyExist
Purpose: Checks to see if a key exists in the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location to check
Return: True if the key exists/False if it doesn't
*/
function Registry_KeyExist(HKEY, lpSubKey) {
var h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ);
Registry_CloseKey(h);
return !(h === ERROR_SUCCESS);
}
/*
Name: Registry_KeyValueExist
Purpose: Checks to see if a value exists in the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location where the key should reside
sKeyName - Key that is being checked to see if it exists
Return: True if the value exists/False if it doesn't
*/
function Registry_KeyValueExist(HKEY, lpSubKey, lpValueName) {
var h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ);
var r = Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, 0, 0, 0) === ERROR_SUCCESS;
Registry_CloseKey(h);
return r;
}
/*
Name: Registry_EnumSubkeys
Purpose: Retrieves a all of the subkeys of a specific key from the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location where to retrieve the subkeys from
sOut - Object to return the subkeys to
Return: Array of subkeys / False if it failed
*/
function Registry_EnumSubkeys(HKEY, lpSubKey, sOut) {
var h;
var lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
var lpcName = Interop.Allocate(4);
lpcName.WriteDWORD(0, 255);
var lpftLastWriteTime = Interop.Allocate(8);
var lIndex = 0;
if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
while (Interop.Call('advapi32', 'RegEnumKeyExW', h, lIndex, lpName, lpcName, 0, 0, 0, 0) !== ERROR_NO_MORE_ITEMS) {
sOut[lpName.ReadString(0)] = '';
++lIndex;
lpcName.WriteDWORD(0, 255);
lpName.Size = 0;
lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
}
Registry_CloseKey(h);
return sOut;
} else { return false; }
}
/*
Name: Registry_EnumKeys
Purpose: Retrieves a all of the subkeys of a specific key from the registry
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location where to retrieve the keys from
sOut - Object which to store keys and values
Return: Object containing keys and their values / False if it failed
*/
function Registry_EnumKeys(HKEY, lpSubKey, sOut) {
var h;
if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
var lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
var lpcName = Interop.Allocate(4);
lpcName.WriteDWORD(0, 255);
var lpType = Interop.Allocate(4);
var lIndex = 0;
while (Interop.Call('advapi32', 'RegEnumValueW', h, lIndex, lpName, lpcName, 0, lpType, 0, 0) !== ERROR_NO_MORE_ITEMS) {
sOut[lpName.ReadString(0)] = Registry_GetKeyValue(HKEY, lpSubKey, lpName.ReadString(0));
++lIndex;
lpcName.WriteDWORD(0, 255);
lpName.Size = 0;
lpName = Interop.Allocate(REG_MAX_NAME_LENGTH);
}
Registry_CloseKey(h);
return sOut;
} else { return false; }
}
/*
Name: Registry_GetKeyValue
Purpose: Get the type of key to generically call the function (useful if you don't remember the type)
Parameters: lKeyLocation - Hive to look in for the sKey(ex. HKEY_CURRENT_USER)
sKey - Location where the key should reside
sKeyName - Key to retrieve the value of
Return: Value of the registry key/False if it failed
*/
function Registry_GetKeyValue(HKEY, lpSubKey, lpValueName) {
var h;
if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_READ)) === ERROR_SUCCESS)) {
var lBufferSize = Interop.Allocate(4);
lBufferSize.WriteDWORD(0, 512);
var lpType = Interop.Allocate(4);
if (Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, 0, lBufferSize) === ERROR_SUCCESS) {
switch (lpType.ReadDWORD(0)) {
case REG_EXPAND_SZ :
case REG_MULTI_SZ :
case REG_SZ :
case REG_BINARY :
var Buffer = Interop.Allocate(2 * lBufferSize.ReadDWORD(0) + 2);
Interop.Call('advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, Buffer, lBufferSize);
Buffer = Buffer.ReadString(0);
break;
case REG_DWORD:
var Buffer = Interop.Allocate(Math.max(4, lBufferSize.ReadDWORD(0)));
Interop.Call('Advapi32', 'RegQueryValueExW', h, lpValueName, 0, lpType, Buffer, lBufferSize);
Buffer = Buffer.ReadDWORD(0);
break;
}
Registry_CloseKey(h);
return Buffer;
} else { return false; }
} else { return false; }
}
/*
Name: Registry_SetKeyValue
Purpose: Set the value of key(useful if you don't remember the type)
Parameters: lKeyLocation - Hive to save sKey in(ex. HKEY_CURRENT_USER)
sKey - Location where the key will reside
sKeyName - Key to create
sKeyValue - Value of the key to be set
lpType - Key type(ex. REG_DWORD)
Return: True if succeeded /False if it failed
*/
function Registry_SetKeyValue(HKEY, lpSubKey, lpKeyName, lpKeyValue, lpType) {
var h, r;
if (!((h = Registry_OpenKey(HKEY, lpSubKey, KEY_WRITE)) === ERROR_SUCCESS)) {
switch (lpType) {
case REG_EXPAND_SZ :
case REG_MULTI_SZ :
case REG_SZ :
case REG_BINARY :
lpKeyValue = String(lpKeyValue);
var lBufferSize = (2 * lpKeyValue.length + 2);
r = Interop.Call('advapi32', 'RegSetValueExW', h, lpKeyName, 0, lpType, lpKeyValue, lBufferSize) === ERROR_SUCCESS;
break;
case REG_DWORD :
var lKeyValue = Interop.Allocate(4);
lKeyValue.WriteDWORD(0, (lpKeyValue & 0xFFFFFFFF));
r = Interop.Call('advapi32', 'RegSetValueExW', h, lpKeyName, 0, lpType, lKeyValue, 4) === ERROR_SUCCESS;
break;
}
Registry_CloseKey(h);
return r;
} else { return false; }
}
function Registry_DeleteTree(HKEY, lpSubKey) {
if (Interop.Call('kernel32', 'GetVersion') % 256 === 6) {
return Registry_DeleteTree_Vista(HKEY, lpSubKey);
} else {
Registry_DeleteTree_Xp(HKEY, lpSubKey, true);
return Registry_KeyExist(HKEY, lpSubKey);
}
}
function Registry_DeleteTree_Vista(HKEY, lpSubKey) {
return Interop.Call('advapi32', 'RegDeleteTreeW', HKEY, lpSubKey) === ERROR_SUCCESS;
}
function Registry_DeleteTree_Xp(HKEY, lpSubKey, bIsParent) {
var o = {};
Registry_EnumKeys(HKEY, lpSubKey, o);
for (var i in o) {
Registry_DeleteKeyValue(HKEY, lpSubKey, i);
}
o = {};
Registry_EnumSubkeys(HKEY, lpSubKey, o);
for (var i in o) {
Registry_DeleteTree_Xp(HKEY, lpSubKey+'\\'+i);
}
Registry_DeleteKey(HKEY, lpSubKey) === ERROR_SUCCESS;
}
/*
Name: Registry_CloseKey
Purpose: Closes the open handle to the registry
Parameters: hKey - handle to the open registry key
Return: True if it closed / False if it failed
*/
function Registry_CloseKey(HKEY) {
return Interop.Call('advapi32', 'RegCloseKey', HKEY) === ERROR_SUCCESS;
}