FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Modify a Reg keys





pollux1er
Hi to every body. I need to write something that modify a reg key. In which language would you advise me to do it. Can somebody tell me how to write it with VBscript?
MrBlueSky
To change the registry from VBscript include the following three routines in your script:

Code:

Sub RegistryWrite(KeyName, ValueName, ValueData, ValueType)
  ValueType = UCase(ValueType)
  If ValueType <> "REG_DWORD" and ValueType <> "REG_BINARY" Then _
                                                   ValueType = "REG_SZ"
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ValueData, ValueType
End Sub

Function RegistryRead(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  RegistryRead = WSHShell.RegRead(KeyName & "\" & ValueName)
End Function

Sub RegistryDelete(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ""
  WshShell.RegDelete KeyName & "\" & ValueName
End Sub


Using these three routines, you can accomplish nearly all Registry tasks. To create a Registry key, type this (note that all HKEY... roots must appear in uppercase):

Code:

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "", "", "")


To assign data to a Registry value:

Code:

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "My Value", _
                                                   "Some Data", "")


Leave "My Value" blank to set the (default) value. To read the data stored in a given value:

Code:

Variable = RegistryRead("HKEY_LOCAL_MACHINE\Software\My Key", "My Value")


Leave "My Value" blank to read the (default) value. To delete a key:

Code:

Call RegistryDelete("HKEY_LOCAL_MACHINE\Software\My Key", "")


To delete a value:

Code:

Call RegistryDelete("HKEY_LOCAL_MACHINE\Software\My Key", "My Value")


To delete the (default) value in a key, we just set the value to nothing:

Code:

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "", "", "")


This code comes from the book "Windows XP Annoyances for Geeks" by D. A. Karp. I haven't tested it.
pollux1er
Thank so much for the script. But I still have a preoccupation. I don't really know how to insert it into a program. One thing I know is that after writing it, I should just save it as *.vbs file. Isn't it?

Can you please post the structure of the complete file.vbs?
pollux1er
This is the script I finally wrote. But unfortunatelly, it does not work.

Code:

Sub RegistryWrite(KeyName, ValueName, ValueData, ValueType)
  ValueType = UCase(ValueType)
  If ValueType <> "REG_DWORD" and ValueType <> "REG_BINARY" Then _
                                                   ValueType = "REG_SZ"
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ValueData, ValueType
End Sub

Function RegistryRead(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  RegistryRead = WSHShell.RegRead(KeyName & "\" & ValueName)
End Function

Sub RegistryDelete(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ""
  WshShell.RegDelete KeyName & "\" & ValueName
End Sub

' to dellete the key

Call RegistryDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SystemCertificates\SPC\Certificates", "")


I saved it as script.vbs. When i tried to run it, i get an error message with code 80070005.

I can just think that the error is coming from the code. The last function on line 17 which is

WshShell.RegDelete KeyName & "\" & ValueName

What do you think is the matter in that code?[/code]
pollux1er
MrBlueSky
pollux1er wrote:
I now know what is the problem. That script is working only when the key has no subkeys. This is my case.
It works for another key without no subkey.



Yes, it only deletes keys without subkey.

Quote:

Please have a look at it and help me if you can.


I'm afraid I can't help you much, because I don't run windows myself, so I can not test anything. But do you get an error message? If so, what is the message?

--edit--

By the way, I think the second line must be

Code:

Const HKEY_LOCAL_MACHINE = &H80000002


Because &H80000001 is HKEY_CURRENT_USER, and the value of HKEY_LOCAL_MACHINE is &H80000002.
pollux1er
MrBlueSky wrote:


Code:

Const HKEY_LOCAL_MACHINE = &H80000002


Because &H80000001 is HKEY_CURRENT_USER, and the value of HKEY_LOCAL_MACHINE is &H80000002.


You are right!!! It works!!! Thank you so much. In fact, with the &H80000001 nothing was happening and no mistake was declared.

When I changed it it works.

Thanks again.
Related topics
Windows Tips&tricks!
Windows Vista Official Thread
Win xp or win 98 ( donnow about latest version)
Transferring Applications
Just-In-Time Debugger / Won't stop bugging me!
Removing Unread Mail
Problem Uninstalling
8 Ghostly registry deletes every time I boot my Portable
Windows logs off on logon
Which are the best free disk and reg cleaners?
regedit files?
* Remap Your KeyBoard Keys *
Hidden Folders don't appear even if checked the box? See so
Knowing Regedit.exe & regedt32.exe
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.