Working with Windows Registry using Visual Basic 6 - A complete Tutorial-VBForums
Results 1 to 19 of 19

Thread: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

Hybrid View

  1. #1

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Brief description: There are plenty of threads scattered in vbforums which tell you how to interact with the registry but none of them are commented thoroughly so that a new user could understand them... I have tried to summarize here with snapshots all that you can do with the Windows registry... BTW I have tried this with Windows XP SP3 and VB 6.0

    Caution: Before you begin with this tutorial, The very first step that you should learn is how to back up windows registry. Please see the section "Backing up the Windows Registry" below.

    Topics
    1. Backing up the Windows Registry
    2. Writing to The Windows Registry
    3. Reading from The Windows Registry
    4. Deleting entries in The Windows Registry
    5. GetAllSettings
    6. Checking if a key created by SaveSetting exists using vb 6.0 code
    7. Writing to any portion of the registry for which you have permissions <A bit more advanced>
    8. Find keys in the Windows Registry... with an Project Example
    9. Other API References
    10. Summary
    Last edited by Siddharth Rout; Mar 26th, 2009 at 07:57 AM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  2. #2

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Backing up the Windows Registry

    Backing up the Windows Registry is very easy, and results in the generation of a text file. You can save the file to your hard drive or to any removable drive (except a floppy drive ). To backup the Registry, click on the Windows Start Button, then click on Run. Type regedit into the textbox, and click on the OK button as shown in the picture below


    The Windows Registry Editor will appear as shown below...


    Click on the menu File -> Export as shown in the picture. A dialog box will appear asking you to specify the name for the Registry Backup file.



    Give a suitable name for the backupfile. Make sure that the Export Range of 'All' is selected. The extension .reg will automatically be added to the file. Select the path where you want to save the registry backup file. Now click on the Save button, and a backup of the Registry will be saved for you.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Writing to The Windows Registry

    To write to the registry we will use the SaveSetting command. SaveSetting Saves or creates an application entry in the application's entry in the Windows Registry. Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software.

    Syntax

    SaveSetting appname, section, key, setting

    The SaveSetting statement syntax has these named 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.

    Lets understand the syntax...
    You can compare the registry to a database which has fields and records in it...
    The first three arguments of the SaveSetting statement can be compared to fields in a record. The field names just happen to be appname, section, key and setting and we need to simply supply values to this via SaveSetting.

    The best thing about the SaveSetting statement is that you can specify any values you want for appname, section and key. However please ensure that you pick something meaningful. The last field "setting" holds the value that we are really interested in. Lets try it with an example which uses the SaveSetting statement to make entries in the Windows registry for the MyApp application.

    vb Code:
    1. '~~> Place some settings in the registry.
    2. SaveSetting appname := "MyApp", section := "Startup", key := "Top", setting := 75
    3. 'Or
    4. SaveSetting "MyApp","Startup", "Top", 75

    What this code does is that it saves the .Top value of your startup form in the Windows Registry.

    Once this line of code executes, a 'record' is written to the Windows Registry with these four 'field' values. If you want to check this out for yourself, you can start the Registry Editor again, and search for the value by selecting Edit-Find from the Registry Editor's Menu Bar.



    Last edited by Siddharth Rout; Mar 26th, 2009 at 05:11 AM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  4. #4

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Reading from The Windows Registry

    Now that we have the MyApp's startup form's .Top value saved in the Windows Registry, we need to access the value whenever our program starts up. We'll use the Visual Basic GetSetting function to read this value from the Windows Registry, and a good place to execute that function is in the Load Event Procedure of the form. Before I show you that code, let me show you the format for the GetSetting function.

    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 ("").

    As you can see, the first three arguments of the GetSetting function are identical to the SaveSetting statement. The difference lies in the fourth argument. With the SaveSetting Statement, the fourth argument was the value to be written to the Registry. For the GetSetting function, the fourth argument is an optional argument used to specify a default value to be returned in the event that the entry specified cannot be located in the Windows Registry.

    Here's the code to place in the Load Event Procedure of the Startup Form. I have inserted 2 message box in the code below so that you can understand how it works...

    vb Code:
    1. Private Sub Form_Load()
    2.     '~~> Check what is the current top value
    3.     MsgBox Form1.Top
    4.     '~~> Get value from registry
    5.     Form1.Top = GetSetting("MyApp", "Startup", "Top", 25)
    6.     '~~> Check the new top value
    7.     MsgBox Form1.Top
    8. End Sub
    Last edited by Siddharth Rout; Mar 26th, 2009 at 03:51 AM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  5. #5

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Deleting entries in The Windows Registry

    To delete the entry that you made in the registry, you need to use DeleteSetting statement

    Syntax

    DeleteSetting appname, section[, key]

    The DeleteSetting statement syntax has these named arguments:

    Part Description
    appname Required String expression containing the name of the application or project to which the section or key setting applies.
    section Required String expression containing the name of the section where the key setting is being deleted. If only appname and section are provided, the specified section is deleted along with all related key settings.
    key Optional String expression containing the name of the key setting being deleted.

    You can use it to delete the appname, section and key entry, similar to the way you created the entry to begin with. For example

    vb Code:
    1. DeleteSetting "MyApp", "Startup", "Top

    After deleting the key, try searching the registry again and you will see that the key has been deleted.
    Last edited by Siddharth Rout; Mar 26th, 2009 at 03:57 AM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  6. #6

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    GetAllSettings

    Visual Basic provides us a function called GetAllSettings which can be used to return the Registry entries for a particular Appname and Setting. Here's the format for the GetAllSettings function. This is used for example in case we loose track of the entries we have made in the windows registry.

    Syntax

    GetAllSettings(appname, section)

    The GetAllSettings function syntax has these named arguments:

    Part Description
    appname Required String expression containing the name of the application orproject whose key settings are requested.
    section Required String expression containing the name of the section whose key settings are requested. GetAllSettings returns aVariant whose contents is a two-dimensional array of strings containing all the key settings in the specified section and their corresponding values.

    Once GetAllSettings executes and returns a two dimensional array, we can use the Lbound and Ubound functions, in conjunction with a For-Next Loop to move through the elements of the Array and display them as shown in the code below

    vb Code:
    1. Private Sub Command2_Click()
    2.     Dim MyAppSettings As Variant, i As Long
    3.    
    4.     MyAppSettings = GetAllSettings("MyApp", "Startup")
    5.    
    6.     '~~> Loop through the array
    7.     For i = LBound(MyAppSettings, 1) To UBound(MyAppSettings, 1)
    8.         '~~> Display the values in the array
    9.         MsgBox MyAppSettings(i, 0), MyAppSettings(i, 1)
    10.     Next i
    11. End Sub
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Kool

    Registry stuff is all greek to me, so I read your Caution in
    post #1 and took it to heart.

    I like the fact that you begin with Backing up the Registry
    in post #2 ... nicely sprinkled with screen shots.

    But, here's my beef.. there does not seem to be a Restoring
    the Registry
    post.

    For a newbie like me, it would be reassuring to see that
    subject discussed. I might then have more confidence
    to proceed onward and upward.

    Spoo

  8. #8

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Could you explain a little more on "Restoring the Registry" and I will update that here as well
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  9. #9
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Kool

    Sure. It may be covered in the Writing to Registry section, but ..
    • Let's say, per your BackUp section, I created MyBackup101.reg
    • I then do some stuff to registry
    • How would I Restore the registry using MyBackup101.reg?


    It must be a simple process, but, like I said, I'm a newbie regarding
    this stuff. A few words about that specific task would be helpful.

    Spoo

  10. #10

    Thread Starter
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,925

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    In the 3rd picture in post 2 where you click on "Export", right above it is the Import Button. Simply click that and select you file "MyBackup101.reg" and click 'Ok'. That's it
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  11. #11
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Kool

    D'oh!!
    Like I said, it must be a simple process! Who knew?
    Thanks

    Spoo

  12. #12
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Why is this one returning stripped out handle in X64 OS?
    Thanks.

    http://www.vbforums.com/showthread.p...04#post3859104

  13. #13
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    Not sure about the answer.. but i know that Visual Basic 6 is great programming language!! coolest one)) i'm learning it for a month..hope will be successful

  14. #14
    Member
    Join Date
    Jun 2012
    Posts
    47

    Re: Working with Windows Registry using Visual Basic 6 - A complete Tutorial

    just one question, in what exact part of the registry is the created one goes? i tried this code and it actually works however i'm searching my registry and i think it does not see the created one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.