用VB取得操作系统安装的时间

用VB取得操作系统安装的时间
 
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


'**************************************************************************************************************
'     函数名称:
'        ----------     GetWindowsInstallDateTime
'     功能描述:
'        ----------     获得操作系统安装的日期时间
'     传入参数:
'        ----------      无
'     返回值:
'        ----------      无
'     修改历史记录:
'**************************************************************************************************************


Function GetWindowsInstallDateTime() As String
   
    On Error Resume Next
    Dim InstallDateValue As Long
    Dim hKey As Long
  
   
 
    If RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Windows NT/CurrentVersion", hKey) = 0 Then '打开该项的
      
       If RegQueryValueEx(hKey, "InstallDate", 0&, REG_DWORD, InstallDateValue, 4) = 0 Then '获取值成功
            If InstallDateValue > 0 Then
                 GetWindowsInstallDateTime = DateAdd("s", InstallDateValue, "1970-01-01 00:00")
            Else
                 GetWindowsInstallDateTime = "时间未知"
            End If
       End If
       If hKey Then RegCloseKey hKey  '关闭打开的项
    End If
   
End Function

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

你可能感兴趣的:(Others)