moloko
asked on
RegOpenKeyEx in VBScript?
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.
I have a VBScript function within an ASP page.
Thanks for your help.
Another method is to create a dll using VB that will do that yourself
'sample usage - Debug.Print RegistryGetKeyValue(HKEY_C LASSES_ROO T, "COMCTL.ListviewCtrl.1\CLS ID", "")
'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_C LASSES_ROO T, "COMCTL.ListviewCtrl.1\CLS ID", "")
'------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
Public Function RegistryGetKeyValue(KeyRoo t 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
'sample usage - Debug.Print RegistryGetKeyValue(HKEY_C
'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_C
'-------------------------
Public Function RegistryGetKeyValue(KeyRoo
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
You might also be able to make use of Windows Scrpt Hosting to do this:
' VBScript.
Set Sh = CreateObject("WScript.Shel l")
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
' VBScript.
Set Sh = CreateObject("WScript.Shel
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
BTW, the above code is in courtesy of John844 dude. I would prefer using RegEx component.
hongjun
hongjun
thanks for the recognition hongjun
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.
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.shel l")
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.shel l")
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.shel l")
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.Shel l")
b.RegDelete Value
End Sub
'form==============
'To Create an registry key:
CreateKey "HKEY_CURRENT_USER\Softwar e\My Software\Version","1.45"
'To Create an Integer registry key:
CreateIntegerKey "HKEY_CURRENT_USER\Softwar e\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\Softwar e\My Software\Version"
' Instead of writing out HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER etc, you can
' use abbreviations such as "HKCU\Software"
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.shel
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.shel
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.shel
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.Shel
b.RegDelete Value
End Sub
'form==============
'To Create an registry key:
CreateKey "HKEY_CURRENT_USER\Softwar
'To Create an Integer registry key:
CreateIntegerKey "HKEY_CURRENT_USER\Softwar
'To Read from the registry
msgbox "Version is " & ReadKey("HKEY_CURRENT_USER
'To Delete from the registry
DeleteKey "HKEY_CURRENT_USER\Softwar
' Instead of writing out HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER etc, you can
' use abbreviations such as "HKCU\Software"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help!
ASKER
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_LOC AL_MACHINE \Software\ Test\Numbe r","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.
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_LOC
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.
http://www.stonebroom.com/swindex.htm
hongjun