linuxrox
asked on
Read / Write to registry, proven method
Hello, I'm looking for a proven tested module or ocx i can use in my VB 6 app to read and write to the registry...the entire registry and not just the default HKEY_CURRENT_USER\Software \VB location that VB uses. wasn't sure if there was an actual dll or something i could purchase that would allow me to do this. i'm looking for it to be as easy as possible for me to read/write to the registry and needs to work on win2k and beyond. don't want to have to sift through lines and lines of code to achieve this. I was hoping someone may know of a module proven to work well for this or could provide me a link to something i could purchase that would work with my VB 6 code as a dll or something, with documentation on how to check for a value, read and write etc etc...
Thanks once again!
Thanks once again!
ASKER
okay, and exactly what is the reference called within VB 6?
i see no vbscript as an option in references.
i see no vbscript as an option in references.
Option Explicit
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
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 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
' Reg Create Type Values...
Private Const REG_OPTION_RESERVED = 0 ' Parameter is reserved
Private Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Private Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Private Const REG_OPTION_CREATE_LINK = 2 ' Created key is a symbolic link
Private Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
' Reg 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_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
' Reg Key ROOT Types...
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Private Const ERROR_SUCCESS = 0 ' Return Value...
Private Const REG_SZ = 1 ' Unicode nul terminated string
Private Const REG_DWORD = 4 ' 32-bit number
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
'-------------------------
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As Variant, Optional ByVal vDefault As Variant) As Boolean
'-------------------------
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 KeyValType 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_ALL_ACCESS, hKey) ' Open Registry Key
If (rc <> 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, _
KeyValType, tmpVal, KeyValSize) ' Get/Create Key Value
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Errors
If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then ' Win95 Adds Null Terminated String...
tmpVal = Left(tmpVal, KeyValSize - 1) ' Null Found, Extract From String
Else ' WinNT Does NOT Null Terminate String...
tmpVal = Left(tmpVal, KeyValSize) ' Null Not Found, Extract String Only
End If
'-------------------------
' Determine Key Value Type For Conversion...
'-------------------------
Select Case KeyValType ' Search Data Types...
Case REG_SZ ' String Registry Key Data Type
KeyVal = tmpVal ' Copy String Value
Case REG_DWORD ' Double Word Registry Key Data Type
'For i = Len(tmpVal) To 1 Step -1 ' Convert Each Bit
' KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' Build Value Char. By Char.
'Next
'KeyVal = Format$("&h" + KeyVal) ' Convert Double Word To String
KeyVal = tmpVal
End Select
GetKeyValue = True ' Return Success
rc = RegCloseKey(hKey) ' Close Registry Key
Exit Function ' Exit
'-------------------------
GetKeyError: ' Cleanup After An Error Has Occured...
'-------------------------
Select Case VarType(KeyVal)
Case vbInteger, vbSingle, vbDouble, vbLong, vbCurrency
If IsMissing(vDefault) Then
KeyVal = 0 ' Set Return Val To Empty String
Else
KeyVal = vDefault
End If
Case vbDate
KeyVal = vbNull
Case vbString
If IsMissing(vDefault) Then
KeyVal = "" ' Set Return Val To Empty String
Else
KeyVal = vDefault
End If
Case vbBoolean
If IsMissing(vDefault) Then
KeyVal = 0 ' Set Return Val To Empty String
Else
KeyVal = vDefault
End If
Case Else
KeyVal = vbNull
End Select
GetKeyValue = False ' Return Failure
rc = RegCloseKey(hKey) ' Close Registry Key
'-------------------------
End Function
'-------------------------
'-------------------------
Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
'-------------------------
Dim rc As Long ' Return Code
Dim hKey As Long ' Handle To A Registry Key
Dim hDepth As Long '
Dim lpAttr As SECURITY_ATTRIBUTES ' Registry Security Type
'-------------------------
lpAttr.nLength = 50 ' Set Security Attributes To Defaults...
lpAttr.lpSecurityDescripto
lpAttr.bInheritHandle = True ' ...
'-------------------------
'- Create/Open Registry Key...
'-------------------------
rc = RegCreateKeyEx(KeyRoot, KeyName, _
0, REG_SZ, _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
hKey, hDepth) ' Create/Open //KeyRoot//KeyName
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' Handle Errors...
'-------------------------
'- Create/Modify Key Value...
'-------------------------
If (SubKeyValue = "") Then SubKeyValue = " " ' A Space Is Needed For RegSetValueEx() To Work...
rc = RegSetValueEx(hKey, SubKeyName, _
0, REG_SZ, _
SubKeyValue, Len(SubKeyValue)) ' Create/Modify Key Value
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' Handle Error
'-------------------------
'- Close Registry Key...
'-------------------------
rc = RegCloseKey(hKey) ' Close Key
UpdateKey = True ' Return Success
Exit Function ' Exit
'-------------------------
CreateKeyError:
'-------------------------
UpdateKey = False ' Set Error Return Code
rc = RegCloseKey(hKey) ' Attempt To Close Key
'-------------------------
End Function
'-------------------------
To add a reference for the Windows Scripting Host Object Model, C:\WINDOWS\System32\wshom. ocx using the Add Reference dialog Browse tab.
ASKER
i run this and get error, "variable not defined on : WScript.CreateObject("WScr ipt.Shell" )"
do i have to create some reference to shell object? and if so where is that?
Private Sub Command3_Click()
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScr ipt.Shell" )
WshShell.RegWrite "HKCU\Software\ACME\Fortun eTeller\", 1, "REG_BINARY"
WshShell.RegWrite "HKCU\Software\ACME\Fortun eTeller\Mi ndReader", "Goocher!", "REG_SZ"
bKey = WshShell.RegRead("HKCU\Sof tware\ACME \FortuneTe ller\")
WScript.Echo WshShell.RegRead("HKCU\Sof tware\ACME \FortuneTe ller\MindR eader")
WshShell.RegDelete "HKCU\Software\ACME\Fortun eTeller\Mi ndReader"
WshShell.RegDelete "HKCU\Software\ACME\Fortun eTeller\"
WshShell.RegDelete "HKCU\Software\ACME\"
End Sub
do i have to create some reference to shell object? and if so where is that?
Private Sub Command3_Click()
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScr
WshShell.RegWrite "HKCU\Software\ACME\Fortun
WshShell.RegWrite "HKCU\Software\ACME\Fortun
bKey = WshShell.RegRead("HKCU\Sof
WScript.Echo WshShell.RegRead("HKCU\Sof
WshShell.RegDelete "HKCU\Software\ACME\Fortun
WshShell.RegDelete "HKCU\Software\ACME\Fortun
WshShell.RegDelete "HKCU\Software\ACME\"
End Sub
I don't have vb6 installed on this computer but I think this line will break to...
WScript.Echo WshShell.RegRead("HKCU\Sof tware\ACME \FortuneTe ller\MindR eader")
...should be removed.
Were you able to add a reference for the windows script host object model?
WScript.Echo WshShell.RegRead("HKCU\Sof
...should be removed.
Were you able to add a reference for the windows script host object model?
ASKER
yes i added it as a reference and i get an error on this line:
Set WshShell = WScript.CreateObject("WScr ipt.Shell" ) //variable not defined.
Set WshShell = WScript.CreateObject("WScr
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yea that works. problem is i can open the ACME reg entry but when i try something like:
bkey = WshShell.RegRead("HKCU\SOF TWARE\Adob e\Acrobat Reader\8.0\Annots\cAnnots\ cAnnot\")
i get unable to open registry key for reading...i know the key exists there though.
bkey = WshShell.RegRead("HKCU\SOF
i get unable to open registry key for reading...i know the key exists there though.
try leaving off the last slash. like...
bkey = WshShell.RegRead("HKCU\SOF TWARE\Adob e\Acrobat Reader\8.0\Annots\cAnnots\ cAnnot")
bkey = WshShell.RegRead("HKCU\SOF
ASKER
tried that, didn't work. tried everything all the way back to \Adobe, with and without slashes!! crazy! doesn't work. but it will read HKCU\SOFTWARE\
looks like there is MUCH more to this reading and writing to the registry business than it seems.
looks like there is MUCH more to this reading and writing to the registry business than it seems.
http://www.microsoft.com/technet/scriptcenter/default.mspx
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScr
WshShell.RegWrite "HKCU\Software\ACME\Fortun
WshShell.RegWrite "HKCU\Software\ACME\Fortun
bKey = WshShell.RegRead("HKCU\Sof
WScript.Echo WshShell.RegRead("HKCU\Sof
WshShell.RegDelete "HKCU\Software\ACME\Fortun
WshShell.RegDelete "HKCU\Software\ACME\Fortun
WshShell.RegDelete "HKCU\Software\ACME\"