Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Registry Enumeration

Part of StandardLibraryCategory

Description

Use std.windows.registry to enumerate the members of the HKEY_CLASSES_ROOT\CLSID branch of the Windows registry that have a ProgID key.

(This example uses cleanStr to attempt to work around a buggy std.windows.registry. I think someone found the solution, now we just need Walter to apply the fix. See Issue #961)

Example

import std.stdio : writefln;
import std.stream : File, FileMode;
import std.utf : toUTF8, validate, UtfException;
import std.windows.registry;

pragma(lib, "advapi32.lib"); /* for WinAPI registry functions such as RegQueryValue, etc. */

void main()
{
    char[] cleanStr(char[] inStr)
    {
        /* 
        
        Remove null characters from the beginning or ending of the string 
        (any such null characters probably indicate bugs in std.windows.registry).
        
        */
        
        char[] tmpStr = inStr;
        if(tmpStr.length > 0)
        {
            if(tmpStr[0 .. 1] == "\x00")
                tmpStr = tmpStr[0 .. $ - 1];            
        }            
        if(tmpStr.length > 0)
        {
            if(tmpStr[$ - 1 .. $] == "\x00")
                tmpStr = tmpStr[1 .. $];
        }

        /* Try again in case there are more null characters*/
        if(tmpStr.length > 0)
        {
            if(tmpStr[0 .. 1] == "\x00")
                tmpStr = tmpStr[0 .. $ - 1];            
        }            
        if(tmpStr.length > 0)
        {
            if(tmpStr[$ - 1 .. $] == "\x00")
                tmpStr = tmpStr[1 .. $];
        }
        
        return tmpStr;
    }

    Key HKCR = Registry.classesRoot;
    Key CLSID = HKCR.getKey("CLSID");

    File outFile = new File(r"ProgIDs.txt", FileMode.Out);
    char[] keyDescript = "";

    foreach(Key key; CLSID.keys)
    {
        bool hasProgID = false;

        keyDescript = "CLSID: " ~ cleanStr(key.name()) ~ "\r\n";
        writefln(key.name());

        foreach(Value val; key.values)
        {
            try
            {
                validate(cleanStr(val.value_SZ()));
                keyDescript ~= "  (default) = " ~ cleanStr(val.value_SZ()) ~ "\r\n";
            }
            catch(UtfException)
                keyDescript ~= "  # UTF Problem #" ~ "\r\n";
            catch(Exception)
                keyDescript ~= "  # Error #" ~ "\r\n";

            foreach(Key key2; key.keys)
            {
                if(key2.name == "ProgID") hasProgID = true;
                if(key2.name == "InprocServer32" || key2.name == "ProgID" )
                {    
                    keyDescript ~= "    " ~ cleanStr(key2.name) ~ "\r\n";
                    foreach(Value val2; key2.values)
                    {
                        try
                        {
                            validate(cleanStr(val2.value_SZ()));
                            keyDescript ~= "      " ~ 
                              ((cleanStr(val2.name) == "") ? "(default)" : cleanStr(val2.name)) ~ 
                              " = " ~
                              cleanStr(val2.value_SZ()) ~ "\r\n";
                        }
                        catch(UtfException)
                            keyDescript ~= "      # UTF Problem #" ~ "\r\n";
                        catch(Exception)
                            keyDescript ~= "      # Error #" ~ "\r\n";
                    }
                }
            }
            keyDescript ~= "\r\n";
        }
        if(hasProgID) outFile.writeString(keyDescript);
    }
    outFile.close();
}

Compatibility

Tested with the Digital Mars D Compiler v1.0 on Windows 2000. This program requires Windows.