Solved

RegOpenKeyEx in VBScript?

Posted on 2001-08-13
10
2,669 Views
Last Modified: 2012-05-04
I would like to know how to access the Registry thru VBScript, something equivalent to RegOpenKeyEx.
I have a VBScript function within an ASP page.

Thanks for your help.
0
Comment
Question by:moloko
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
You got to use a component called RegEx from
http://www.stonebroom.com/swindex.htm

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Another method is to create a dll using VB that will do that yourself

'sample usage - Debug.Print RegistryGetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")


'Registry Key Security Options...
Private Const READ_CONTROL = &H20000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Private Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Private Const KEY_EXECUTE = KEY_READ
Private Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                      KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                      KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
'Registry Return Values
Private Const REGISTRY_ERROR_SUCCESS = 0

'Registry Data Types...
Private Const REG_SZ = 1                         ' Unicode nul terminated string
Private Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Private Const REG_DWORD = 4                      ' 32-bit number

Private Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted


Private Type SECURITY_ATTRIBUTES
   nLength As Long
   lpSecurityDescriptor As Long
   bInheritHandle As Boolean
End Type

Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
 ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
 ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
 ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
 ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, _
 ByRef lpdwDisposition As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
 ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long


' Reg Key ROOT Types...
Public Enum HKEYTypes
   HKEY_CLASSES_ROOT = &H80000000
   HKEY_CURRENT_USER = &H80000001
   HKEY_LOCAL_MACHINE = &H80000002
   HKEY_USERS = &H80000003
   HKEY_PERFORMANCE_DATA = &H80000004
End Enum


'-------------------------------------------------------------------------------------------------
'sample usage - Debug.Print RegistryGetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")
'-------------------------------------------------------------------------------------------------
Public Function RegistryGetKeyValue(KeyRoot As HKEYTypes, KeyName As String, SubKeyRef As String) As
String
   Dim i As Long                                           ' Loop Counter
   Dim rc As Long                                          ' Return Code
   Dim hKey As Long                                        ' Handle To An Open Registry Key
   Dim hDepth As Long                                      '
   Dim sKeyVal As String
   Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
   Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
   Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
   
   ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
   '------------------------------------------------------------
   rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_QUERY_VALUE, hKey) ' Open Registry Key
   
   If (rc <> REGISTRY_ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
   
   tmpVal = String$(1024, 0)                             ' Allocate Variable Space
   KeyValSize = 1024                                       ' Mark Variable Size
   
   '------------------------------------------------------------
   ' Retrieve Registry Key Value...
   '------------------------------------------------------------
   rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                        lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
                       
   If (rc <> REGISTRY_ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
     
   tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)

   '------------------------------------------------------------
   ' Determine Key Value Type For Conversion...
   '------------------------------------------------------------
   Select Case lKeyValType                                  ' Search Data Types...
   Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
       sKeyVal = tmpVal                                     ' Copy String Value
   Case REG_DWORD                                          ' Double Word Registry Key Data Type
       For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
           sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
       Next
       sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
   End Select
   
   RegistryGetKeyValue = sKeyVal                                   ' Return Value
   rc = RegCloseKey(hKey)                                  ' Close Registry Key
   Exit Function                                           ' Exit
   
GetKeyError:    ' Cleanup After An Error Has Occured...
   RegistryGetKeyValue = vbNullString                      ' Set Return Val To Empty String
   rc = RegCloseKey(hKey)                                  ' Close Registry Key
End Function


hongjun
0
 
LVL 5

Expert Comment

by:mattyk
Comment Utility
You might also be able to make use of Windows Scrpt Hosting to do this:

' VBScript.
Set Sh = CreateObject("WScript.Shell")
key =  "HKEY_CURRENT_USER\"
Sh.RegWrite key & "WSHTest\", "testkeydefault"
Sh.RegWrite key & "WSHTest\string1", "testkeystring1"
Sh.RegWrite key & "WSHTest\string2", "testkeystring2", "REG_SZ"
Sh.RegWrite key & "WSHTest\string3", "testkeystring3", "REG_EXPAND_SZ"
Sh.RegWrite key & "WSHTest\int", 123, "REG_DWORD"
WScript.Echo Sh.RegRead(key & "WSHTest\")
WScript.Echo Sh.RegRead(key & "WSHTest\string1")
WScript.Echo Sh.RegRead(key & "WSHTest\string2")
WScript.Echo Sh.RegRead(key & "WSHTest\string3")
WScript.Echo Sh.RegRead(key & "WSHTest\int")
Sh.RegDelete key & "WSHTest\"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsconmanipulatingsystemregistryprogrammatically.asp

-matty
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
BTW, the above code is in courtesy of John844 dude. I would prefer using RegEx component.

hongjun
0
 
LVL 7

Expert Comment

by:John844
Comment Utility
thanks for the recognition hongjun
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

by:John844
Comment Utility
as for the code sample, It was something I had working for quite a while.  Another user posted the code to use Wscript and that is what I would use now as well.
0
 
LVL 7

Expert Comment

by:John844
Comment Utility
here is the code using WScript wrapped into simple usable functions.

reading / writing to registry
'PLACE ALL THIS INTO A NEW MODULE or IN Declarations OF YOUR FORM

Public Sub CreateKey(Folder As String, Value As String)

Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
b.RegWrite Folder, Value

End Sub

Public Sub CreateIntegerKey(Folder As String, Value As Integer)

Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
b.RegWrite Folder, Value, "REG_DWORD"


End Sub

Public Function ReadKey(Value As String) As String

'READ FROM WINDOWS REGISTRY
'.........................
Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
r = b.RegRead(Value)
ReadKey = r
End Function


Public Sub DeleteKey(Value As String)
'DELETE VALUES FROM WINDOWS REGISTRY
'-----------------------------------
Dim b As Object
On Error Resume Next
Set b = CreateObject("Wscript.Shell")
b.RegDelete Value
End Sub

'form==============
'To Create an registry key:
CreateKey "HKEY_CURRENT_USER\Software\My Software\Version","1.45"

'To Create an Integer registry key:
CreateIntegerKey "HKEY_CURRENT_USER\Software\My Software\Number","50"

'To Read from the registry
msgbox "Version is " & ReadKey("HKEY_CURRENT_USER\Software\My Software\Version"

'To Delete from the registry
DeleteKey "HKEY_CURRENT_USER\Software\My Software\Version"

' Instead of writing out HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER etc, you can
' use abbreviations such as "HKCU\Software"

0
 
LVL 7

Accepted Solution

by:
John844 earned 50 total points
Comment Utility
sorry, here is the code converted for asp

'To Create an registry key:
CreateKey "HKEY_LOCAL_MACHINE\Software\Test\Version","1.45"

'To Create an Integer registry key:
CreateIntegerKey "HKEY_LOCAL_MACHINE\Software\Test\Number","50"

'To Read from the registry
Response.Write "Version is " & ReadKey("HKEY_LOCAL_MACHINE\Software\Test\Version")

'To Delete from the registry
DeleteKey "HKEY_LOCAL_MACHINE\Software\Test\Version"





Sub CreateKey(ByVal Folder, ByVal Value)
On Error Resume Next
     'CREATE WINDOWS REGISTRY KEY
     '-----------------------------------
     Dim objScript
     Set objScript = server.CreateObject("wscript.shell")
     objScript.RegWrite Folder, Value
     Set objScript = nothing
End Sub

Sub CreateIntegerKey(Folder, Value)
On Error Resume Next
     'CREATE WINDOWS INTEGER REGISTRY KEY
     '-----------------------------------
     Dim objScript
     Set objScript = server.CreateObject("wscript.shell")
     objScript.RegWrite Folder, cint(Value), "REG_DWORD"
     Set objScript = nothing
End Sub

Function ReadKey(ByVal Value)
On Error Resume Next
     'READ FROM WINDOWS REGISTRY
     '.........................
     Dim objScript
     Dim returnValue
     Set objScript = server.CreateObject("wscript.shell")
     returnValue = objScript.RegRead(Value)
     ReadKey = returnValue
     Set objScript = nothing
End Function


Sub DeleteKey(Value)
On Error Resume Next
     'DELETE VALUES FROM WINDOWS REGISTRY
     '-----------------------------------
     Dim objScript
     Set objScript = server.CreateObject("Wscript.Shell")
     objScript.RegDelete Value
     Set objScript = nothing
End Sub
0
 

Author Comment

by:moloko
Comment Utility
Thanks for your help!
0
 

Author Comment

by:moloko
Comment Utility
Hi,

The ReadKey works great but i cannot CreateKey working.
There's no error but not key is written to the registry.
for example i call the following:
CreateIntegerKey("HKEY_LOCAL_MACHINE\Software\Test\Number","50")

In the path ..\test, I should see a key Number w/ a value of 50.

Is there something i am missing?
I will post it as a question later so you can get more points.

thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now