ASP MagicRegistry Object  

The MagicRegistry object allows you to manipulate registry from an Active Server Page. You can create registry keys and tree branches, read them and change values.

 
 

Methods

  ConnectToRemote Method
SelectSection Method
GetPosition Method
CreateNewKey Method
SetKeyValue Method
SetKeyValueType Method
GetTypeValue Method
QueryValue Method
DeleteKey Method
DeleteField Method
KeyScanSimple Method
KeyScanRecursive Method
SortKeyList Method
ValueScan Method
RemoveFromKeyList Method
RemoveFromValueList Method
CleanKeyList Method
CleanValueList Method
KeyList Method
ValueList Method
GetLastErrorNbr Method
GetLastErrorDscr Method

 

Properties

  KeyCount Property
ValueCount Property

 

Working with MagicRegistry

To call the MagicRegistry Object from an Active Server Page, you will need to create the object using the following code:

<%Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")%>

Remember to enclose the code within <% %> brackets.
 
ConnectToRemote Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.ConnectToRemote(strRemote)
strRemote - remote computer name; if strRemote = "" then it connects to the localhost.

Example: The following example connects to the remote computer named "Host2".

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
if MyObj.ConnectToRemote("\\Host2") then
  response.write("Connection is established successfully.")
else
   response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
   response.write(MyObj.GetLastErrorDscr)
end if
set MyObj=Nothing
%>
SelectSection Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.SelectSection(strSection)
strSection - section name. Section name should be one of the following values:

"HKEY_LOCAL_MACHINE" "HKLM"
"HKEY_CURRENT_USER" "HKCU"
"HKEY_CLASSES_ROOT" "HKCR"
"HKEY_CURRENT_CONFIG" "HKCC"
"HKEY_USERS" "HKUS"
"HKEY_PERFORMANCE_DATA"   "HKPD"
"HKEY_DYN_DATA" "HKDD"

Note, that each of the section may be defined as long section name like "HKEY_LOCAL_MACHINE" or as short section name like "HKLM".
If section is not selected, all functions works with "HKEY_LOCAL_MACHINE" section.

Example: The following example selects the "HKEY_CURRENT_USER" section.

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
if MyObj.SelectSection("HKCU") then
  response.write("Section is selected.")
else
   response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
   response.write(MyObj.GetLastErrorDscr)
end if
set MyObj=Nothing
%>
GetPosition Method
Return Value: the current registry section if seccessfull, otherwise FALSE.
The current registry section (returned value) is one of the following values:

    "HKEY_LOCAL_MACHINE",
    "HKEY_CURRENT_USER",
    "HKEY_CLASSES_ROOT",
    "HKEY_CURRENT_CONFIG",
    "HKEY_USERS",
    "HKEY_PERFORMANCE_DATA",
    "HKEY_DYN_DATA"

Syntax: object.GetPosition()

Example: The following example obtains the current registry section.

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")

response.write("Current registry section is " & MyObj.GetPosition)
set MyObj=Nothing
%>
CreateNewKey Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.CreateNewKey(strKey)

Example: The following example creates new keys (test1\test2) in the registry under HKEY_LOCAL_MACHINE\Software

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
if MyObj.CreateNewKey("Software\test1\test2") then
  response.write("HKEY_LOCAL_MACHINE\Software\test1\test2 created successfully")
else
   response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
   response.write(MyObj.GetLastErrorDscr)
end if
set MyObj=Nothing
%>
SetKeyValue Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.SetKeyValue(strKey, strName, strValue)

Example: The following example sets new key MyKey in the registry under HKEY_LOCAL_MACHINE\Software\test1\test2

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
if MyObj.SetKeyValue("Software\test1\test2", "MyKey", "Hello") then
  response.write("HKEY_LOCAL_MACHINE\Software\test1\test2 Mykey set successfully")
else
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
  response.write("<br>")
end if
set MyObj=Nothing
%>
GetTypeValue Method
Return Value: the current key type if seccessfull, otherwise "". The current key type (returned value) is one of the following values:

"REG_SZ"
"REG_BINARY"
"REG_DWORD_LITTLE_ENDIAN"
"REG_DWORD_BIG_ENDIAN"
"REG_EXPAND_SZ"
"REG_LINK"
"REG_MULTI_SZ"
"REG_NONE"
"REG_RESOURCE_LIST"
"REG_DWORD"

Syntax: object.GetTypeValue(strKey, strName)
Example: For example the following code will get a type of the MyKey key.
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
keyType = MyObj.GetTypeValue("Software\test1\test2","MyKey")
If keyType<>"" then
  response.write("Type of the HKEY_LOCAL_MACHINE\Software\test1\test2 Mykey is " & keyType)
else
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
  response.write("<br>")
end if
set MyObj=Nothing
%>
SetKeyValueType Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.SetKeyValueType(strKey, strName, strValue)

You can set the type of the key by using function SetKeyValueType and passing in one of the 10 parameters as string.

"REG_SZ"
"REG_BINARY"
"REG_DWORD_LITTLE_ENDIAN"
"REG_DWORD_BIG_ENDIAN"
"REG_EXPAND_SZ"
"REG_LINK"
"REG_MULTI_SZ"
"REG_NONE"
"REG_RESOURCE_LIST"
"REG_DWORD"

Example: For example the following code will set MyKey to the DWORD value 10.
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
if MyObj.SetKeyValueType("Software\test1\test2","MyKey", 10, "REG_DWORD") then
  response.write("HKEY_LOCAL_MACHINE\Software\test1\test2 Mykey set successfully")
else
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
  response.write("<br>")
end if
set MyObj=Nothing
%>
QueryValue Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.QueryValue(strKey, strName, strValue)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.QueryValue("Software\test1\test2", "MyKey", regValue)
if IsOk=0 then
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
else
  response.write("HKEY_LOCAL_MACHINE\Software\test1\test2, MyKey=" & regValue)
end if
set MyObj=Nothing
%>
DeleteKey Method
This method deletes the key and all subkeys (if present) from the registry
Return Value: Nothing
Syntax: object.DeleteKey(strKey)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
MyObj.DeleteKey("Software\test1")
set MyObj=Nothing
%>
DeleteField Method
This method deletes the value from the registry
Return Value: Nothing
Syntax: object.DeleteField(strKey, strValue)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
MyObj.DeleteField("Software\test1\test2", "MyKey")
set MyObj=Nothing
%>
KeyScanSimple Method
This method scans non-recursively keys and fills Key list
Return Value: Nonzero if successful; otherwise 0
Syntax: object.KeyScanSimple(strKey)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanSimple("Software\test1")
if IsOk=0 then
  response.write("Keys was not found")
else
  response.write("Keys was found")
end if
set MyObj=Nothing
%>
KeyScanRecursive Method
This method scans recursively keys and fills Key list
Return Value: Nonzero if successful; otherwise 0
Syntax: object.KeyScanRecursive(strKey)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanRecursive("Software\test1")
if IsOk=0 then
  response.write("Keys was not found")
else
  response.write("Keys was found")
end if
set MyObj=Nothing
%>
SortKeyList Method
This method sorts Key list in ascending or descending order
Return Value: Nothing
Syntax: object.SortKeyList(varDirection)
varDirection<>0 - Key list will be sorted in ascending order
varDirection=0 - Key list will be sorted in descending order
ValueScan Method
This method scans values in selected registry and fill Value list
Return Value: Nonzero if successful; otherwise 0
Syntax: object.ValueScan(strKey)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.ValueScan("Software\test1\test2")
if IsOk=0 then
  response.write("Values was not found")
else
  response.write("Values was found")
end if
set MyObj=Nothing
%>
RemoveFromKeyList Method
This method removes the key from Key list
Return Value: Nothing
Syntax: object.RemoveFromKeyList(strKey)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanSimple("Software\test1")
if IsOk=0 then
  response.write("Values was not found")
else
  response.write("Values was found")
end if
MyObj.RemoveFromKeyList("Software\test2")
set MyObj=Nothing
%>
RemoveFromValueList Method
This method removes the value from Value list
Return Value: Nothing
Syntax: object.RemoveFromValueList(strValue)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.ValueScan("Software\test1\test2")
if IsOk=0 then
  response.write("Values was not found")
else
  response.write("Values was found")
end if
MyObj.RemoveFromValueList("MyKey")
set MyObj=Nothing
%>
CleanKeyList Method
This method removes all records from Key list
Return Value: Nothing
Syntax: object.CleanKeyList()

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanRecursive("Software\test1")
if IsOk=0 then
  response.write("Keys was not found")
else
  response.write("Keys was found")
end if
MyObj.CleanKeyList()
set MyObj=Nothing
%>
CleanValueList Method
This method removes all records from Value list
Return Value: Nothing
Syntax: object.CleanValueList()

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.ValueScan("Software\test1\test2")
if IsOk=0 then
  response.write("Values was not found")
else
  response.write("Values was found")
end if
MyObj.CleanValueList()
set MyObj=Nothing
%>
KeyList Method
This method returns an item from Key list by KeyNo index
Return Value: an item
Syntax: object.KeyList(KeyNo)
KeyNo - value from 0 to (object.KeyCount-1)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanRecursive("Software\test1")
if IsOk=0 then
  response.write("Keys was not found")
else 'remove "Software\test1\test2" from the Key list
 MyObj.RemoveFromKeyList("Software\test1\test2")
 for i=0 to MyObj.KeyCount-1
  response.write(MyObj.KeyList(i))
  response.write("<br>")
 next
end if
MyObj.CleanKeyList()
set MyObj=Nothing
%>
ValueList Method
This method returns an item value from Value list by ValueNo index
Return Value: an item
Syntax: object.ValueList(ValueNo)
ValueNo - value from 0 to (object.ValueCount-1)

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.ValueScan("Software\test1\test2")
if IsOk=0 then
  response.write("Values was not found")
else  for i=0 to MyObj.ValueCount-1
  response.write(MyObj.ValueList(i))
  response.write("<br>")
 next
end if
MyObj.CleanValueList()
set MyObj=Nothing
%>
GetLastErrorNbr Method
Syntax: object.GetLastErrorNbr
Returns number of the last error.
GetLastErrorDscr Method
Syntax: object.GetLastErrorDscr
Returns description of the last error.
KeyCount Property
Indicates the number of keys in a Key list collection
Return Value: the number of keys
Syntax: object.KeyCount

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.KeyScanRecursive("Software\test1")
if IsOk=0 then
  response.write("Keys was not found")
else
  response.write(MyObj.KeyCount & " keys<br>")
end if
MyObj.CleanKeyList()
set MyObj=Nothing
%>
ValueCount Property
Indicates the number of values in a Value list collection
Return Value: the number of values
Syntax: object.ValueCount

Example:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicRegistry")
IsOk=MyObj.ValueScan("Software\test1\test2")
if IsOk=0 then
  response.write("Values was not found")
else
  response.write(MyObj.ValueCount & " values<br>")
end if
MyObj.CleanValueList()
set MyObj=Nothing
%>
top

 

Home | FAQ | News | How to buy | Contacts | ASP MagicPerf Component | ASP MagicShell Component