VBS写入较长的二进制注册表键值的方法

来源:互联网 发布:centos 7.2关闭防火墙 编辑:程序博客网 时间:2024/06/05 23:21

举个例子

Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]"ShellState"=hex:24,00,00,00,73,a0,01,00,00,00,00,00,00,00,00,00,00,00,00,00,\  01,00,00,00,0d,00,00,00,00,00,00,00,00,00,00,00

VBS似乎只能写较短的二进制代码,无法写入较长的。比如

set r=wscript.createobject("wscript.shell")r.regwrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ShellState",&H2401aaff,"REG_BINARY"

这段代码可以写入,但是如果在增加的话,就会出现错误。

较长的二进制可通过WMI可以实现,代码如下

'Define constantConst HKEY_CR = &H80000001 'HKEY_CURRENT_USER常量,具体参看本章最后附录部分Const REG_BINARY=3 '二进制类型'string varablestrComputer = "."strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer"strValue = "ShellState"'binary arrayDim arrDataarrData=Array(&H24,&H00,&H00,&H00,&H73,&Ha0,&H01,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H01, &H00,&H00,&H00,&H0d,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00,&H00)'registry objectSet objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")'create registry keyobjRegistry.CreateKey HKEY_CR, strKeyPath'setup the binary keyvalueretcode = objRegistry.SetBinaryValue(HKEY_CR, strKeyPath, strValue, arrData)'sample error handleIf (retcode = 0) And (Err.Number = 0) Then  WScript.Echo "Binary value added successfully"Else  ' An error occurred  WScript.Echo "An error occurred. Return code: " & retcodeEnd If

补充一下,使用WMI的StdRegProv类对注册表操作时,需要用到的几个常量

Const HKEY_CURRENT_USER = &H80000001Const HKEY_LOCAL_MACHINE = &H80000002Const HKEY_USERS = &H80000003Const HKEY_CURRENT_CONFIG = &H80000005Const HKEY_DYN_DATA = &H80000006Const REG_SZ = 1Const REG_EXPAND_SZ = 2Const REG_BINARY = 3Const REG_DWORD = 4Const REG_MULTI_SZ = 7