代码改变世界

用VB得到windows系统安装时的时间(for NT,2000,XP,..)

2005-04-21 11:27  电脑人生  阅读(356)  评论(0编辑  收藏  举报

Option Explicit

Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_DWORD = 4

Function GetWindowsNTInstallTime() As String
    On Error Resume Next
    Dim InstallDateValue As Long
    Dim hKey As Long
    Dim ret As String
    If RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Windows NT/CurrentVersion", hKey) = 0 Then
       If RegQueryValueEx(hKey, "InstallDate", 0&, REG_DWORD, InstallDateValue, 4) = 0 Then
          ret = CStr(InstallDateValue)
       End If
       If hKey Then RegCloseKey hKey
    End If
    If ret <> "" Then
        GetWindowsNTInstallTime = DateAdd("s", CLng(ret), "1970-01-01 00:00")
    Else
        GetWindowsNTInstallTime = "时间未知"
    End If
End Function

Private Sub Command1_Click()
    MsgBox "当前系统的安装时间为:" & GetWindowsNTInstallTime, vbInformation, ""
End Sub

'-------------------------------------------
' 转载请注明出处
' 作者:唐细刚
' 邮箱:tanaya@163.com
'-------------------------------------------