vbscript 从32位应用程序读取和写入64位注册表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript 从32位应用程序读取和写入64位注册表相关的知识,希望对你有一定的参考价值。


Option Explicit
 
'---------------------------------------------------
' Declared Constants 
'---------------------------------------------------
 
Const wbemFlagReturnImmediately = &h10 
Const wbemFlagForwardOnly = &h20
Const Success = 0
Const Failure = 1
Const HKEY_LOCAL_MACHINE = &H80000002
Const Read_REG_SZ = "GetStringValue"
Const Write_REG_SZ = "SetStringValue"
Const Read_REG_DWORD = "GetDWORDValue"
Const Write_REG_DWORD = "SetDWORDValue"
 
'---------------------------------------------------
' Declared Variables 
'---------------------------------------------------
 
Dim strResult, TextString, DwordValue
 
'---------------------------------------------------
' Parameters for Funktions ReadRegStr and WriteRegStr
'---------------------------------------------------
 
' Reads a REG_SZ and REG_DWORD value from the local computer's registry using WMI. 
' Parameters:
' Method - What type of value going to write (GetStringValue, SetStringValue, GetDWORDValue, SetDWORDValue)
' RootKey - The registry hive (HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG
' Key - The key that contains the desired value. 
' Value - The value that you want to get. 
' RegType - The registry bitness: 32 or 64
 
'---------------------------------------------------
' Create _TEST registry key in 64-bit Registry
'---------------------------------------------------
 
WScript.Echo "---------------------------------------------------"
WScript.Echo "Creating _TEST key in 64-bit Registry"
WScript.Echo "---------------------------------------------------"
 
strResult = CreateRegKey (HKEY_LOCAL_MACHINE, "Software\_TEST", 64)
 
If strResult = 0 Then
WScript.Echo "Able to Create Key : " & "HKEY_LOCAL_MACHINE\Software\_TEST"
Else
WScript.Echo "Not able to Create Key"
'WScript.Quit
End If
 
'---------------------------------------------------
' Set _TEST registry values in 64-bit Registry
'---------------------------------------------------
 
WScript.Echo "---------------------------------------------------"
WScript.Echo "Writing in 64-bit Registry"
WScript.Echo "---------------------------------------------------"
 
' Writing a string value
TextString = "Test of writing test string value"
strResult = WriteRegStr (Write_REG_SZ, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", TextString, 64)
 
If strResult = 0 Then
WScript.Echo "Able to Write Value : " & "SubKey1" & " = "& TextString
Else
WScript.Echo "Not able to Write Value"
'WScript.Quit
End If
 
' Writing a DWORD value
DwordValue = 1
strResult = WriteRegDword (Write_REG_DWORD, HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey2", DwordValue, 64)
 
If strResult = 0 Then
WScript.Echo "Able to Write Value : " & "SubKey2" & " = "& DwordValue
Else
WScript.Echo "Not able to Write Value"
'WScript.Quit
End If
 
'---------------------------------------------------
' Delete a SubKey value in 64-bit Registry
'---------------------------------------------------
 
WScript.Echo "---------------------------------------------------"
WScript.Echo "Delete SubKey1 in 64-bit Registry"
WScript.Echo "---------------------------------------------------"

strResult = DeleteSubKeyValue (HKEY_LOCAL_MACHINE, "Software\_TEST", "SubKey1", 64)
 
If strResult = 0 Then
WScript.Echo "Able to Delete SubKey value : " & "HKEY_LOCAL_MACHINE\Software\_TEST\SubKey1"  
Else
WScript.Echo "Not able to Delete SubKey value"
'WScript.Quit
End If
 
'---------------------------------------------------
' Delete _TEST registry key in 64-bit Registry
'---------------------------------------------------
 
WScript.Echo "---------------------------------------------------"
WScript.Echo "Delete _TEST key in 64-bit Registry"
WScript.Echo "---------------------------------------------------"
 
strResult = DeleteRegKey (HKEY_LOCAL_MACHINE, "Software\_TEST", 64)
 
If strResult = 0 Then
WScript.Echo "Able to Delete Key : " & "HKEY_LOCAL_MACHINE\Software\_TEST"
Else
WScript.Echo "Not able to Delete Key"
'WScript.Quit
End If
 
'---------------------------------------------------
' Function Create Registry Key
'---------------------------------------------------
 
Function CreateRegKey(RootKey, KeyPath, RegType)
 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
Dim strKeyPath, Return
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
 
Set oInParams = oReg.Methods_("CreateKey").InParameters 
oInParams.hDefKey = RootKey 
oInParams.sSubKeyName = KeyPath 
 
Set oOutParams = oReg.ExecMethod_("CreateKey", oInParams, , oCtx)
 
CreateRegKey = oOutParams.ReturnValue
 
set oCtx = Nothing 
set oLocator = Nothing 
 
End Function
 
'---------------------------------------------------
' Function Delete Registry Key
'---------------------------------------------------
 
Function DeleteRegKey(RootKey, KeyPath, RegType)
 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
Dim strKeyPath, Return
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
 
Set oInParams = oReg.Methods_("DeleteKey").InParameters 
oInParams.hDefKey = RootKey 
oInParams.sSubKeyName = KeyPath 
 
Set oOutParams = oReg.ExecMethod_("DeleteKey", oInParams, , oCtx)
 
DeleteRegKey = oOutParams.ReturnValue
 
wscript.echo 
 
set oCtx = Nothing 
set oLocator = Nothing 
 
End Function
 
'---------------------------------------------------
' Function Read Registry String
'---------------------------------------------------
 
Function ReadRegStr (Method, RootKey, Key, Value, RegType) 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType 
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv") 
 
Set oInParams = oReg.Methods_(Method).InParameters 
oInParams.hDefKey = RootKey 
oInParams.sSubKeyName = Key 
oInParams.sValueName = Value 
 
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx) 
 
ReadRegStr = oOutParams.sValue 
 
set oCtx = Nothing 
set oLocator = Nothing 
End Function
 
'---------------------------------------------------
' Function Write Registry String
'---------------------------------------------------
 
Function WriteRegStr (Method, RootKey, Key, ValueName, Value, RegType) 
 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType 
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv") 
 
Set oInParams = oReg.Methods_(Method).InParameters 
oInParams.hDefKey = RootKey 
oInParams.sSubKeyName = Key 
oInParams.sValueName = ValueName 
oInParams.sValue = Value 
 
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx) 
 
WriteRegStr = oOutParams.ReturnValue
 
Set oCtx = Nothing 
Set oLocator = Nothing 
 
End Function

'---------------------------------------------------
' Function Write Registry DWORD
'---------------------------------------------------
 
Function WriteRegDword (Method, RootKey, Key, ValueName, Value, RegType) 
 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType 
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv") 
 
Set oInParams = oReg.Methods_(Method).InParameters 
oInParams.hDefKey = RootKey 
oInParams.sSubKeyName = Key 
oInParams.sValueName = ValueName 
oInParams.uValue = Value 
 
Set oOutParams = oReg.ExecMethod_(Method, oInParams, , oCtx) 
 
WriteRegDword = oOutParams.ReturnValue
 
Set oCtx = Nothing 
Set oLocator = Nothing 
 
End Function

'---------------------------------------------------
' Function Delete Registry value
'---------------------------------------------------
 
Function DeleteSubKeyValue (RootKey, KeyPath, ValueName, RegType) 
 
Dim oCtx, oLocator, oReg, oInParams, oOutParams 
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet") 
oCtx.Add "__ProviderArchitecture", RegType 
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator") 
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv") 
 
Set oInParams = oReg.Methods_("DeleteValue").InParameters 
oInParams.hDefKey = RootKey
oInParams.sSubKeyName = KeyPath
oInParams.sValueName = ValueName
 
Set oOutParams = oReg.ExecMethod_("DeleteValue", oInParams, , oCtx) 
 
DeleteSubKeyValue = oOutParams.ReturnValue
 
Set oCtx = Nothing 
Set oLocator = Nothing 
 
End Function

以上是关于vbscript 从32位应用程序读取和写入64位注册表的主要内容,如果未能解决你的问题,请参考以下文章

从 32 位安装程序写入 64 位注册表

从 32 位应用程序读取 64 位注册表

32 位 pyodbc 读取 64 位访问 (accdb)

如何从 32 位进程读取 64 位注册表项?

Qt_32位程序在64位系统下读取64位注册表

Qt_32位程序在64位系统下读取64位注册表