用WMI操作注册表

REG.HTA
==========================================



用WMI操作注册表






 

  

  

 

 


 

 



wmi_reg.js
=========================================


var HKCR = 0x80000000; //HKEY_CLASSES_ROOT
var HKCU = 0x80000001; //HKEY_CURRENT_USER
var HKLM = 0x80000002; //HKEY_LOCAL_MACHINE
var HKUS = 0x80000003; //HKEY_USERS
var HKCC = 0x80000005; //HKEY_CURRENT_CONFIG

var HKEY_CLASSES_ROOT  = 0x80000000;
var HKEY_CURRENT_USER  = 0x80000001;
var HKEY_LOCAL_MACHINE  = 0x80000002;
var HKEY_USERS   = 0x80000003;
var HKEY_CURRENT_CONFIG = 0x80000005;

var REG_SZ = 1;
var REG_EXPAND_SZ = 2;
var REG_BINARY = 3;
var REG_DWORD = 4;
var REG_MULTI_SZ = 7;

function WMI(){} //WMI Object

//********************************************************
function EnumMethods(MethodName, sRoot, sRegPath)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item(MethodName);
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;
    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
    return oOutParam; //返回输出参数对象
}
function GetMethods(MethodName, sRoot, sRegPath, strValueName)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item(MethodName);
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;
    oInParam.sValueName = strValueName;

    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
    return oOutParam; //返回输出参数对象
}
function SetMethods(MethodName, sRoot, sRegPath, strValueName, xValue)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item(MethodName);
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;
    oInParam.sValueName = strValueName;
    //oInParam.sValue = xValue;
    if (typeof(xValue) == "object") {
 if(typeof(xValue[0]) == "string") oInParam.sValue = xValue;
 else oInParam.uValue = xValue;
    } else {
 if (typeof(xValue) == "string") oInParam.sValue = xValue;
 else oInParam.uValue = xValue;
    }

    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
}
//************************************************

//枚举注册表值和类型
WMI.prototype.EnumValues = function(sRoot, sRegPath)
{
    var oOutParam = EnumMethods("EnumValues", sRoot, sRegPath);

    if (oOutParam.sNames!=null){
 var o = new Object();
 o["Names"] = oOutParam.sNames.toArray();
 o["Types"] = oOutParam.Types.toArray();
 return o;
    } else { return null;}
};

//枚举子项
WMI.prototype.EnumKey = function(sRoot, sRegPath)
{
    var oOutParam = EnumMethods("EnumKey", sRoot, sRegPath);
   
    if (oOutParam.sNames==null) return null;
    else return oOutParam.sNames.toArray();
}

//读取字符串
WMI.prototype.GetStringValue = function(sRoot, sRegPath, strValueName)
{
    var oOutParam = GetMethods("GetStringValue", sRoot, sRegPath, strValueName);
   
    return oOutParam.sValue; //注意是sValue
}

//读取DWORD值
WMI.prototype.GetDWORDValue = function(sRoot, sRegPath, strValueName)
{
    var oOutParam = GetMethods("GetDWORDValue", sRoot, sRegPath, strValueName);

    return oOutParam.uValue; //注意是uValue
}

//读取扩展的字符串值
WMI.prototype.GetExpandedStringValue = function(sRoot, sRegPath, strValueName)
{
    var oOutParam = GetMethods("GetExpandedStringValue", sRoot, sRegPath, strValueName);

    return oOutParam.sValue; //注意是sValue
}

//读取 MultiString 值
WMI.prototype.GetMultiStringValue = function(sRoot, sRegPath, strValueName)
{
    var oOutParam = GetMethods("GetMultiStringValue", sRoot, sRegPath, strValueName);

    return oOutParam.sValue.toArray();
}

//读取二进制注册表值
WMI.prototype.GetBinaryValue = function(sRoot, sRegPath, strValueName)
{
    var oOutParam = GetMethods("GetBinaryValue", sRoot, sRegPath, strValueName);

    return oOutParam.uValue.toArray(); //注意是uValue
}


//创建扩展的字符串值
WMI.prototype.SetExpandedStringValue = function(sRoot, sRegPath, strValueName, strValue)
{
    SetMethods("SetExpandedStringValue", sRoot, sRegPath, strValueName, strValue)
}

//创建字符串
WMI.prototype.SetStringValue = function(sRoot, sRegPath, strValueName, strValue)
{
    SetMethods("SetStringValue", sRoot, sRegPath, strValueName, strValue)
}

//创建DWORD值
WMI.prototype.SetDWORDValue = function(sRoot, sRegPath, strValueName, dwValue)
{
    SetMethods("SetDWORDValue", sRoot, sRegPath, strValueName, dwValue)
}

//创建 MultiString 值
WMI.prototype.SetMultiStringValue = function(sRoot, sRegPath, strValueName, aValue)
{
    SetMethods("SetMultiStringValue", sRoot, sRegPath, strValueName, aValue)
}
//创建 MultiString 值
WMI.prototype.SetBinaryValue = function(sRoot, sRegPath, strValueName, aValue)
{
    SetMethods("SetBinaryValue", sRoot, sRegPath, strValueName, aValue)
}

//创建注册表项
//sRegPath串,可以一次创建完整的项子树(各级不存在也会被创建)
WMI.prototype.CreateKey = function(sRoot, sRegPath)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item("CreateKey");
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;

    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
}

//删除注册表项
//只能删除叶结点项(如果该项下还有子项则不能删除,无效果)
//删除所指定的但并不存在子项无效果
//删除指定的子项时,连同该子项下的所有值均被删除
WMI.prototype.DeleteKey = function(sRoot, sRegPath)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item("DeleteKey");
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;

    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
}

//删除注册表值
WMI.prototype.DeleteValue = function(sRoot, sRegPath, strValueName)
{
    var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
    var oSvc = oLoc.ConnectServer(null, "root//default");
    var oReg = oSvc.Get("StdRegProv");

    var oMethod = oReg.Methods_.Item("DeleteValue");
    var oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = sRoot;
    oInParam.sSubKeyName = sRegPath;
    oInParam.sValueName = strValueName;
    var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
}

你可能感兴趣的:(用WMI操作注册表)