Is there a way to get the setting, and save a setting outside of the visual studio folder in regedit or whatever? I have heard it isnt possible, but it must be...
GetSetting and SaveSetting only save to the VB/VBA section of the registry. You can use other registry editing API's to read or write anywhere that you want.
Yes, using the API you can read and write to the whole registry (well, if you have permission to do so atleast). The code is a bit long since there are several functions involved so I've just attached a class module that I use. I haven't written this myself, I think it's downloaded from VBAccelerator.
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) 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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
VB Code:
Private Sub Command1_Click()
Dim Result As Long
Dim KeyRet As Long
Dim KeyToRead As String
Dim KeyValue As String
Dim Length As Long
Result = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Search Assistant\ACMru\5603", KeyRet)
KeyToRead = "000"
Result = RegQueryValueEx(KeyRet, KeyToRead, 0, 0, "", Length)
KeyValue = Space$(Length - 1)
Result = RegQueryValueEx(KeyRet, KeyToRead, 0, 0, ByVal KeyValue, Length)
' function prototypes, constants, and type definitions
' for Windows 32-bit Registry API
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
' Registry API prototypes
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 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
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 RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
'Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
'Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long
Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hkey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) 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 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
'Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue 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 RegReplaceKey Lib "advapi32.dll" Alias "RegReplaceKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hkey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
'Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
'Declare Function RegSetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long
Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData 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
Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Public Const REG_NONE = 0 ' No value type
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_EXPAND_SZ = 2 ' Unicode nul terminated string
Public Const REG_BINARY = 3 ' Free form binary
Public Const REG_DWORD = 4 ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4 ' 32-bit number (same as REG_DWORD)
Public Const REG_DWORD_BIG_ENDIAN = 5 ' 32-bit number
Public Const REG_LINK = 6 ' Symbolic Link (unicode)
Public Const REG_MULTI_SZ = 7 ' Multiple Unicode strings
Public Const REG_RESOURCE_LIST = 8 ' Resource list in the resource map
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9 ' Resource list in the hardware description
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
Public Const REG_CREATED_NEW_KEY = &H1 ' New Registry Key created
Public Const REG_OPENED_EXISTING_KEY = &H2 ' Existing Key opened
Public Const REG_WHOLE_HIVE_VOLATILE = &H1 ' Restore whole hive volatile
Public Const REG_REFRESH_HIVE = &H2 ' Unwind changes to last flush
Public Const REG_NOTIFY_CHANGE_NAME = &H1 ' Create or delete (child)
Public Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Public Const REG_NOTIFY_CHANGE_LAST_SET = &H4 ' Time stamp
Public Const REG_NOTIFY_CHANGE_SECURITY = &H8
Public Const REG_LEGAL_CHANGE_FILTER = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
'Public Const REG_LEGAL_OPTION = (REG_OPTION_RESERVED Or REG_OPTION_NON_VOLATILE Or REG_OPTION_VOLATILE Or REG_OPTION_CREATE_LINK Or REG_OPTION_BACKUP_RESTORE)
' Reg Create Type Values...
Public Const REG_OPTION_RESERVED = 0 ' Parameter is reserved
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Public Const REG_OPTION_CREATE_LINK = 2 ' Created key is a symbolic link
Public Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
'Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
'Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
'Public Const KEY_EXECUTE = (KEY_READ)
Public 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))
Public Sub savekey(hkey As Long, strpath As String)
Dim keyhand&
r = RegCreateKey(hkey, strpath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub
Function RegQueryStringValue(ByVal hkey As Long, ByVal strValueName As String)
okay, i know this was a while ago. but could you help me here Joacim Andersson?
your class module looks really nice, but all i would like to know is if you could add a ' next to the functions and give me a brief explanation, or post them here
Sure Remix. However I didn't create that class myself but it's the one I use. I don't know exactly what you want me to comment about it so I will instead show you a quick example on how to use it.
VB Code:
Private Sub MyRegistrySettings()
Dim reg As cRegistry
Set reg = New cRegistry
'This will create a new value in the Run section
'so your program will automatically run when Windows starts
Great! worked like a charm for creating and getting keys. I really like that class module now!
okay, just 2 things:
I had to comment out this line: reg.CreateKey = True
because it was telling me it had to return something...i dont know why but it did. it worked without it.
#2:
what i meant by that above, was this:
Public Property Get KeyExists() As Boolean
Public Function DeleteKey() As Boolean
Public Function DeleteValue() As Boolean...
and so on..if you could just show me a small example for how to use them..but i think the example you gave me will overwrite everytime, so it will work perfect for exactly what i need!
thanks so much
Sorry about that reg.CreateKey thingy... That's not a property but rather a function that will return True if it was able to create the key.
The KeyExists returns True if a particular key exists.
VB Code:
reg.ClassKey = HKEY_CURRENT_USER
reg.SectionKey = "Software\My Company\My App"
If reg.KeyExists = False Then
reg.CreateKey
End If
DeleteKey will obviously delete the key.
VB Code:
reg.ClassKey = HKEY_CURRENT_USER
reg.SectionKey = "Software\My Company\My App"
If reg.KeyExists = True Then
reg.DeleteKey
End If
While DeleteValue would delete a value instead.
VB Code:
reg.ClassKey = HKEY_CURRENT_USER
reg.SectionKey = "Software\My Company\My App"
reg.ValueKey = "FormTop"
reg.DeleteValue
This class can also easily be used if you want to associate a file type with your application so that Windows will start your app when the user double click a file in the Windows Explorer.
The above code will associate files named *.xxx with a program named XFiles.exe. It uses the class name XFiles (that a registry section) and has the description "The truth is out there". The description is used as the document type in Windows Explorer when it's in Details view.
I forgot to mention that you can also write the values as any of the different registry value types by first setting the ValueType property before writing to the registry. The default setting is REG_SZ (string).
Oh, I was wrong... The ValueType must be set, if you would use the REG_SZ it saves the values as string, however for the Form position and size you could just as well save them as REG_DWORD, however the class enforce that the value of the type then must be either an Integer or a Long and the Top, Left, Width, and Height properties are actually stored in a Single so you have to convert them first (if you use REG_DWORD). Sorry about the confusion I haven't used this class for a while.
VB Code:
'This will create a new section to store your own keys in
Mine was a little bit different for writing keys. Also, your function has no return value. Do you mean to have a subroutine? That could cause problems.
VB Code:
' This routine allows you to write values into the entire Registry, it currently
' only handles string and double word values.
Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
thanks so much for your great work as usual Joacim,btw how do i pronounce that? is it like joeaqueam or is the c soft? like joeakim?
The C in my name is pronounce as a K. Actually spelling it as Joakim is pretty common, so is Joachim. In Spain and Portugal I've seen this spelling: Joaquim.
Last edited by Joacim Andersson; Jun 6th, 2005 at 02:17 PM.
im working on a way to change your proxy without going thru the hassle. Ive got the code right, its changing the correct values in the registry. But IE is not recognizing the change ..im wondering is there a text file or something IE is comparing the registry keys with?
VB Code:
Private Sub Command1_Click()
Set reg = New cRegistry
'This will create a new value in the Run section
'so your program will automatically run when Windows starts