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...
Printable View
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, you need API calls to read/write to other registry keys.
RegOpenKey
RegQueryKeyValue
RegCloseKey
etc.
sweet!Thankyou so much, ill post if i have problems, i hope they are pretty explanatory :D
nvm, grr stupid allapi.net
A little but here is an example.
Registry Keys
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.
No, they're VB6 but your probably looking at the .NET equilivalent
Win32.RegistryKey.Close
etc.
i found it and it works, im gonna go look for the save one, what is its name?
heres the code so far:
Option Explicit
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
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) MsgBox KeyValue RegCloseKey KeyRet End Sub
RegSaveKey
This is a general purpose registry module. It has most of what you need.
No it's RegCreateKey, RegCreateKeyEx, RegSetValue, RegSetValueExQuote:
Originally Posted by RobDog888
VB Code:
Option Explicit '----------------- ' ADVAPI32 ' ----------------- ' 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 '''''''''''''''''''''''''''''''''''''''''''''''''''''
just, how do i call itVB Code:
Public Const STANDARD_RIGHTS_READ = &H20000 Public Const STANDARD_RIGHTS_WRITE = &H20000 Public Const STANDARD_RIGHTS_EXECUTE = &H20000 Public Const STANDARD_RIGHTS_REQUIRED = &HF0000 Public Const STANDARD_RIGHTS_ALL = &H1F0000 Public Const DELETE = &H10000 Public Const READ_CONTROL = &H20000 Public Const WRITE_DAC = &H40000 Public Const WRITE_OWNER = &H80000 Public Const SYNCHRONIZE = &H100000 ' Reg Key Security Options Public Const KEY_QUERY_VALUE = &H1 Public Const KEY_SET_VALUE = &H2 Public Const KEY_CREATE_SUB_KEY = &H4 Public Const KEY_ENUMERATE_SUB_KEYS = &H8 Public Const KEY_NOTIFY = &H10 Public Const KEY_CREATE_LINK = &H20 '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) 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 getstring(hkey As Long, strpath As String, strvalue As String) 'This module allows easy access to the Windows 95 and NT Registry. 'It takes the following parameters: ' 'HKEY - one of the following constants: 'HKEY_CLASSES_ROOT 'HKEY_CURRENT_USER 'HKEY_LOCAL_MACHINE 'HKEY_USERS 'HKEY_PERFORMANCE_DATA ' 'strpath - the rest of the registry 'path' eg: '"Control Panel\desktop" ' 'strvalue - A string that refers to the key that you want to retrieve eg: '"Wallpaper" ' 'The Example above would be called as follows: ' 'dim strwallpaper as string 'strwallpaper = getstring(HKEY_CURRENT_USER, "Control Panel\desktop", "Wallpaper") ' 'This would return the current desktop wallpaper ' 'This module downloaded from VB-World at [url]http://www.geocities.com/SiliconValley/Bay/8409/[/url] Dim keyhand& Dim datatype& r = RegOpenKey(hkey, strpath, keyhand&) getstring = RegQueryStringValue(keyhand&, strvalue) r = 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 savestring(hkey As Long, strpath As String, strvalue As String, strdata As String) 'This module allows easy access to the Windows 95 and NT Registry. 'It takes the following parameters: ' 'HKEY - one of the following constants: 'HKEY_CLASSES_ROOT 'HKEY_CURRENT_USER 'HKEY_LOCAL_MACHINE 'HKEY_USERS 'HKEY_PERFORMANCE_DATA ' 'strpath - the rest of the registry 'path' eg: '"Control Panel\desktop" ' 'strvalue - A string that refers to the key that you want to save eg: '"Wallpaper" ' 'strdata - the string you want to save against strvalue eg: '"c:\windows\clouds.bmp" ' 'The Example above would be called as follows: ' 'dim strwallpaper as string 'call savestring(HKEY_CURRENT_USER, "Control Panel\desktop", "Wallpaper", "C:\Windows\Clouds.bmp") ' 'If this 'path' does not exist, it will be created. ' 'This module downloaded from VB-World at [url]http://www.geocities.com/SiliconValley/Bay/8409/[/url] Dim keyhand& r = RegCreateKey(hkey, strpath, keyhand&) r = RegSetValueEx(keyhand&, strvalue, 0, REG_SZ, ByVal strdata, Len(strdata)) r = RegCloseKey(keyhand&) End Sub
dg that bas did nothing for me... :eek:
anyone? im lost
You already have the API's declared, so I won't post those, but here are the two routines I use to get and save/update registry keys with examples.Does this help?VB Code:
Public Function GetKeyValue(KeyRoot As Long, 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 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_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, _ lKeyValType, tmpVal, KeyValSize) ' Get/Create Key Value If (rc <> 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 GetKeyValue = sKeyVal ' Return Value rc = RegCloseKey(hKey) ' Close Registry Key Exit Function ' Exit GetKeyError: ' Cleanup After An Error Has Occured... GetKeyValue = vbNullString ' Set Return Val To Empty String rc = RegCloseKey(hKey) ' Close Registry Key End Function 'Example Usage: intValue = Val(GetKeyValue(&H80000002, gsKey, "Key Name")) 'gsKey is a global string containing folder key names...like \SOFTWARE\AppName\AppKey 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.lpSecurityDescriptor = 0 ' ... 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... ' Create/Modify Key Value rc = RegSetValueEx(hKey, SubKeyName, _ 0, REG_SZ, _ SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode))) 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 'Example Useage: glReSult = UpdateKey(&H80000002, gsKey, "What I Want To Call The Key", Val(Text1.Text)) 'where Text1.Text contains the value that I want to put in this key
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 reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run" reg.ValueKey = "MyProgram" reg.Value = "c:\MyProgram\MyApp.exe" 'This will create a new section to store your own keys in reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" 'create the above key reg.CreateKey = True 'Save the Form settings to the registry reg.ValueKey = "FormTop" reg.Value = Me.Top reg.ValueKey = "FormLeft" reg.Value = Me.Left reg.ValueKey = "FormWidth" reg.Value = Me.Width reg.ValueKey = "FormHeight" reg.Value = Me.Height 'Even if its stupid to do so here since we just saved 'these settings this is how you read them back reg.ValueKey = "FormTop" Me.Top = reg.Value reg.ValueKey = "FormLeft" Me.Left = reg.Value '...and so on End Sub
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.DeleteKey will obviously delete the key.VB Code:
reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" If reg.KeyExists = False Then reg.CreateKey End IfWhile DeleteValue would delete a value instead.VB Code:
reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" If reg.KeyExists = True Then reg.DeleteKey End IfThis 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.VB Code:
reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" reg.ValueKey = "FormTop" reg.DeleteValueThe 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.VB Code:
Call reg.CreateEXEAssociation("c:\MyPath\XFiles.exe", _ "XFiles", "The truth is out there", "xxx")
thanks so much! but as ive been messing with it, the values are returning "Empty" and thus placing the form at 0,0..im not sure why...
That's probably because you marked out the CreateKey call.
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).VB Code:
reg.ValueType = REG_DWORD
Its still returning empty, tell me if/what im doing wrong here:
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 reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run" reg.ValueKey = "MyProgram" reg.Value = "c:\MyProgram\MyApp.exe" 'This will create a new section to store your own keys in reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" reg.CreateKey 'create the above key 'Save the Form settings to the registry reg.ValueKey = "FormTop" reg.Value = Me.Top reg.ValueKey = "FormLeft" reg.Value = Me.Left reg.ValueKey = "FormWidth" reg.Value = Me.Width reg.ValueKey = "FormHeight" reg.Value = Me.Height 'Even if its stupid to do so here since we just saved 'these settings this is how you read them back reg.ValueKey = "FormTop" Me.Top = reg.Value reg.ValueKey = "FormLeft" Me.Left = reg.Value '...and so on End Sub
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 reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.ValueType = REG_DWORD reg.ValueKey = "FormTop" reg.Value = CLng(Me.Top) reg.ValueKey = "FormLeft" reg.Value = CLng(Me.Left) reg.ValueKey = "FormWidth" reg.Value = CLng(Me.Width) reg.ValueKey = "FormHeight" reg.Value = CLng(Me.Height)
The attached image shows the values saved as both REG_SZ (strings) and as REG_DWORD (Long).
Just for future reference let me post this:
VB Code:
Private Sub MyRegistrySettings() Set reg = New cRegistry 'This will create a new value in the Run section 'so your program will automatically run when Windows starts reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run" reg.ValueKey = "MyProgram" reg.Value = "c:\MyProgram\MyApp.exe" 'This will create a new section to store your own keys in reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.ValueType = REG_DWORD reg.ValueKey = "FormTop" reg.Value = CLng(Me.Top) reg.ValueKey = "FormLeft" reg.Value = CLng(Me.Left) reg.ValueKey = "FormWidth" reg.Value = CLng(Me.Width) reg.ValueKey = "FormHeight" reg.Value = CLng(Me.Height) End Sub Private Sub Command1_Click() Call MyRegistrySettings End Sub Private Sub Command12_Click() Set reg = New cRegistry reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run" reg.ValueKey = "MyProgram" reg.Value = "c:\MyProgram\MyApp.exe" 'This will create a new section to store your own keys in reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" reg.ValueKey = "FormTop" Me.Top = reg.Value reg.ValueKey = "FormLeft" Me.Left = reg.Value End Sub
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?
thanks again :wave:
Sorry, remix. I just noticed that I posted the wrong module before.
I have replaced it with the correct one. :wave:
thanks dg, yeah the old one was corrupted or something (836 bytes i believe) : D
I use this one in two different apps. It works well, and if you uncomment the setction on errors, helps to debug problems like you were having.
Just to let you guys know..i made a simple function for this, to make it ALOT easier to read.
VB Code:
Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String) reg.ClassKey = HKey reg.SectionKey = SectionKey reg.ValueKey = ValueKey reg.Value = Value 'This will create a new section to store your own keys in reg.ClassKey = HKey reg.SectionKey = SectionKey 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.Valuetype = Valuetype reg.ValueKey = ValueKey reg.Value = Value End Function
call it like:
VB Code:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "80.2523.135.315:70")
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) Dim lResult As Long Dim lKeyValue As Long Dim InLen As Long Dim lNewVal As Long Dim sNewVal As String On Error Resume Next RegCreateKeyEx Group, Section, 0, "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, lKeyValue, lResult If ValType = ValDWord Then lNewVal = CLng(Value) InLen = 4 lResult = RegSetValueExLong(lKeyValue, Key, 0&, ValType, lNewVal, InLen) Else sNewVal = Value InLen = Len(sNewVal) lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen) End If lResult = RegFlushKey(lKeyValue) lResult = RegCloseKey(lKeyValue) End Sub
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.Quote:
Originally Posted by |2eM!x
okay i figure ill post this here.
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 :blush:..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 Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "65.215.15.95:80") Call CreateKeys(HKEY_USERS, "S-1-5-21-1715567821-413027322-725345543-1004\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "65.215.15.95:80") End Sub Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String) reg.ClassKey = HKey reg.SectionKey = SectionKey reg.ValueKey = ValueKey reg.Value = Value 'This will create a new section to store your own keys in reg.ClassKey = HKey reg.SectionKey = SectionKey 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.Valuetype = Valuetype reg.ValueKey = ValueKey reg.Value = Value End Function
this allows you to change the proxy in IE, thats where i know its stored..but unfortunately IE doesnt recognize my change..any ideas?
Did you re-start IE?
yes, but it should still be able to use it while running
Then what is this?
Quote:
'This will create a new value in the Run section
'so your program will automatically run when Windows starts
1. Is ProxyEnable set to enable (1) ?
2. I think you have to put in the proxy server as such
"http=65.215.15.95:80"
*EDIT* And https= if you want that as well. Or ftp= gopher= ...
i dont know, thats the comments that i was given above...its modifying them as it runs, ive been checking..
also, the format IE puts it in, in the registry is the same way ive been putting it, so i dont think there is a http=