花点时间,写了NetRemoteTOD方式的完整代码。改变_NetRemoteTOD返回类型,方便au3 UDF直接调用。同net time一样,本地账户必须有远程服务器的网络登录权限,服务器必须开Server服务。代码在WinXP/2003下测试通过。#include <Date.au3>
Opt("MustDeclareVars", 1)
Opt("TrayAutoPause", 0)
Opt("TrayIconDebug", 1)
Local $sServer = "192.168.0.1"
If TimeSync($sServer) Then
MsgBox(0x40, "提示", "与服务器 " & $sServer & " 时间同步成功。" & @CRLF & "当前时间: " & _Date_Time_SystemTimeToDateTimeStr(_Date_Time_GetLocalTime()))
Else
MsgBox(0x30, "错误", "与服务器 " & $sServer & " 时间同步失败!" & @CRLF & "当前时间: " & _Date_Time_SystemTimeToDateTimeStr(_Date_Time_GetLocalTime()))
EndIf
Exit
Func TimeSync($sServer)
Local $tSystemTime
If Ping($sServer, 250) = 0 Then Return 0
$tSystemTime = _NetRemoteTOD($sServer)
If @error Then Return 0
If _Date_Time_SetSystemTime(DllStructGetPtr($tSystemTime)) Then
Return 1
Else
Return 0
EndIf
EndFunc ;==>TimeSync
;===============================================================================
; 说明: 获取以 UTC 表示的指定服务器日期和时间信息
; 语法: _NetRemoteTOD($sServer)
; 参数: $sServer - 计算机名称
; 需要: <Date.au3>
; 返回: 成功 - 包含服务器日期和时间的 $tagSYSTEMTIME 结构体
; 失败 - 0, 并设置 @error
; 备注: @@MsdnLink@@ NetRemoteTOD
;===============================================================================
Func _NetRemoteTOD($sServer)
Local $aResult, $tTIME_OF_DAY_INFO, $tSYSTEMTIME
Local Const $tagTIME_OF_DAY_INFO = _
"dword tod_elapsedt;dword tod_msecs;" & _
"dword tod_hours;dword tod_mins;" & _
"dword tod_secs;dword tod_hunds;" & _
"long tod_timezone;dword tod_tinterval;" & _
"dword tod_day;dword tod_month;" & _
"dword tod_year;dword tod_weekday"
$aResult = DllCall("netapi32.dll", "int", "NetRemoteTOD", "wstr", $sServer, "ptr*", 0)
If @error Then Return SetError(@error, 0, 0)
If $aResult[0] <> 0 Then Return SetError($aResult[0], 0, 0)
$tTIME_OF_DAY_INFO = DllStructCreate($tagTIME_OF_DAY_INFO, $aResult[2])
$tSYSTEMTIME = _Date_Time_EncodeSystemTime( _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_month"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_day"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_year"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_hours"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_mins"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_secs"), _
DllStructGetData($tTIME_OF_DAY_INFO, "tod_hunds") * 10)
DllCall("netapi32.dll", "int", "NetApiBufferFree", "ptr", $aResult[2])
Return $tSYSTEMTIME
EndFunc ;==>_NetRemoteTOD
|