

var version = 101
var Native = null;
var guid = "{3DD12613-1A9C-48A6-8691-4CBA20BB7B31}";
var typeLibGuid = "{06237D6D-F2AF-4317-8DCD-3560C789D47F}";
var progId = "ProcessMonitor.ProcessMonitorGadget";
var dllPath = System.Gadget.path+"\\Release\\ProcessMonitor";  //32.dll added later

var DockedWidth = 125;
var DockedHeight = 60;
var UndockedWidth = 180;
var UndockedHeight = 86;

function Load()
{
    DockedChanged();

    LoadNative();
    Native.CheckVersion(version);
    Native.Localized = LocalizedFunction;
    Native.GadgetPath = System.Gadget.path+"\\";
    Native.Init();

    System.Gadget.onDock = DockedChanged;
    System.Gadget.onUndock = DockedChanged;
}

function Unload()
{
    if(Native != null)
        Native.Dispose();
    Native = null;
}

function DockedChanged()
{
    with(document.body.style)
        width = System.Gadget.docked ? DockedWidth : UndockedWidth,
        height = System.Gadget.docked ? DockedHeight : UndockedHeight;
}

function LocalizedFunction(name)
{
    return Localized[name];
}

function LoadNative()
{
    var firstError = "WScriptError: ";
    try
    {
        var wshShell = new ActiveXObject("WScript.Shell"); 
        try
        {
            RegisterNative(wshShell, "HKLM");
        }
        catch(err)
        {
            firstError = err.name + ": " + err.number + ", " + err.message + "<br/>";
            RegisterNative(wshShell, "HKCU");
        }
    }
    catch(err)
    {
        ErrorText.innerHTML = firstError + err.name + ": " + err.number + ", " + err.message;
        document.body.style.width = "400px";
        document.body.style.height = "200px";
    }
}

function RegisterNative(wshShell, root)
{
    var dllPost = "";
    if(System.Machine.processorArchitecture.indexOf("64") != -1)
        dllPost = "64.dll";
    else
        dllPost = "32.dll";
    wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\", progId);
    wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\CLSID\\", guid);
    wshShell.RegWrite(root + "\\Software\\Classes\\" + progId + "\\CurVer\\", progId + ".1");

    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\", progId);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\", dllPath+dllPost);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\ThreadingModel", "Apartment");
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\ProgID\\", progId + ".1");
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\TypeLib\\", typeLibGuid);
    wshShell.RegWrite(root + "\\Software\\Classes\\CLSID\\" + guid + "\\VersionIndependentProgID\\", progId);

    wshShell.RegWrite(root + "\\Software\\Classes\\TypeLib\\" + typeLibGuid + "\\1.0\\", progId + " - TypeLib");
    wshShell.RegWrite(root + "\\Software\\Classes\\TypeLib\\" + typeLibGuid + "\\1.0\\0\\win32\\", dllPath+dllPost);
    wshShell.RegWrite(root + "\\Software\\Classes\\TypeLib\\" + typeLibGuid + "\\1.0\\FLAGS\\", "0");
    wshShell.RegWrite(root + "\\Software\\Classes\\TypeLib\\" + typeLibGuid + "\\1.0\\HELPDIR\\", "");

/*    try
    {*/
        Native = new ActiveXObject(progId); 
/*    }
    finally
    {
        wshShell.RegDelete(root + "\\Software\\Classes\\" + progId + "\\CLSID\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\" + progId + "\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\InprocServer32\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\ProgId\\");
        wshShell.RegDelete(root + "\\Software\\Classes\\CLSID\\" + guid + "\\");
    }*/
}
