Results 1 to 5 of 5

Thread: Saving to registry..........

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Saving to registry..........

    I need to save some very simple things in my program to the registry. Can someone post a short example of how to Read and write to the windows registry? Also, are there any differences with this in Windows XP? All I need to save is whether or not a a checkbox is checked or not. Then when I load the program get the registery setting and set the checkbox value to that. To be honest, I don't need to save settings much in the small apps I make, and I have forgotten how to do this! I had a module of how to use Get & Save setting in one of my programs, but it was lost when I cleaned up my system and upgraded to Win XP. Any example appreciated.

  2. #2
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    SaveSetting and GetSetting are the most commonly used. They are limited in scope, however. Here is an example of reading a registry key using a copy of the Registry APIs. For more examples, down load the free API Viewer from http://www.allapi.net
    VB Code:
    1. Private Const HKEY_CURRENT_USER = &H80000001
    2. Private Const HKEY_LOCAL_MACHINE = &H80000002
    3.  
    4. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    5. 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
    6. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    7.  
    8. Private Sub Command1_Click()
    9. Dim Result As Long
    10. Dim KeyRet As Long
    11. Dim KeyToRead As String
    12. Dim KeyValue As String
    13. Dim Length As Long
    14. Result = RegOpenKey(HKEY_CURRENT_USER, "Software\KPD-Team\APIViewer 2001\", KeyRet)
    15. KeyToRead = "MSDN"
    16. Result = RegQueryValueEx(KeyRet, KeyToRead, 0, 0, "", Length)
    17. KeyValue = Space$(Length - 1)
    18. Result = RegQueryValueEx(KeyRet, KeyToRead, 0, 0, ByVal KeyValue, Length)
    19. MsgBox KeyValue
    20. RegCloseKey KeyRet
    21. End Sub

  4. #4
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Saving to registry..........

    Hack could u tell me if i can use your code to put read/write option to my vb6 application. could u explain to me where i can get the module for it. Api viewer dowload section has many thigns to downlod . which one do i need? thanks

  5. #5
    Fanatic Member eimroda's Avatar
    Join Date
    Jul 2000
    Location
    Philippines
    Posts
    642

    Re: Saving to registry..........

    without API:
    VB Code:
    1. 'save to registry
    2.     'chkAutoCapsLock is a checkbox and im saving its value in the reg
    3.     'u can put this in a command button click event
    4.     SaveSetting "mySchool", "Settings", "AutoCaps", chkAutoCapsLock

    to get the value and assign to checkbox:
    VB Code:
    1. Sub GetSettings()
    2. On Error GoTo HELL
    3.  
    4.     chkAutoCapsLock = CInt(GetSetting("mySchool", "Settings", "AutoCaps", "0"))
    5.  
    6.     GoTo CleanUp
    7. HELL:
    8.     WriteError Err.Number, Err.Description, Date, Me.Name, "GetSettings"
    9. CleanUp:
    10. End Sub

    form the MSDN:
    GetSetting Function


    Returns a key setting value from an application's entry in the Windowsregistry.

    Syntax

    GetSetting(appname, section, key[, default])

    The GetSetting function syntax has thesenamed arguments:

    Part Description
    appname Required.String expression containing the name of the application or project whose key setting is requested.
    section Required. String expression containing the name of the section where the key setting is found.
    key Required. String expression containing the name of the key setting to return.
    default Optional.Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").


    Remarks

    If any of the items named in the GetSetting arguments do not exist, GetSetting returns the value of default.

    SaveSetting Statement


    Saves or creates an application entry in the application's entry in the Windowsregistry.

    Syntax

    SaveSetting appname, section, key, setting

    The SaveSetting statement syntax has thesenamed arguments:

    Part Description
    appname Required.String expression containing the name of the application orproject to which the setting applies.
    section Required. String expression containing the name of the section where the key setting is being saved.
    key Required. String expression containing the name of the key setting being saved.
    setting Required.Expression containing the value that key is being set to.


    Remarks

    An error occurs if the key setting can’t be saved for any reason.
    On Error GoTo Hell

    Hell:
    Kill Me


    Food For Thought:

    - Do not judge a book... if you're not a judge!


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