StdRegProv類所屬方法的使用(之四)

(07)        GetMultiStringValue
返回鍵值類型爲REG_MULTI_SZ的指定鍵值名稱的鍵值數值
uint32 GetMultiStringValue(
  [in, optional]  uint32 hDefKey = 2147483650,
  [in]            string sSubKeyName,
  [in]            string sValueName,
  [out]          string sValue[]
);

例1:讀取HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\System下鍵值名爲Sources的鍵值數值。這個鍵值數值是REG_MULTI_SZ 類型的,它是一個以空格相隔的數組。本例直接調用GetMultiStringValue。
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
strValueName = "Sources"

Return = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,_
    strValueName,arrValues)
If (Return = 0) And (Err.Number = 0) Then  
' Treat the multistring value as a collection of strings separated by spaces and output
  For Each strValue In arrValues
    WScript.Echo  strValue
  Next
Else
    Wscript.Echo "GetMultiStringValue failed. Error = " & Err.Number
End If


例2:本例使用ExecMethod_()調用GetMultiStringValue。
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
sMethod                = "GetMultiStringValue"

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 = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
oInParam.sValueName = "Sources"
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

If (oOutParam.ReturnValue = 0) And (Err.Number = 0) Then
  For iCount = 0 To UBound(oOutParam.Properties_("sValue"))
        WScript.Echo oOutParam.Properties_("sValue")(iCount)
  Next
Else
  Wscript.Echo "GetMultiStringValue failed. Error = " & Err.Number
End If


例3:相應的PS程序
$computer = "."
$namespace = "root\default"

$hDefKey = "&H80000002"
$strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
$strValueName = "Sources"

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

$oOutParams = $oRegistry.GetMultiStringValue($hDefKey, $strKeyPath, $strValueName)
If ($oOutParams.returnvalue -eq 0)
{
  Foreach($oOutParam in $oOutParams)
  {
    $oOutParam.sValue
  }
}
Else
{
  "GetMultiStringValue failed."
}


(08)        CreateKey
在指定註冊表路徑下創建一個子鍵。
uint32 CreateKey(
  [in]   uint32 hDefKey = 2147483650,
  [in]   string sSubKeyName,
);

例1:在註冊表HKEY_LOCAL_MACHINE\SOFTWARE路徑下創建名爲MyCompany的子鍵。此例是直接調用CreateKey。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer = "."
hDefKey = HKEY_LOCAL_MACHINE
sSubKeyName = "SOFTWARE\MyCompany"

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

ReturnCode = oRegistry.CreateKey(hDefKey, sSubKeyName)
WScript.Echo ReturnCode


例2:此例是通過ExecMethod_()調用CreateKey方法。
Const HKEY_LOCAL_MACHINE         = &H80000002
sComputer = "."
sMethod        = "CreateKey"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\MyCompany"

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

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)


例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-1"

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


(09)        SetBinaryValue
爲指定鍵值名稱的鍵值設置鍵值類型爲REG_BINARY的鍵值數值。
uint32 SetBinaryValue(
  [in]   uint32 hDefKey = 2147483650,
  [in]   string sSubKeyName,
  [in]   string sValueName,
  [in]   uint8  uValue[]
);

例1:爲鍵值名稱爲BinValue的鍵值賦予類型爲REG_BINARY的鍵值數值“ff 01 01 01”。該鍵值屬於HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany子鍵。

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

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 = "BinValue"
oInParam.uValue = Array("255","1","1", "1")
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

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

注意uValue是一個字符串數組,每個元素是一個0-255的整數,表示一個單一字節。下圖顯示正常執行後的結果。

例2:直接調用SetBinaryValue。
Const HKEY_LOCAL_MACHINE = &H80000002
sComputer        = "."

hTree                = HKEY_LOCAL_MACHINE
sKey                = "SOFTWARE\MyCompany"
sValueName        = "BinValue"
aValue                = Array("255","1","1","1")

Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
                sComputer & "/root/default:StdRegProv")
ReturnCode = oRegistry.SetBinaryValue(hTree, sKey, sValueName, aValue)
If (ReturnCode = 0) And (Err.Number = 0) Then
    Wscript.Echo "Binary value added successfully"
Else
    Wscript.Echo "An error occurred"
End If


例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 = "BinValue"
$uValue = ("255","1","1", "1")

$ReturnCode = $oRegistry.SetBinaryValue($hDefKey, $sSubKeyName, $sValueName, $uValue)
"Retrun Colde is : " + $ReturnCode.ReturnValue

 

本文轉載自 http://bbs.winos.cn/viewthread.php?tid=71064

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