| Parse Registry SubKeys in JScript? | 
| Author: | Message: | 
| Jedimark Full Member
 
    
 
 Posts: 140
 Reputation: 6
 Joined: Apr 2002
 
 | | O.P.  Parse Registry SubKeys in JScript? Does anyone know how to do the equivalent of this C# code in JScript? 
Cheers, 
- Mark
 code:try
 {
 RegistryKey rootPath = Registry.CurrentUser.OpenSubKey("Software\\Item1\\Item2", true);
 foreach(string newKey in rootPath.GetSubKeyNames())
 {
 // other stuff here
 }
 }
 
 | 
 | 
| 08-24-2006 05:23 PM |  | 
|  | 
| J-Thread Full Member
 
    
 
  
 Posts: 467
 Reputation: 8
 – /
  / – Joined: Jul 2004
 
 | | RE: Parse Registry SubKeys in JScript? I think we had a thread about this a few weeks ago, but I can't find it anymore...
 I think the solution was to use a win32 function for listing registry subkeys, because it seems like it cannot be done easily with the scripting engine.
 
 Can somebody else find the thread / remember the solution?
 | 
 | 
| 08-24-2006 07:52 PM |  | 
|  | 
| Ezra Veteran Member
 
      
 
  Forgiveness is between them and God
 
 Posts: 1960
 Reputation: 31
 38 /
  /  Joined: Mar 2003
 
 | | RE: Parse Registry SubKeys in JScript? quote:Originally posted by Jedimark
 Does anyone know how to do the equivalent of this C# code in JScript?
 
 Cheers,
 - Mark
 
 
 code:try
 {
 RegistryKey rootPath = Registry.CurrentUser.OpenSubKey("Software\\Item1\\Item2", true);
 foreach(string newKey in rootPath.GetSubKeyNames())
 {
 // other stuff here
 }
 }
 
 
 
Why not make a nice dll that returns an array with keys? | 
 | 
| 08-24-2006 10:35 PM |  | 
|  | 
| Shondoit Full Member
 
    
 
  Hmm, Just Me...
 
 Posts: 227
 Reputation: 15
 37 /
  /  Joined: Jul 2006
 
 | | RE: Parse Registry SubKeys in JScript? No need for a DLL, it can be done without it 
I have the code, but I'll have to look for it though 
post them asap
 
-edit1- I found the code, but it is script specific, I will make it variable now 
-edit2- Changed the code, gonna test it now...
 
-edit3- Here is the final code. The first function enumerates all available subkey names, the second function enumerates all key value names, could be used to look wich programs run on windows startup ("HKLM\Software\Microsoft\Windows\CurrentVersion\Run", if you'd want to know, user specific startup is located in HKCU instead of HKLM)
 code: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();
 }
 }
 
 
Then you can use it like this...
 code:var SubKeyArray = EnumSubKeys("HKEY_CURRENT_USER\\Software\\Item1\\Item2");
 for (Index in SubKeyArray) {
 Debug.Trace(SubKeyArray[Index])
 }
 
 
Or enumerate all programs running on startup...
 code:var Shell = new ActiveXObject("WScript.Shell")
 var Key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
 var ValuesArray = EnumValues(Key);
 Debug.Trace("=== Global startup ===")
 for (Index in ValuesArray) {
 var ValueName = ValuesArray[Index]
 ValueValue = Shell.RegRead(Key + "\\" + ValueName)
 Debug.Trace(ValueName + " = " + ValueValue)
 }
 var Key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
 var ValuesArray = EnumValues(Key);
 Debug.Trace("=== User startup ===")
 for (Index in ValuesArray) {
 var ValueName = ValuesArray[Index]
 ValueValue = Shell.RegRead(Key + "\\" + ValueName)
 Debug.Trace(ValueName + " = " + ValueValue)
 }
 
 
This post was edited on 08-24-2006 at 11:43 PM by Shondoit.
 | 
 | 
| 08-24-2006 10:54 PM |  | 
|  | 
| CookieRevised Elite Member
 
      
 
  
 Posts: 15494
 Reputation: 173
 – /
  /  Joined: Jul 2003
 Status: Away
 
 | | RE: Parse Registry SubKeys in JScript? new ActiveXObject("WbemScripting.SWbemLocator") ???? 
Locator.ConnectServer(null, "root\\default") ???? 
Registry.Methods_.Item("EnumValues") ???? 
etc...
 
I'm sorry but all this stuff is really not needed at all.
 
J-Thread is absolutely correct, 'simply' use the available Windows registry API's to do the job. They are all you need (although the method you showed has some "je-ne-sais-qua" too; still I really don't like the detour which activex takes (afterall, it uses those very same apis)).
Speaking of which, I have seen many registry accessing codes, and they are almost always exactly the same (with the same 'mistakes' or limitations), although they 'work', I haven't seen many totally fool proof and correct 100% codes (eg: first checking the length a registry key is, instead of assuming the key wnt be longer than x bytes, before retrieving it) 
IIRC, Matty already posted an almost complete (but with some limitations) registry accessing script on the forums... 
EDIT: I was wrong(partially) what Matty used was an external DLL => ActiveX Registry Access for ScriptsThis post was edited on 08-25-2006 at 12:09 AM by CookieRevised.
 .-= A 'frrrrrrrituurrr' for Wacky =-. | 
 | 
| 08-25-2006 12:00 AM |  | 
|  | 
| Shondoit Full Member
 
    
 
  Hmm, Just Me...
 
 Posts: 227
 Reputation: 15
 37 /
  /  Joined: Jul 2006
 
 | | RE: Parse Registry SubKeys in JScript? What do you mean "not needed at all", it does the job doesn't it?He got his answer. It doesn't have to be from the API...
 
 Personally, I always forget to 'Free' the Dll's I used, and a lot of scripts I looked at forget it too...
 This way is a much cleaner way, because you don't have to use anything external, so it saves from using DataBloc structures (Interop.Allocate)
 
 And by the way, these methods use an official Registry control object (StdRegProv)
 | 
 | 
| 08-25-2006 12:09 AM |  | 
|  | 
| CookieRevised Elite Member
 
      
 
  
 Posts: 15494
 Reputation: 173
 – /
  /  Joined: Jul 2003
 Status: Away
 
 | | RE: RE: Parse Registry SubKeys in JScript? quote:Originally posted by Shondoit
 What do you mean "not needed at all", it does the job doesn't it?
 
 With a detour, yes... quote:Originally posted by Shondoit
 He got his answer. It doesn't have to be from the API...
 
 You'll learn more from using APIs (if APIs can be used) than anything else. APIs are the stuff under the hood and do not have the limitations which activex objects like that have. quote:Originally posted by Shondoit
 Personally, I always forget to 'Free' the Dll's I used, and a lot of scripts I looked at forget it too...
 
 You don't need to free system DLLs (as a matter of fact, freeing them will actually not free them at all) (and as another matter of fact, you probably use more memory when you use that activex than when you use the APIs; moreover in Plus! scripting, once you loaded an activex object it stays loaded; eg: try to delete an activex dll after you've loaded it in your script, you wont be able to do it) quote:Originally posted by Shondoit
 This way is a much cleaner way, because you don't have to use anything external, so it saves from using DataBloc structures (Interop.Allocate)
 
 I don't find that cleaner at all, matter of opinion probably... quote:Originally posted by Shondoit
 And by the way, these methods use an official Registry control object (StdRegProv)
 
 Which has probably some restrictions too (like every activex) and which on its term uses the APIs anyways. So why use a detour when you can use the stuff you need directly, without any restrictions...
 
-------------
 
EDIT quote:thread reported to be moved from private beta forum to public scripting forum... my bad, sorryOriginally posted by Shondoit
 (btw: I don't have access to the thread you showed... or something like that)
 
This post was edited on 08-25-2006 at 12:20 AM by CookieRevised.
 .-= A 'frrrrrrrituurrr' for Wacky =-. | 
 | 
| 08-25-2006 12:14 AM |  | 
|  | 
| Shondoit Full Member
 
    
 
  Hmm, Just Me...
 
 Posts: 227
 Reputation: 15
 37 /
  /  Joined: Jul 2006
 
 | | RE: Parse Registry SubKeys in JScript? Matter of opinion indeed...
 I still think, using a class specificaly designed for Reg access is much easier, Most people don't create Windows with API calls either
 
 (btw: I don't have access to the thread you showed... or something like that)
 | 
 | 
| 08-25-2006 12:17 AM |  | 
|  | 
| matty Scripting Guru
 
      
 
 Posts: 8327
 Reputation: 109
 40 /
  /  Joined: Dec 2002
 Status: Away
 
 | | RE: Parse Registry SubKeys in JScript? quote:Originally posted by CookieRevised
 IIRC, Matty already posted an almost complete (but with some limitations) registry accessing script on the forums...
 EDIT: I was wrong(partially) what Matty used was an external DLL => ActiveX Registry Access for Scripts
 
 
I should slap you soooooooooo hard did you forget this module?
 
Next thing to add is the SetBinaryValue 
GetBinaryValue doesn't work unless the value going to be returned is a string (need to figure this out). 
Combine all GetValues and SetValues into one.
 
Then anything else that could be thought of really.
  Attachment: registry.zip (2.03 KB) This file has been downloaded 481 time(s).
 
 | 
 | 
| 08-25-2006 01:52 AM |  | 
|  | 
| -dt- Scripting Contest Winner
 
      
 
  ;o
 
 Posts: 1818
 Reputation: 74
 37 /
  /  Joined: Mar 2004
 
 |  | 
| 08-25-2006 04:15 AM |  | 
|  | 
| Pages: (2): 
« First
  
 [ 1 ]
 2
 
»
 
Last » | 
|  |