在64位系统中,32位应用会遇到读取注册表发生重定向的问题。今天提供一个简单一点的思路。就是先获取系统架构,64位系统就去64位注册表,32位系统就去32位注册表读取,以此解决重定向的问题。代码其实写了很久了,在此做个记录吧。
Const HKEY_LOCAL_MACHINE = &H80000002
Sub 获取machineid()
Dim objCtx As Object, OSArch As Byte, objLocator As Object, objStdRegProv As Object, Inparams As Object, Outparams As Object
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
OSArch = GetOSArch '获取操作系统位数
objCtx.Add "__ProviderArchitecture", OSArch '这一句设置读取的注册表位置,设置64即为读取64位注册表
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objStdRegProv = objLocator.ConnectServer(".", "root\default", "", "", , , , objCtx).Get("StdRegProv")
Set Inparams = objStdRegProv.Methods_("GetStringValue").Inparameters
Inparams.Hdefkey = HKEY_LOCAL_MACHINE
Inparams.Ssubkeyname = "SOFTWARE\Microsoft\SQMClient"
Inparams.Svaluename = "MachineId"
Set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams, , objCtx)
MsgBox "MachineId:" & Outparams.SValue
Set Outparams = Nothing
Set Inparams = Nothing
Set objStdRegProv = Nothing
Set objLocator = Nothing
Set objCtx = Nothing
End Sub
Function GetOSArch() As Byte '获取系统是32位还是64位
GetOSArch = Val(GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture)
End Function
Views: 32