chrisparr
05-17-2003, 12:31 PM
Hello all,
I am using the following code to (*attempt*) to set values in the registey (windows XP pro).
When I call the proc SaveMachineSettings I would expect the two values passed to it to be saved to HKEY_LOCAL_MACHINE\software\testapp, however they just get saved to HKEY_LOCAL_MACHINE\software
I don't understand why the values are saved at the software level and not testapp.
Any ideas?????
Thanks in advance
Chris
(code below)
<SNIPPET>
Option Explicit
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" _
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, _
lpSecurityAttributes As SECURITY_ATTRIBUTES, _
phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
' This declaration has been modified to accept VB strings
Private Declare Function RegSetValueExStr Lib "advapi32.dll" _
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
' This declaration has been modified to accept VB strings
Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const SYNCHRONIZE = &H100000
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _
KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And _
(Not SYNCHRONIZE))
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) _
And (Not SYNCHRONIZE))
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Sub SaveMachineSettings(ByVal strTCPAddress As String, _
strDBPlatform As String)
Dim hKey As Long
Dim lngDisposition As Long
Dim lngRet As Long
Dim lngRet1 As Long
Dim lngRet2 As Long
Dim sec As SECURITY_ATTRIBUTES
' Open or create the Registry key
lngRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\testapp", _
0, vbNull, REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, sec, hKey, lngDisposition)
' Save the values
If lngRet = ERROR_SUCCESS Then
lngRet1 = RegSetValueExStr(hKey, "ServerAddress", 0, _
REG_SZ, strTCPAddress, Len(strTCPAddress) + 1)
lngRet2 = RegSetValueExStr(hKey, "DBPlatform", 0, _
REG_SZ, strDBPlatform, Len(strDBPlatform) + 1)
If lngRet2 <> ERROR_SUCCESS Or lngRet1 <> ERROR_SUCCESS Then
GoTo SaveErr
End If
' Close the open key
RegCloseKey hKey
Else
GoTo SaveErr
End If
Exit Sub
SaveErr:
' Close the open key
If hKey <> 0 Then
RegCloseKey hKey
End If
Err.Raise vbObjectError + 8000, "SaveMachineSettings", _
"Registry settings could not be saved."
End Sub
Public Sub LoadMachineSettings(strTCPAddress As String, _
strDBPlatform As String)
Dim hKey As Long
Dim lngDisposition As Long
Dim lngRet As Long
Dim lngRet1 As Long
Dim lngRet2 As Long
Dim lngType As Long
Dim strValue As String
Dim lngLen As Long
' Open Registry key
lngRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\" & _
App.ProductName, 0, KEY_READ, hKey)
' Read the values
If lngRet = ERROR_SUCCESS Then
' Create a buffer to store the value
strValue = Space(255)
lngLen = 255
lngRet1 = RegQueryValueExStr(hKey, "ServerAddress", 0, _
lngType, strValue, lngLen)
strTCPAddress = Left(strValue, lngLen - 1)
' Create a buffer to store the value
strValue = Space(255)
lngLen = 255
lngRet2 = RegQueryValueExStr(hKey, "DBPlatform", 0, _
lngType, strValue, lngLen)
strDBPlatform = Left(strValue, lngLen - 1)
If lngRet2 <> ERROR_SUCCESS Or lngRet1 <> ERROR_SUCCESS Then
GoTo LoadErr
End If
' Close the open key
RegCloseKey hKey
Else
GoTo LoadErr
End If
Exit Sub
LoadErr:
' Close the open key
If hKey <> 0 Then
RegCloseKey hKey
End If
Err.Raise vbObjectError + 8001, "LoadMachineSettings", _
"Registry settings could not be loaded."
End Sub
</SNIPPET>
I am using the following code to (*attempt*) to set values in the registey (windows XP pro).
When I call the proc SaveMachineSettings I would expect the two values passed to it to be saved to HKEY_LOCAL_MACHINE\software\testapp, however they just get saved to HKEY_LOCAL_MACHINE\software
I don't understand why the values are saved at the software level and not testapp.
Any ideas?????
Thanks in advance
Chris
(code below)
<SNIPPET>
Option Explicit
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" _
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, _
lpSecurityAttributes As SECURITY_ATTRIBUTES, _
phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
' This declaration has been modified to accept VB strings
Private Declare Function RegSetValueExStr Lib "advapi32.dll" _
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
' This declaration has been modified to accept VB strings
Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const SYNCHRONIZE = &H100000
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _
KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And _
(Not SYNCHRONIZE))
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) _
And (Not SYNCHRONIZE))
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Sub SaveMachineSettings(ByVal strTCPAddress As String, _
strDBPlatform As String)
Dim hKey As Long
Dim lngDisposition As Long
Dim lngRet As Long
Dim lngRet1 As Long
Dim lngRet2 As Long
Dim sec As SECURITY_ATTRIBUTES
' Open or create the Registry key
lngRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\testapp", _
0, vbNull, REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, sec, hKey, lngDisposition)
' Save the values
If lngRet = ERROR_SUCCESS Then
lngRet1 = RegSetValueExStr(hKey, "ServerAddress", 0, _
REG_SZ, strTCPAddress, Len(strTCPAddress) + 1)
lngRet2 = RegSetValueExStr(hKey, "DBPlatform", 0, _
REG_SZ, strDBPlatform, Len(strDBPlatform) + 1)
If lngRet2 <> ERROR_SUCCESS Or lngRet1 <> ERROR_SUCCESS Then
GoTo SaveErr
End If
' Close the open key
RegCloseKey hKey
Else
GoTo SaveErr
End If
Exit Sub
SaveErr:
' Close the open key
If hKey <> 0 Then
RegCloseKey hKey
End If
Err.Raise vbObjectError + 8000, "SaveMachineSettings", _
"Registry settings could not be saved."
End Sub
Public Sub LoadMachineSettings(strTCPAddress As String, _
strDBPlatform As String)
Dim hKey As Long
Dim lngDisposition As Long
Dim lngRet As Long
Dim lngRet1 As Long
Dim lngRet2 As Long
Dim lngType As Long
Dim strValue As String
Dim lngLen As Long
' Open Registry key
lngRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\" & _
App.ProductName, 0, KEY_READ, hKey)
' Read the values
If lngRet = ERROR_SUCCESS Then
' Create a buffer to store the value
strValue = Space(255)
lngLen = 255
lngRet1 = RegQueryValueExStr(hKey, "ServerAddress", 0, _
lngType, strValue, lngLen)
strTCPAddress = Left(strValue, lngLen - 1)
' Create a buffer to store the value
strValue = Space(255)
lngLen = 255
lngRet2 = RegQueryValueExStr(hKey, "DBPlatform", 0, _
lngType, strValue, lngLen)
strDBPlatform = Left(strValue, lngLen - 1)
If lngRet2 <> ERROR_SUCCESS Or lngRet1 <> ERROR_SUCCESS Then
GoTo LoadErr
End If
' Close the open key
RegCloseKey hKey
Else
GoTo LoadErr
End If
Exit Sub
LoadErr:
' Close the open key
If hKey <> 0 Then
RegCloseKey hKey
End If
Err.Raise vbObjectError + 8001, "LoadMachineSettings", _
"Registry settings could not be loaded."
End Sub
</SNIPPET>