Dai

Dai

  • NA
  • 1
  • 0

Last Write Time of a Registry key

Apr 10 2009 5:48 AM
Hey guys, I've had a look around and I can't quite make sense of trying to get the last write time of a registry key in vb.net. Im a bit of a beginner in vb.net but I really have done an exhaustive search and cant find anything I can make any sense of.

I can open subkeys and get values using .GetValue("ValueName"), but as last write time isnt a value inside the key, this doesn't help here.

I have had a look around and it seems in VB6 RegOpenKeyEx was used (to open the key and get a handle), then RegQueryInfoKey was used to get the last write time (of the subkey via its handle) BUT i cant get this to work.

I have tried entering the following code:

Module moduleMountTimes
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const REG_SZ = 1
    Public Const DELETE = &H10000
    Public Const READ_CONTROL = &H20000
    Public Const SYNCHRONIZE = &H100000
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const STANDARD_RIGHTS_READ = READ_CONTROL
    Public Const ERROR_SUCCESS = 0&
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const KEY_NOTIFY = &H10
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))

    Public Structure FILETIME
        Dim dwLowDateTime As Integer
        Dim dwHighDateTime As Integer
    End Structure

    Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, _
       ByVal ulOptions As Integer, ByVal samDesired As Integer, ByVal phkResult As Integer) As Integer

    Public Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hCurKey As Integer, ByVal lpClass As String, _
       ByVal lpcbClass As Integer, ByVal lpReserved As Integer, ByVal lpcSubKeys As Integer, ByVal lpcbMaxSubKeyLen As Integer, _
       ByVal lpcbMaxClassLen As Integer, ByVal lpcValues As Integer, ByVal lpcbMaxValueNameLen As Integer, ByVal lpcbMaxValueLen As Integer, _
       ByVal lpcbSecurityDescriptor As Integer, ByVal lpftLastWriteTime As FILETIME) As Integer

    Public Declare Function FromFileTime Lib "mscorlib.dll" Alias "FromFileTimeA" (ByVal fileTime As Integer) As DateTime
End Module





I pieced together this code from a few places on the net, keeping in mind that all of the Integer values were listed as LONG in VB6 code. I found something somewhere telling me that changing these LONGs over to Integers would solve my problem but no luck for me!


With the following call, I cannot even get an error_success from OPENING a key, let alone getting the last write time:

Dim handleOpenKey As Integer
If (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM", 0, KEY_READ, handleOpenKey)) = ERROR_SUCCESS Then
            MessageBox.Show("ERROR_SUCCESS")
Else
            MessageBox.Show("NOT ERROR_SUCCESS")
End If


SYSTEM isnt the key I want, but if I can get it to work simply for SYSTEM i can obviously figure the rest out from there.

The code I have used to call the RegQueryInfoEx is as follows:

Dim lastWriteTimeFT As FILETIME

If (RegQueryInfoKey(hOpenKey, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, lastWriteTimeFT)) = ERROR_SUCCESS Then
        MessageBox.Show("ERROR_SUCCESS")
Else
        MessageBox.Show("NOT ERROR_SUCCESS")
End If



Now I have read that in vb.net this has all been "replaced" by the Microsoft.Win32.Registry ...erm... "stuff" and even though I'm using the .GetValue method of getting values in a key, I cant seem to see anything in the .Registry class that deals with last write times?


Basically, I cant find any info in vb.net to get the last write time, and I cant get the VB6 way of doing it to work. Could anyone help?

Thanks,
Dai