PDA

View Full Version : General VBA Registry Read



stanl
03-17-2009, 05:50 AM
I'm playing around with a function to convert a REG BINARY value that represents a date into an actual date and account for the GMT bias. I chose the windows ShutDown as a start. The first step would be to read the registry key - which should return an 8-paired hex string

However, this simple sub I wrote for Excel


Sub getshutdown()
Dim key As String
Dim myWS As Object
key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Windows\ShutdownTime"
Set myWS = CreateObject("WScript.Shell")
Rkey = myWS.RegRead(key)
Cells(1, 1).Value = Rkey
End Sub



returns a numeric value - 68.

Either I have misrepresented the key or something else is wrong.

Stan

P.S. what is interesting about the value returned is it has to be manipulated from 1/1/1601 [base date]

Bob Phillips
03-17-2009, 06:23 AM
Stan,

It returns a variant array, not an 8 paired Hex string. So just process the array.

stanl
03-17-2009, 07:02 AM
My bad! Should have realized that prior to posting... corrected vode


Function Hex2Date(aD)
Term = aD(7) * (2 ^ 56) + aD(6) * (2 ^ 48) + aD(5) * (2 ^ 40) + aD(4) * (2 ^ 32) + aD(3) * (2 ^ 24) + aD(2) * (2 ^ 16) + aD(1) * (2 ^ 8) + aD(0)
Days = Term / (1E7 * 86400)
sDate = DateSerial(1601, 1, 1) + Days
Hex2Date = sDate
End Function

Sub getshutdown()
Dim key As String
Dim myWS As Object
key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Windows\ShutdownTime"
Set myWS = CreateObject("WScript.Shell")
Rkey = myWS.RegRead(key)
Cells(1, 1).Value = Hex2Date(Rkey) & " GMT"
End Sub



Stan