기 타 Copilot 이용하여 시간동기화 프로램 만들기(Autoit)
2024.08.10 13:46
; NTP 서버 주소
Global $sNTPServer = "time.windows.com"
; NTP 서버로부터 시간을 가져오는 함수
Func _GetNTPTime($sServer)
Local $iSocket = UDPStartup()
Local $iUDP = UDPOpen($sServer, 123)
Local $aNTPPacket[12] = [0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Local $tNTPPacket = DllStructCreate("byte[48]")
DllStructSetData($tNTPPacket, 1, $aNTPPacket)
UDPSend($iUDP, DllStructGetPtr($tNTPPacket), 48)
Local $tResponse = DllStructCreate("byte[48]")
UDPRecv($iUDP, DllStructGetPtr($tResponse), 48)
UDPCloseSocket($iUDP)
UDPShutdown()
Local $iSeconds = DllStructGetData($tResponse, 1, 43) * 256 + DllStructGetData($tResponse, 1, 44)
$iSeconds = $iSeconds * 256 + DllStructGetData($tResponse, 1, 45)
$iSeconds = $iSeconds * 256 + DllStructGetData($tResponse, 1, 46)
Return $iSeconds - 2208988800
EndFunc
; 시스템 시간을 설정하는 함수
Func _SetSystemTime($iSeconds)
Local $tSystemTime = DllStructCreate("ushort[8]")
Local $iTime = _Date_Time_EncodeFileTime($iSeconds)
DllStructSetData($tSystemTime, 1, $iTime[0])
DllStructSetData($tSystemTime, 2, $iTime[1])
DllStructSetData($tSystemTime, 3, $iTime[2])
DllStructSetData($tSystemTime, 4, $iTime[3])
DllStructSetData($tSystemTime, 5, $iTime[4])
DllStructSetData($tSystemTime, 6, $iTime[5])
DllStructSetData($tSystemTime, 7, $iTime[6])
DllStructSetData($tSystemTime, 8, $iTime[7])
DllCall("kernel32.dll", "int", "SetSystemTime", "ptr", DllStructGetPtr($tSystemTime))
EndFunc
; 메인 함수
Func _Main()
Local $iNTPTime = _GetNTPTime($sNTPServer)
_SetSystemTime($iNTPTime)
EndFunc
_Main()
수고하셨습니다.