|
-
Aug 24th, 2000, 01:45 PM
#1
Hi,
Can any one explain the use of RegOpenKey and RegCloseKey in regarding to the Registry. I have noticed that some people use it in their code and some people dont.
Is it necessary to use them, if so why ?
Thanks in advance
-
Aug 24th, 2000, 01:49 PM
#2
Here is my full registry Module.
Put the following in a Module.
Code:
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal HKEY As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal HKEY As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
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
Public Const REG_SZ = 1
Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)
Dim lResult As Long
Dim lValueType As Long
Dim strBuf As String
Dim lDataBufSize As Long
On Error GoTo 0
lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
RegQueryStringValue = StripTerminator(strBuf)
End If
End If
End If
End Function
Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
Dim KeyHand&
Dim datatype&
Call RegOpenKey(HKEY, sPath, KeyHand&)
GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
Call RegCloseKey(KeyHand&)
End Function
Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function
Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
Dim KeyHand As Long
Call RegCreateKey(HKEY, sPath, KeyHand)
Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))
Call RegCloseKey(KeyHand&)
End Sub
Usage: Requires 2 Commandbuttons
Code:
Private Sub Command1_Click()
'Save a Value to the Registry
savestring HKEY_CURRENT_USER, "Software\Myapp", "Testing", "Hello"
End Sub
Private Sub Command2_Click()
'Get a value from the Registry
Retval = GetString(HKEY_CURRENT_USER, "Software\Myapp", "Testing")
Print Retval
End Sub
-
Aug 24th, 2000, 01:50 PM
#3
Monday Morning Lunatic
That's fine if you only want strings. To use DWORDs as well, here's the code, and an example: http://www.parksie.uklinux.net/registry.zip
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 24th, 2000, 01:53 PM
#4
Thanks,
But i actually wanted to know why do you have to use RegOpenKey and RegClose function and how come some people dont use it in their code.
Thanks again
-
Aug 24th, 2000, 01:56 PM
#5
Monday Morning Lunatic
GetSetting and SaveSetting only allow changing of string values under HKEY_CURRENT_USER\Software\VB and VBA Program Settings. For many apps, that is fine, but other people want to give a professional edge: ie use Software\My Company\My Product\Options etc. The RegOpenKeyEx function and its siblings let you access any part of the registry (except on NT, where permissions may limit you). You can also enter/retrieve items of any type.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 24th, 2000, 01:58 PM
#6
It's not that hard to save as DWORD's. Simply replace REG_SZ with REG_DWORD. You'll need this constant for it.
Code:
Const REG_DWORD = 4
-
Aug 24th, 2000, 02:08 PM
#7
I am sorry you guys misunderstood my question. I will try to explain again. Look at the following code, in here they dont use the Function RegOpenKey and RegCloseKey at all but the code still works.
If you look at Megatron's Code you will see he used both function(regopenkey & regclosekey). I just wnat to know why do you have to use these two functions. Any ideas
VB Code:
Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Const ERROR_SUCCESS = 0&
Const ERROR_BADDB = 1009&
Const ERROR_BADKEY = 1010&
Const ERROR_CANTOPEN = 1011&
Const ERROR_CANTREAD = 1012&
Const ERROR_CANTWRITE = 1013&
Const ERROR_REGISTRY_RECOVERED = 1014&
Const ERROR_REGISTRY_CORRUPT = 1015&
Const ERROR_REGISTRY_IO_FAILED = 1016&
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Const regKey = "Sybex\Mastering VB 5.0"
Private Sub Form_Load()
Dim retValue As Long
Dim result As Long
Dim keyID As Long
Dim keyValue As String
Dim subKey As String
Dim bufSize As Long
Label6.Caption = regKey
'Create key
retValue = RegCreateKey(HKEY_CURRENT_USER, regKey, keyID)
If retValue = 0 Then
'Create width
subKey = "Window Width"
retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
0&, bufSize)
'No value, set it
If bufSize < 2 Then
keyValue = Me.Width
retValue = RegSetValueEx(keyID, subKey, 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
Else
keyValue = String(bufSize + 1, " ")
retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
ByVal keyValue, bufSize)
keyValue = Left$(keyValue, bufSize - 1)
Me.Width = keyValue
End If
'Set values on form
Label4.Caption = subKey
Label5.Caption = Me.Width
'Create height
subKey = "Window Height"
retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
0&, bufSize)
If bufSize < 2 Then
keyValue = Me.Height
retValue = RegSetValueEx(keyID, subKey, 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
Else
keyValue = String(bufSize + 1, " ")
retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
ByVal keyValue, bufSize)
keyValue = Left$(keyValue, bufSize - 1)
Me.Height = keyValue
End If
'Set values on form
Label8.Caption = subKey
Label7.Caption = Me.Height
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim keyValue As String
Dim retValue As Long
Dim keyID As Long
retValue = RegCreateKey(HKEY_CURRENT_USER, regKey, keyID)
keyValue = Me.Width
retValue = RegSetValueEx(keyID, "Window Width", 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
keyValue = Me.Height
retValue = RegSetValueEx(keyID, "Window Height", 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
MsgBox "Registry updated"
End Sub
Last edited by Danial; Aug 1st, 2001 at 09:19 PM.
-
Aug 24th, 2000, 02:10 PM
#8
Monday Morning Lunatic
RegCreateKeyEx creates a key and opens it. If the key is already there, it simply opens it. RegOpenKeyEx, on the other hand, returns an error if the key is not present. In the example you have, their code is wrong, because you should always close every key you open.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|