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