I have searched for a good solution to enum the registries with JScript for a long time. Today I got it. The original url is http://www.msghelp.net/showthread.php?tid=65331&page=1 .
The two functions are as below:
function EnumSubKeys(RegKey) {
var RootKey = new Object()
RootKey["HKCR"] = RootKey["HKEY_CLASSES_ROOT"] = 0x80000000;
RootKey["HKCU"] = RootKey["HKEY_CURRENT_USER"] = 0x80000001;
RootKey["HKLM"] = RootKey["HKEY_LOCAL_MACHINE"] = 0x80000002;
RootKey["HKUS"] = RootKey["HKEY_USERS"] = 0x80000003;
RootKey["HKCC"] = RootKey["HKEY_CURRENT_CONFIG"] = 0x80000005;
var RootVal = RootKey[RegKey.substr(0, RegKey.indexOf("\\"))]
if (RootVal != undefined) {
Locator = new ActiveXObject("WbemScripting.SWbemLocator");
ServerConn = Locator.ConnectServer(null, "root\\default");
Registry = ServerConn.Get("StdRegProv");
Method = Registry.Methods_.Item("EnumKey");
p_In = Method.InParameters.SpawnInstance_();
p_In.hDefKey = RootVal;
p_In.sSubKeyName = RegKey.substr(RegKey.indexOf("\\") + 1)
p_Out = Registry.ExecMethod_(Method.Name, p_In);
return p_Out.sNames.toArray();
}
}
function EnumValues(RegKey) {
var RootKey = new Object()
RootKey["HKCR"] = RootKey["HKEY_CLASSES_ROOT"] = 0x80000000;
RootKey["HKCU"] = RootKey["HKEY_CURRENT_USER"] = 0x80000001;
RootKey["HKLM"] = RootKey["HKEY_LOCAL_MACHINE"] = 0x80000002;
RootKey["HKUS"] = RootKey["HKEY_USERS"] = 0x80000003;
RootKey["HKCC"] = RootKey["HKEY_CURRENT_CONFIG"] = 0x80000005;
var RootVal = RootKey[RegKey.substr(0, RegKey.indexOf("\\"))]
if (RootVal != undefined) {
Locator = new ActiveXObject("WbemScripting.SWbemLocator");
ServerConn = Locator.ConnectServer(null, "root\\default");
Registry = ServerConn.Get("StdRegProv");
Method = Registry.Methods_.Item("EnumValues");
p_In = Method.InParameters.SpawnInstance_();
p_In.hDefKey = RootVal;
p_In.sSubKeyName = RegKey.substr(RegKey.indexOf("\\") + 1)
p_Out = Registry.ExecMethod_(Method.Name, p_In);
return p_Out.sNames.toArray();
}
}
The test code is following
var Shell = new ActiveXObject("WScript.Shell")
var Key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
var ValuesArray = EnumValues(Key);
WScript.StdOut.WriteLine("=== Global startup ===")
for (Index in ValuesArray) {
var ValueName = ValuesArray[Index]
ValueValue = Shell.RegRead(Key + "\\" + ValueName)
WScript.StdOut.WriteLine(ValueName + " = " + ValueValue)
}
var Key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
var ValuesArray = EnumSubKeys(Key);
WScript.StdOut.WriteLine("=== Uninstall startup ===")
for (Index in ValuesArray) {
var ValueName = ValuesArray[Index]
WScript.StdOut.WriteLine(ValueName);
}
没有评论:
发表评论