Quang_Hải
Thành viên gạo cội
- Tham gia
- 21/2/09
- Bài viết
- 6,050
- Được thích
- 7,948
- Nghề nghiệp
- Làm đủ thứ
Mình muốn thử code lấy ngày giờ từ Internet để cập nhật giờ hệ thống tự động.
Trong Win 7 hàm SetSystemTime của API không tác dụng nên mình viết thế này để dùng lệnh Time trong CMD, nhưng loay hoay mãi chưa code được
Trong Win 7 hàm SetSystemTime của API không tác dụng nên mình viết thế này để dùng lệnh Time trong CMD, nhưng loay hoay mãi chưa code được
PHP:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Sub TimeSetting()
ShellExecute 0, "runas", "cmd", Command, vbNullString, 1
End Sub
Function InternetTime(Optional GMT_Dif As Integer) As Date
Dim ServerURL As String, Res As String, NetDate As String
Dim NetTime As Date, LocalTime As Date
ServerURL = "http://www.timeanddate.com/worldclock/fullscreen.html?n=2"
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", ServerURL, 0, "", ""
.Send
If .ReadyState = 4 Then
Res = .getResponseHeader("Date")
Res = Mid(Res, 6, Len(Res) - 9)
NetDate = Left(Res, Len(Res) - 9)
NetTime = Right(Res, 8)
LocalTime = NetTime + (GMT_Dif / 24)
[A1] = DateValue(NetDate)
[A2] = TimeValue(LocalTime)
End If
End With
End Function
Sub Auto_Open()
InternetTime (7)
TimeSetting
End Sub