Hatena::Groupmoz-addon

Ci.nsIZIGOROu

2007-08-08一度痛い目に遭った方がいい

COMの列挙

| 20:16 |  COMの列挙 - Ci.nsIZIGOROu を含むブックマーク はてなブックマーク -  COMの列挙 - Ci.nsIZIGOROu

そろそろビリヤードに行くのでソースだけ貼り付ける。

var inspect = function(obj, maxDepth, name, curDepth) {
    var crop = function(string, max) {
        string = string.match(/^(.+?)(\n|$)/m)[1];   
        max = max || 70;
        return string.length > max - 3 ? string.slice(0, max - 3) + "..." : string;
    };

    if (typeof name == "undefined") {
        name = "<" + typeof obj + ">";
    }
    if (typeof maxDepth == "undefined") {
        maxDepth = 0;
    }
    if (typeof curDepth == "undefined") {
        curDepth = 0;
    }
    if (typeof maxDepth != "undefined" && curDepth > maxDepth) {
        return;
    }

    var i = 0;
    
    for (var prop in obj) {
        try {
            i++;

            if (typeof obj[prop] == "object") {
                if (typeof obj.length != "undefined") {
                    WScript.Echo(name + "." + prop + "=[probably array, length " + obj[prop].length + "]");
                } 
                else {
                    WScript.Echo(name + "." + prop + "=[" + typeof obj[prop] + "]");
                }

                arguments.callee(obj[prop], maxDepth, name + "." + prop, curDepth + 1);
            }
            else if (typeof obj[prop] == "function") {
                WScript.Echo(name + "." + prop + "=[function]");
            }
            else {
                WScript.Echo(name + "." + prop + "=" + obj[prop]);
            }
        };
        catch (e) {
            WScript.Echo(name + "." + prop + " - Exception while inspecting.");
        }
    }
    if (!i) {
        WScript.Echo(name + " is empty");
    }
};

var Components_Class = function(name, number) {
    this.name = name;
    this.number = number;
};

Components_Class.prototype = {
    name: null,
    number: null,
    toString: function() {
        return this.name;
    }
};

var Components_Classes = function() {
};

var Components = {
    registry: null,
    classes: null,
    interfaces: null,
    init: function() {
        this.classes = new Components_Classes();

        this.registry = (new ActiveXObject("WbemScripting.SWbemLocator")).ConnectServer(null, "root\\default").Get("StdRegProv");

        var HKCR = 0x80000000;
        var subKeyName = "CLSID";
        var clsid = this.getRegistryKeys(HKCR, subKeyName);
        var regex = /(\.\d+)+$/;

        for (var i = 0, l = clsid.length; i < l; i++) {
            var progIDKey = ([subKeyName, clsid[i], "ProgID"]).join("\\");
            var progID = this.getRegistryStringValue(HKCR, progIDKey, null);

            this.classes[progID] = new Components_Class(progID, clsid[i]);

            if (regex.exec(progID)) {
                var sprogID = progID.replace(regex, "");
                this.classes[sprogID] = new Components_Class(sprogID, clsid[i]);
            }
        }
    },
    getRegistryStringValue: function(defKey, subKeyName, valueName) {
        var result = this.callRegistryMethod("GetStringValue", {
            hDefKey: defKey,
            sSubKeyName: subKeyName,
            sValueName: valueName
        });
        
        return result.sValue;
    },
    getRegistryKeys: function(defKey, subKeyName) {
        var result = this.callRegistryMethod("EnumKey", {
            hDefKey: defKey,
            sSubKeyName: subKeyName
        });

        return result.sNames.toArray();
    },
    callRegistryMethod: function(methodName, args) {
        var method = this.registry.Methods_.Item(methodName);
        var params = method.InParameters.SpawnInstance_();

        for (var name in args) {
            params[name] = args[name];
        }

        return this.registry.ExecMethod_(method.Name, params);
    },
    inspect: inspect
};

Components.init();
Components.inspect(Components.classes);