StdRegProv类所属方法的使用(之六)

13)        SetStringValue
为指定键值名称的键值设置键值类型为REG_SZ的键值数值。这种方法是最简单最常用的方法。

uint32 SetStringValue(
  [in]  uint32 hDefKey = 2147483650,
  [in]  string sSubKeyName,
  [in]  string sValueName,
  [in]  string sValue
);

例1:为键值名称为StringValue的键值赋予类型为REG_SZ的键值数据“www.swynk.com”,该键值属于HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子键
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer        = "."
sMethod                = "SetStringValue"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                strComputer & "/root/default:StdRegProv")

Set oMethod        = oRegistry.Methods_(sMethod)
Set oInParam        = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_LOCAL_MACHINE
oInParam.sSubKeyName = "SOFTWARE\MyCompany"
oInParam.sValueName = "StringValue"
oInParam.sValue = "www.swynk.com"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
    Wscript.Echo "String value added successfully"
Else
    Wscript.Echo "An error occurred"
End If


例2:本例直接调用SetStringValue。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer        = "."

hDefKey                = HKEY_LOCAL_MACHINE
sSubKeyName        = "SOFTWARE\MyCompany"
sValueName        = "StringValue"
sValue                = "www.swynk.com"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                sComputer & "/root/default:StdRegProv")

oOutParam = oRegistry.SetStringValue(hDefKey,sSubKeyName,sValueName,sValue)
WScript.Echo "The return code is : " & oOutParam


例3:相应的PS程序
$HKEY_LOCAL_MACHINE = "&H80000002"
$computer = "."
$namespace = "root\default"

$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }

$hDefKey = $HKEY_LOCAL_MACHINE
$sSubKeyName = "SOFTWARE\MyCompany"
$sValueName = "StringValue"
$sValue        = "www.swynk.com"

$oOutParam = $oRegistry.SetStringValue($hDefKey, $sSubKeyName, $sValueName, $sValue)
"Retrun Colde is : " + $oOutParam.ReturnValue


(14)        DeleteKey
删除指定路径下已存在的子键
uint32 DeleteKey(
  [in, optional]  uint32 hDefKey = 2147483650,
  [in]            string sSubKeyName
);

例1:删除HKEY_LOCAL_MACHINE\SOFTWARE路径下的子键“MyCompany”。本例使用ExecMethod_()调用DeleteKey。

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer        = "."
sMethod                = "DeleteKey"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                strComputer & "/root/default:StdRegProv")

Set oMethod        = oRegistry.Methods_(sMethod)
Set oInParam        = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_LOCAL_MACHINE
oInParam.sSubKeyName = "SOFTWARE\MyCompany"

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
    Wscript.Echo "SubKey deleted successfully"
Else
    Wscript.Echo "An error occurred"
End If


例2:本例直接调用DeleteKey方法
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer        = "."

hDefKey        = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                sComputer & "/root/default:StdRegProv")

oOutParam = oRegistry.DeleteKey(hDefKey,sSubKeyName)
WScript.Echo "The return code is : " & oOutParam


例3:相应的PS程序
$HKEY_LOCAL_MACHINE = "&H80000002"
$computer = "."
$namespace = "root\default"

$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }

$hDefKey = $HKEY_LOCAL_MACHINE
$sSubKeyName = "SOFTWARE\MyCompany"

$oOutParam = $oRegistry.DeleteKey($hDefKey, $sSubKeyName)
"Retrun Colde is : " + $oOutParam.ReturnValue


(15)        DeleteValue
删除指定子键的指定键值名称的键值。
uint32 DeleteValue(
  [in, optional]  uint32 hDefKey = 2147483650,
  [in]            string sSubKeyName,
  [in]            string sValueName
);

例1:删除HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子键中键值名为“ToBeDeleted”的键值。
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer        = "."
sMethod                = "DeleteValue"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                strComputer & "/root/default:StdRegProv")

Set oMethod        = oRegistry.Methods_(sMethod)
Set oInParam        = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = HKEY_LOCAL_MACHINE
oInParam.sSubKeyName = "SOFTWARE\MyCompany"
oInParam.sValueName = "ToBeDeleted"

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
    Wscript.Echo "Key Value is deleted successfully"
Else
    Wscript.Echo "An error occurred"
End If


例2:直接调用DeleteValue方法
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer        = "."

hDefKey                = HKEY_LOCAL_MACHINE
sSubKeyName        = "SOFTWARE\MyCompany"
sValueName        = "ToBeDeleted"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                sComputer & "/root/default:StdRegProv")

oOutParam = oRegistry.DeleteValue(hDefKey,sSubKeyName,sValueName)
WScript.Echo "The return code is : " & oOutParam


例3:相应的PS程序
$HKEY_LOCAL_MACHINE = "&H80000002"
$computer = "."
$namespace = "root\default"

$oRegistry = get-wmiobject -list -namespace $namespace -ComputerName $computer | where-object { $_.name -eq "StdRegProv" }

$hDefKey        = $HKEY_LOCAL_MACHINE
$sSubKeyName        = "SOFTWARE\MyCompany"
$sValueName        = "ToBeDeleted"

$oOutParam = $oRegistry.DeleteKey($hDefKey, $sSubKeyName)
"Retrun Colde is : " + $oOutParam.ReturnValue


(16)        CheckAccess
检查用户是否具有指定的权限。标准注册表提供程序的CheckAccess方法只检查当前帐户的权限级别,如果当前帐户没有适当的权限,该方法返回-1,否则返回0。
uint32 CheckAccess(
  [in, optional]  uint32 hDefKey = 2147483650,
  [in]            string sSubKeyName,
  [in]            uint32 lRequired,
  [out]           bool bGranted
);

例1:执行下面脚本检查当前帐户是否具有“set”权限。
        cscript /nologo CheckAccess.vbs set

Const KEY_QUERY_VALUE                         = &H0001
Const KEY_SET_VALUE                         = &H0002
Const KEY_CREATE_SUB_KEY                                 = &H0004
Const KEY_ENUMERATE_SUB_KEYS                         = &H0008
Const KEY_NOTIFY                                 = &H0010
Const KEY_CREATE_LINK                         = &H0020
Const DELETE                                 = &H00010000
Const READ_CONTROL                         = &H00020000
Const WRITE_DAC                                 = &H00040000
Const WRITE_OWNER                         = &H00080000
Const HKEY_LOCAL_MACHINE                 = &H80000002

sComputer        = "."
sMethod                = "CheckAccess"
hTree                = HKEY_LOCAL_MACHINE
sKey                = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\test"

Set oRegistry        = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                sComputer & "/root/default:StdRegProv")

Set oMethod        = oRegistry.Methods_(sMethod)
Set oInParam        = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey

sAccess = WScript.Arguments(0)

Select Case sAccess
        Case "query"                hAccess = KEY_QUERY_VALUE
        Case "set"                hAccess = KEY_SET_VALUE
        Case "createsub"                hAccess = KEY_CREATE_SUB_KEY
        Case "enumsub"                hAccess = KEY_ENUMERATE_SUB_KEYS
        Case "notify"                hAccess = KEY_NOTIFY
        Case "createlink"                hAccess = KEY_CREATE_LINK
        Case "delete"                hAccess = DELETE
        Case "readcontrol"                        hAccess = READ_CONTROL
        Case "writedac"                hAccess = WRITE_DAC
        Case "writeowner"                hAccess = WRITE_OWNER
End Select

oInParam.uRequired = hAccess
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
'WScript.Echo oOutParam.Properties_("bGranted")
'If the current user does not have appropriate permissions,the method returns -1
'and 0 otherwise
If oOutParam.Properties_("bGranted") = 0 Then
WScript.Echo "Current user has " & Chr(34) & UCase(sAccess) & Chr(34) & " registry permissions"
Else
WScript.Echo "Current user does not have " & Chr(34) & UCase(sAccess) & chr(34) & " registry permissions"
End If

 

本文转载自 http://bbs.winos.cn/viewthread.php?tid=71146

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章