[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

RegOpenKeyEx in VBScript?

Posted on 2001-08-13
10
Medium Priority
?
2,825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 33

Expert Comment

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

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6380553
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
ID: 6380559
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

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

hongjun
0
 
LVL 7

Expert Comment

by:John844
ID: 6380707
thanks for the recognition hongjun
0
 
LVL 7

Expert Comment

by:John844
ID: 6380771
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
ID: 6380774
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 200 total points
ID: 6381069
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
ID: 6381463
Thanks for your help!
0
 

Author Comment

by:moloko
ID: 6384839
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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 informatio…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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