Using the windows registry to your advantage.
Results 1 to 6 of 6

Thread: Using the windows registry to your advantage.

  1. #1
    Banned
    Join Date
    May 2003
    Posts
    42

    Using the windows registry to your advantage.

    When doing windows development one of the biggest things I see is people writing applications that store configuration data in text files. I would like to present a small tutorial on scriptable registry editing to store user/configuration data.

    PreReq's:

    Basic knowlege of VB/VBS
    Basic knowlege of registry
    Basic knowlege of WSH (helpfull)

    For this example lets assume we are creating an application where the user must enter their name, country of origin, age, and sex as part of the configuration.

    -------------begin------------

    we will be using several pre-defiined constants in this example, I will list them below.

    HKCU - "HKEY_CURRENT_USER"
    HKLM - "HKEY_LOCAL_MACHINE"
    REG_PATH - the path used to house application data. (can be anything)
    REG_SZ - registry string value
    REG_DWORD - registry integer value

    'BEGIN SCRIPT
    '----------------------------------------------------------------------------------------

    function writeRegValues(strName, strCountry, intAge, strSex)

    const REG_PATH = "HKCU\Software\testApplication\params"

    'define object variable and initialize it as a WSH shell object.
    dim wsh
    set wsh = createobject("wscript.shell")

    'write the values to the registry
    wsh.regWrite REG_PATH & "\name\" & strName, strName, "REG_SZ"
    wsh.regWrite REG_PATH & "\name\" & strName & "\country", strCountry, "REG_SZ"
    wsh.regWrite REG_PATH & "\name\" & strName & "\age", strAge, "REG_DWORD"
    wsh.regWrite REG_PATH & "\name\" & strName & "\sex", strSex, "REG_SZ"

    'for now we will asume that the arguments passed to the function 'writeRegValues' were Joe, USA, 25, male.
    'we have now created the registry values in the HKEY_CURRENT_USER hive:

    'HKEY_CURRENT_USER\Software\testApplication\params\name\joe" - with the string value of "joe"'HKEY_CURRENT_USER\Software\testApplication\params\name\joe\country" - with the string value of "USA"
    'HKEY_CURRENT_USER\Software\testApplication\params\name\joe\age" - with the integer value of 25.
    'HKEY_CURRENT_USER\Software\testApplication\params\name\joe\sex" - with the string value of "male"

    'destroy the WSH object reference to free up memory.
    set wsh=nothing

    'end the function
    end function

    'END SCRIPT

    To use this example call the function like so:

    writeRegValues "joe", "USA", 25, "Male"

    This function does not return a value or status code on purpose. I will post part 2 of this tutorial later on how to retrieve and compare these values. As well as delete reg keys, etc.

    Hope this may help to shed light on using the registry as a powerful tool for storing basic information.

    -2PC

  2. #2
    Senior Member
    Join Date
    Apr 2002
    Posts
    634
    Using the Windows registry to store informations seems to be an interesting idea. But in case of format or Windows re-installation (what can become frequent with some versions), the user will lost nearly automatically all those informations whereas they would not be affected if they would be in the form of independant text files (not at all if you re-install Windows, or through a simple backup if you format your hdd).
    That's why I keep some doubts about the utility of such a method unless you want to hide and/or preserve those infos even if the user uninstall the software deleting all of its files.

    KC
    Life is boring. Play NetHack... --more--

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Well, that's one reason why you should make backups of your registry before reformating...

    Using the registry is indeed quite practical for storing volatile data or user preferences for example. I do however prefere having settings files for copying settings between computers, easier backups and etc.

    Either ways, with more recent version of windows (2K, XP...) you should take care of placing those preference files in the user's profile folder (ie: C:\Documents and settings\username\...) so there's no conflict in multi-user environments (even more so if using terminal server...).

    Ammo
    Credit travels up, blame travels down -- The Boss

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    634
    Well, that's one reason why you should make backups of your registry before reformating...
    It's perfectly true...Unless you want to re-install Windows because your registry became too crappy even to be repaired . It's what happened to me numerous times with 9x/Me.
    Or you can also save only some selected keys, but it is often a too long work for bad results. Moreover I like to be able to refer easily to a specific file for the config of my apps.

    KC
    Life is boring. Play NetHack... --more--

  5. #5
    Senior Member
    Join Date
    Feb 2003
    Posts
    118
    The registry is a good and bad idea. It is a good solution because we have a single and standardized manner to access parameter settings. But it is also a bad solution because all the configurations are stored in one file thus if a virus attacks this file and writes inside in an anarchistic way all your system crash.

    The previous solution (ini file) allowed more safety but there were a great number of very small file which wasted disk space.

    Each solution has its advantages and disadvantages but I think that the least worse is a file of configuration by application.

  6. #6
    Member
    Join Date
    May 2003
    Posts
    45
    I really liked this thread. Scripting to the registry can be very rewarding (or devistating) depending on how you do it and back yourself.

    I always back up my regestry when I first install the OS (after the current updates of course)

    That way I have free reign to jack my system and then try to fix it. It makes for better tweaking that way.

    Good info keep it up man.

    I really liked this thread. Scripting to the registry can be very rewarding (or devistating) depending on how you do it and back yourself.

    I always back up my regestry when I first install the OS (after the current updates of course)

    That way I have free reign to jack my system and then try to fix it. It makes for better tweaking that way.

    Good info keep it up man.

Posting Permissions

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