|
-
Jun 14th, 2005, 02:12 AM
#1
Thread Starter
Admodistrator
Modifying the registry:
There are a few ways to go about using the registry, (the first way keeps the code inside a specific part of the registry called VB/VBA) the easiest are Called SaveSetting and GetSetting:
To start, lets save our forms X,Y values so it starts off in the same place as it last closed at:
VB Code:
Private Sub Form_Load()
Me.Left = GetSetting(App.EXEName, "Options", "X")'Retrieves The value of X
Me.Top = GetSetting(App.EXEName, "Options", "Y")'Retrieves the value of Y
End Sub
Private Sub Form_Unload(Cancel As Integer)
SaveSetting App.EXEName, "Options", "X", Me.Left 'Saves the value of me.left to X
SaveSetting App.EXEName, "Options", "Y", Me.Top 'Saves the value of me.top to Y
End Sub
Now if you want to check if a registry key exists youd do the following:
VB Code:
If len(GetSetting(App.EXEName, "Options", "X"))>0'then it exists
--------
Now onto the hard way. This way is EXTREMELY useful. It lets you read and write ANYWHERE in the registry.
Download this Class Module:Reg.Cls
Applying the same thought as above, we will save the forms top and left coordinates on form unload, and load them on form load.
VB Code:
Option Explicit
Dim reg As cRegistry
Private Sub SaveSettings() 'Writes to Registry
Set reg = New cRegistry
'This will create a new value in the Run section
'so your program will automatically run when Windows starts
'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 GetSettings() 'Reads the Registry
Set reg = New cRegistry
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
Private Sub Form_Load()
Call GetSettings
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call SaveSettings
End Sub
Now what ive done with these function below, is simplified EVERYTHING.
VB Code:
''For Saving
Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String)
Set reg = New cRegistry
'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
Usage:
VB Code:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "80.2523.135.315:70")
For Reading:
VB Code:
Dim reg As cRegistry
Private Function GetKeys(Hkey As String, Section As String, Valuekey As String) 'Reads the Registry
Set reg = New cRegistry
reg.Classkey = Hkey
reg.SectionKey = Section
reg.Valuekey = Valuekey
Msgbox reg.value
End Function
Usage:
VB Code:
Private Sub Form_Load()
Call GetKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer")
End Sub
Hope this cleared some stuff up, it took me forever to understand how it all works, hope i helped you
Last edited by |2eM!x; Jun 14th, 2005 at 02:23 AM.
-
Apr 17th, 2006, 06:20 AM
#2
New Member
Re: Modifying the registry:
Hi,
I have been trying to use this example to modify the security settings in ms excel as follows:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Office\11.0\Excel\Security", "Level", REG_DWORD, "4")
Although I can write to REG_SZ, REG_DWORD does not work. I assume this is some sort of lock to prevent VBA from altering the security settings to protect against malicious code. Is this correct?
-
Apr 18th, 2006, 08:58 PM
#3
Re: Modifying the registry:
You may be correct, however there are methods to get around it. There are API and constants available that you can use to change the security settings so you enter data into the registry.
Hope this Helps
Jenova
-
Apr 18th, 2006, 09:39 PM
#4
Thread Starter
Admodistrator
Re: Modifying the registry:
Check out these constants
VB Code:
Public Const REG_NONE As Long = 0
Public Const REG_SZ As Long = 1
Public Const REG_EXPAND_SZ As Long = 2
Public Const REG_BINARY As Long = 3
Public Const REG_DWORD As Long = 4
Public Const REG_LINK As Long = 6
Public Const REG_MULTI_SZ As Long = 7
Public Const REG_RESOURCE_LIST As Long = 8
-
Apr 19th, 2006, 03:34 AM
#5
New Member
Re: Modifying the registry:
Thank you (this is the first post I have had a reply to so it is quite exciting).
I have managed to modify the part of the registry in question using some other code. I would include it in this post but I get the impression that what I am doing is a little contentious and that you don't want exact details on how to modify the security setting on this forum. I can understand and respect that.
The code I used to do this all resides in a module and uses:
Declare Function RegCreateKeyEx& Lib "advapi32.dll"
Declare Function RegSetValueEx& Lib "advapi32.dll" (uses lpDataBuff as Any)
Declare Function RegCloseKey& Lib "advapi32.dll"
And it works fine. The thing is that I am now really curious about the code given in the example above because I know these settings can be modified. There is no block to prevent it. I also know that the code given can modify Reg_SZ and can be used to read the value that I want to change. But I just can't get it to work.
I have used the public constants given but it still only operates when I use REG_SZ. I even checked out another constant (Public Const HKEY_CURRENT_USER = &H80000001).
The thing I just can't fathom is why it works with REG_SZ and not with REG_DWORD.
Can you give me any more clues?
-
Apr 20th, 2006, 07:43 PM
#6
Thread Starter
Admodistrator
Re: Modifying the registry:
Sorry, I misread your problem, the error was because inside the class it checks to see whether you are passing an integer with the DWORD or a string, and accidently we were passing a string instead.
VB Code:
Option Explicit
Dim reg As New cRegistry
Private Sub Form_Load()
Debug.Print (GetKeys(HKEY_CURRENT_USER, "Software\Game\Settings", "ScreenHeight"))
Debug.Print (CreateKeys(HKEY_CURRENT_USER, "Software\Game\Settings", "ScreenHeight", reg_dword, 700))
End Sub
Private Function GetKeys(HKey As String, Section As String, ValueKey As String) As String
reg.ClassKey = HKey
reg.SectionKey = Section
reg.ValueKey = ValueKey
GetKeys = reg.Value
End Function
Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String) As Boolean
reg.ClassKey = HKey
reg.SectionKey = SectionKey
reg.CreateKey
reg.Valuetype = Valuetype
reg.ValueKey = ValueKey
If IsNumeric(Value) Then
reg.Value = CInt(Value)
Else
reg.Value = Value
End If
CreateKeys = (reg.Value = Value)
End Function
Private Sub Form_Unload(Cancel As Integer)
Set cRegistry = Nothing
End Sub
This is MUCH better code, and I think you will understand it better. I sucked pretty bad at coding when I wrote this, and I didnt even use the functions to return anything
Anywho, if you have troubles, drop a message right here
-
Apr 22nd, 2006, 08:53 PM
#7
Re: Modifying the registry:
 Originally Posted by GMWhite
Hi,
I have been trying to use this example to modify the security settings in ms excel as follows:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Office\11.0\Excel\Security", "Level", REG_DWORD, "4")
Although I can write to REG_SZ, REG_DWORD does not work. I assume this is some sort of lock to prevent VBA from altering the security settings to protect against malicious code. Is this correct?
You're sending REG_DWORD a string - try sending it a numeric:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Office\11.0\Excel\Security", "Level", REG_DWORD, 4)
-
Apr 24th, 2006, 04:59 AM
#8
New Member
Re: Modifying the registry:
I have it working. Thanks all for your help.
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
|