Results 1 to 8 of 8

Thread: Modifying the registry:

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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:
    1. Private Sub Form_Load()
    2.     Me.Left = GetSetting(App.EXEName, "Options", "X")'Retrieves The value of X
    3.     Me.Top = GetSetting(App.EXEName, "Options", "Y")'Retrieves the value of Y
    4. End Sub
    5.  
    6. Private Sub Form_Unload(Cancel As Integer)
    7.     SaveSetting App.EXEName, "Options", "X", Me.Left 'Saves the value of me.left to X
    8.     SaveSetting App.EXEName, "Options", "Y", Me.Top 'Saves the value of me.top to Y
    9. End Sub

    Now if you want to check if a registry key exists youd do the following:
    VB Code:
    1. 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:
    1. Option Explicit
    2. Dim reg As cRegistry
    3.  
    4. Private Sub SaveSettings() 'Writes to Registry
    5.     Set reg = New cRegistry
    6.     'This will create a new value in the Run section
    7.     'so your program will automatically run when Windows starts
    8.        'This will create a new section to store your own keys in
    9. reg.Classkey = HKEY_CURRENT_USER
    10. reg.SectionKey = "Software\My Company\My App"
    11. 'create the above key
    12. reg.CreateKey
    13. 'Save the Form settings to the registry
    14. reg.Valuetype = REG_DWORD
    15. reg.Valuekey = "FormTop"
    16. reg.Value = CLng(Me.Top)
    17. reg.Valuekey = "FormLeft"
    18. reg.Value = CLng(Me.Left)
    19. reg.Valuekey = "FormWidth"
    20. reg.Value = CLng(Me.Width)
    21. reg.Valuekey = "FormHeight"
    22. reg.Value = CLng(Me.Height)
    23. End Sub
    24.  
    25. Private Sub GetSettings() 'Reads the Registry
    26.     Set reg = New cRegistry
    27.     reg.ClassKey = HKEY_CURRENT_USER
    28.     reg.SectionKey = "Software\My Company\My App"
    29.     reg.ValueKey = "FormTop"
    30.     Me.Top = reg.Value
    31.     reg.ValueKey = "FormLeft"
    32.     Me.Left = reg.Value
    33. End Sub
    34.  
    35. Private Sub Form_Load()
    36. Call GetSettings
    37. End Sub
    38.  
    39. Private Sub Form_Unload(Cancel As Integer)
    40. Call SaveSettings
    41. End Sub

    Now what ive done with these function below, is simplified EVERYTHING.
    VB Code:
    1. ''For Saving
    2. Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String)
    3.    Set reg = New cRegistry
    4.        'This will create a new section to store your own keys in
    5. reg.Classkey = Hkey
    6. reg.SectionKey = SectionKey
    7. 'create the above key
    8. reg.CreateKey
    9. 'Save the Form settings to the registry
    10. reg.Valuetype = Valuetype
    11. reg.Valuekey = Valuekey
    12. reg.Value = Value
    13. End Function

    Usage:
    VB Code:
    1. Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "80.2523.135.315:70")

    For Reading:
    VB Code:
    1. Dim reg As cRegistry
    2.  
    3. Private Function GetKeys(Hkey As String, Section As String, Valuekey As String)  'Reads the Registry
    4.     Set reg = New cRegistry
    5.     reg.Classkey = Hkey
    6.     reg.SectionKey = Section
    7.     reg.Valuekey = Valuekey
    8.     Msgbox reg.value
    9. End Function

    Usage:
    VB Code:
    1. Private Sub Form_Load()
    2. Call GetKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer")
    3. 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.

  2. #2
    New Member
    Join Date
    Mar 2006
    Posts
    8

    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?

  3. #3
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    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

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Modifying the registry:

    Check out these constants

    VB Code:
    1. Public Const REG_NONE As Long = 0
    2. Public Const REG_SZ As Long = 1
    3. Public Const REG_EXPAND_SZ As Long = 2
    4. Public Const REG_BINARY As Long = 3
    5. Public Const REG_DWORD As Long = 4
    6. Public Const REG_LINK As Long = 6
    7. Public Const REG_MULTI_SZ As Long = 7
    8. Public Const REG_RESOURCE_LIST As Long = 8

  5. #5
    New Member
    Join Date
    Mar 2006
    Posts
    8

    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?

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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:
    1. Option Explicit
    2. Dim reg As New cRegistry
    3.  
    4. Private Sub Form_Load()
    5.     Debug.Print (GetKeys(HKEY_CURRENT_USER, "Software\Game\Settings", "ScreenHeight"))
    6.     Debug.Print (CreateKeys(HKEY_CURRENT_USER, "Software\Game\Settings", "ScreenHeight", reg_dword, 700))
    7. End Sub
    8.  
    9. Private Function GetKeys(HKey As String, Section As String, ValueKey As String) As String
    10.     reg.ClassKey = HKey
    11.     reg.SectionKey = Section
    12.     reg.ValueKey = ValueKey
    13.     GetKeys = reg.Value
    14. End Function
    15.  
    16. Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String) As Boolean
    17.     reg.ClassKey = HKey
    18.     reg.SectionKey = SectionKey
    19.     reg.CreateKey
    20.     reg.Valuetype = Valuetype
    21.     reg.ValueKey = ValueKey
    22.     If IsNumeric(Value) Then
    23.         reg.Value = CInt(Value)
    24.     Else
    25.         reg.Value = Value
    26.     End If
    27.     CreateKeys = (reg.Value = Value)
    28. End Function
    29.  
    30. Private Sub Form_Unload(Cancel As Integer)
    31.     Set cRegistry = Nothing
    32. 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

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Modifying the registry:

    Quote 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)

  8. #8
    New Member
    Join Date
    Mar 2006
    Posts
    8

    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
  •  



Click Here to Expand Forum to Full Width