[archived content]

module dfl.registry;
public class DflRegistryException: DflException
{
public int errorCode;
}
public class Registry
{
public static RegistryKey classesRoot; [property getter]
public static RegistryKey currentConfig; [property getter]
public static RegistryKey currentUser; [property getter]
public static RegistryKey dynData; [property getter]
public static RegistryKey localMachine; [property getter]
public static RegistryKey performanceData; [property getter]
public static RegistryKey users; [property getter]
}
public enum RegistryHive;
public class RegistryKey
{
public final void close();
public final RegistryKey createSubKey(char[] name);
public final void deleteSubKey(char[] name);
public final void deleteSubKey(char[] name,bool throwIfMissing);
public final void deleteSubKeyTree(char[] name);
public final void deleteValue(char[] name,bool throwIfMissing);
public final void deleteValue(char[] name);
public final void flush();
public final char[][] getSubKeyNames();
public final RegistryValue getValue(char[] name);
public final RegistryValue getValue(char[] name,RegistryValue defaultValue);
public final char[][] getValueNames();
public final HKEY handle; [property getter]
public static final RegistryKey openRemoteBaseKey(RegistryHive hhive,char[] machineName);
public final RegistryKey openSubKey(char[] name,bool writeAccess);
public final RegistryKey openSubKey(char[] name);
public final int opEquals(Object o);
public final int opEquals(RegistryKey rk);
public final void setValue(char[] name,DWORD value);
public final void setValue(char[] name,RegistryValue value);
public final void setValue(char[] name,char[] value);
public final void setValue(char[] name,char[][] value);
public final int subKeyCount; [property getter]
public final int valueCount; [property getter]
}
public class RegistryValue
{
public char[] toString();
public DWORD valueType; [property getter]
}
public class RegistryValueBinary: RegistryValue
{
public char[] toString();
public void[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueDword: RegistryValue
{
public char[] toString();
public DWORD value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueDwordBigEndian: RegistryValue
{
public char[] toString();
public DWORD value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueExpandSz: RegistryValue
{
public char[] toString();
public char[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueLink: RegistryValue
{
public char[] toString();
public void[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueMultiSz: RegistryValue
{
public char[] toString();
public char[][] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueNone: RegistryValue
{
public char[] toString();
public void[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueResourceList: RegistryValue
{
public char[] toString();
public void[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}
public class RegistryValueSz: RegistryValue
{
public char[] toString();
public char[] value;
public DWORD valueType; [property getter]
[+] Inherited members
}

Example - or a minimalistic howto

The example shown here is very small, is currently alpha and is far from production quality. But based on my tests it does work quite well. TODO: Add return checks and proper exception handling where required. In this example we are manipulating a DWORD and a SZ value. If you want something more robust cite this example as a reference and refer to the dfl.registry documentation. The reader should have a basic knowledge of dfl and of the registry.

In order to manipulate or view the registry you need to open one of the standard hives:

rkey = reg.currentUser(); //I'm opening HKEY_CURRENT_USER here.

the variables rkey and reg has been declared as (subkey will be used later):

dfl.registry.Registry reg;
dfl.registry.RegistryKey? rkey, subkey;

If you would like to print all the sub keys of HKEY_CURRENT_USER you would do the following:

char[][] allEntries = rkey.getSubKeyNames();
foreach(entry; allEntries)
{
msgBox(entry);
}

You probably want to open another sub key, so you need to do the following:

subkey = rkey.openSubKey("CloneCD?", true);
Entries = subkey.getValueNames();

We have now opened HKEY_CURRENT_USER\CloneCD?. Listing the values in that key is simple in dfl, too. Lets assume we have only a set of null terminated strings (REG_SZ). We have to declare a new variable and a string to save our information:

char[] v="";

and we list all values using a foreach (like above):

foreach(entry; Entries)
{

to get the type of the current value (stored in variable entry) we have to do following:

val = subkey.getValue(entry);
v = getValueString(val.valueType());

getValueString is a custom method listed below, if Chris Miller wants to commit it to the current development version he is free to do so (I personally find it useful):

//FIXME: Implement enums [ie. enum RegKeyValueType? : uint { REG_SZ = 1, REG_EXPAND_SZ, REG_BINARY; } ... etc]
char[] getValueString(uint typeValue)
{
switch(typeValue)
{
case 1:
return "REG_SZ";
break;
case 2:
return "REG_EXPAND_SZ";
break;
case 3:
return "REG_BINARY";
break;
case 4:
return "REG_DWORD";
break;
case 5:
return "REG_DWORD_BIG_ENDIAN";
break;
case 6:
return "REG_LINK";
break;
case 7:
return "REG_MULTI_SZ";
break;
case 8:
return "REG_RESOURCE_LIST";
break;
case 9:
return "REG_FULL_RESOURCE_DESCRIPTOR";
break;
case 10:
return "REG_RESOURCE_REQUIREMENTS_LIST";
break;
case 11:
return "REG_QWORD";
break;
default:
return "Unknown";
}
}

And within the foreach loop it can be printed out in a nice dfl messagebox as following:

if(v == "REG_DWORD")
{
dval = cast(RegistryValueDword?)subkey.getValue(entry);
msgBox(std.string.format("d, type: %s", entry, cast(uint)dval.value, v));
//subkey.setValue(entry, 1337);
}
else
{
msgBox(std.string.format("s, type: %s", entry, val.toString(), v));
//subkey.setValue(entry, "holy shit...!");
}

In the above example / snippet you can see how to change values, too.

the entire foreach will be closed as:

}

and that's it. Hope you had fun playing with your registry.

Keep in mind that erroneous writes to the registry can prevent your system from functioning normally, always keep a backup handy.

Since this is not a whyto you can read more here about the registry: http://msdn.microsoft.com/en-us/library/ms724182.aspx

thanks for reading, d-k]

PS: Thanks to a_canadian_girl for proof reading and correcting this document.

Page last modified on November 07, 2008, at 07:27 AM