Consulting

Results 1 to 3 of 3

Thread: General VBA Registry Read

  1. #1
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location

    General VBA Registry Read

    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

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

    [/vba]

    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]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Stan,

    It returns a variant array, not an 8 paired Hex string. So just process the array.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    My bad! Should have realized that prior to posting... corrected vode

    [vba]
    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\ShutdownTi me"
    Set myWS = CreateObject("WScript.Shell")
    Rkey = myWS.RegRead(key)
    Cells(1, 1).Value = Hex2Date(Rkey) & " GMT"
    End Sub

    [/vba]

    Stan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •