Jump to content

Problem with _Date_Time_SetSystemTimeAdjustment


Recommended Posts

Hello,

I have tested the following script on few operating systems:

#include <Date.au3>
#include <WinAPI.au3>

; Under Vista the Windows API "SetSystemTimeAdjustment" may be rejected due to system security

_Main()

Func _Main()
    Local $aInfo

    ; Open the clock so we can watch the fun
    Run("RunDll32.exe shell32.dll,Control_RunDLL timedate.cpl")
    WinWaitActive("[CLASS:#32770]")

    ; Get current time adjustments
    $aInfo = _Date_Time_GetSystemTimeAdjustment()

    ; Slow down clock
    If Not _Date_Time_SetSystemTimeAdjustment($aInfo[1] / 10, False) Then
        MsgBox(4096, "Error", "System clock cannot be DOWN" & @CRLF & @CRLF & _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
    MsgBox(4096, "Information", "Slowing down system clock", 2)

    Sleep(5000)

    ; Speed up clock
    If Not _Date_Time_SetSystemTimeAdjustment($aInfo[1] * 10, False) Then
        MsgBox(4096, "Error", "System clock cannot be UP" & @CRLF & @CRLF & _WinAPI_GetLastErrorMessage())
    EndIf
    MsgBox(4096, "Information", "Speeding up system clock", 2)

    Sleep(5000)

    ; Reset time adjustment
    If Not _Date_Time_SetSystemTimeAdjustment($aInfo[1], True) Then
        MsgBox(4096, "Error", "System clock cannot be RESET" & @CRLF & @CRLF & _WinAPI_GetLastErrorMessage())
    Else
        MsgBox(4096, "Information", "System clock restored")
    EndIf

EndFunc   ;==>_Main

and I get the following error: "Not all privileges or groups referenced are assigned to the caller."

This is an example of Helpfile and I tested on : Windows 2000, Windows XP x86 and X64. and Windows Vista x64 logged as user or administrator.

Can anyone help me solve this problem?

Thanks in advance.

Link to comment
Share on other sites

From MSDN: SetSystemTimeAdjustment Function:

An application must have system-time privilege (the SE_SYSTEMTIME_NAME privilege) for this function to succeed. The SE_SYSTEMTIME_NAME privilege is disabled by default. Use the AdjustTokenPrivileges function to enable the privilege before calling SetSystemTimeAdjustment, and then to disable the privilege after the SetSystemTimeAdjustment call.

:(

P.S. See _Security_SetPrivilege() in help file. :)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks @PsaltyDS,

but function _Date_Time_SetSystemTimeAdjustment is already doing this.

; #FUNCTION# ====================================================================================================================
; Name...........: _Date_Time_SetSystemTimeAdjustment
; Description ...: Enables or disables periodic time adjustments to the system's time of day clock
; Syntax.........: _Date_Time_SetSystemTimeAdjustment($iAdjustment, $fDisabled)
; Parameters ....: $iAdjustment - The number of 100 nanosecond units added to the time of day clock at each clock interrupt if
;                  +periodic time adjustment is enabled.
;                  $fDisabled   - A value of True specifies that periodic time  adjustment is to be disabled.  The system is free
;                  +to adjust time of day using its own internal mechanisms.  The system's internal adjustment mechanisms may
;                  +cause the time-of-day clock to jump noticeably when adjustments are made.  A value of False specifies that
;                  +periodic time adjustment is to be enabled, and will be used to adjust the time of day clock.  The system will
;                  +not interfere with the time adjustment scheme, and will not attempt to synchronize time of day on its own.
;                  +The system will add the value of iAdjustment to the time of day at each clock interrupt.
; Return values .: Success      - True
;                  Failure      - False
; Author ........: Paul Campbell (PaulIA)
; Modified.......: Gary Frost (gafrost)
; Remarks .......:
; Related .......: _Date_Time_GetSystemTimeAdjustment
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _Date_Time_SetSystemTimeAdjustment($iAdjustment, $fDisabled)
    ; Enable system time privileged mode
    Local $hToken = _Security__OpenThreadTokenEx(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
    If @error Then Return SetError(@error, @extended, False)
    _Security__SetPrivilege($hToken, "SeSystemtimePrivilege", True)
    Local $iError = @error
    Local $iLastError = @extended
    Local $iRet = False
    If Not @error Then
        ; Set system time
        Local $aResult = DllCall("kernel32.dll", "bool", "SetSystemTimeAdjustment", "dword", $iAdjustment, "bool", $fDisabled)
        If @error Then
            $iError = @error
            $iLastError = @extended
        ElseIf $aResult[0] Then
            $iRet = True
        Else
            $iError = 1
            $iLastError = _WinAPI_GetLastError()
        EndIf

        ; Disable system time privileged mode
        _Security__SetPrivilege($hToken, "SeSystemtimePrivilege", False)
        If @error Then $iError = 2

    EndIf
    _WinAPI_CloseHandle($hToken)

    Return SetError($iError,  $iLastError, $iRet)
EndFunc   ;==>_Date_Time_SetSystemTimeAdjustment

I think there's other reason for getting this error

Link to comment
Share on other sites

...but function _Date_Time_SetSystemTimeAdjustment is already doing this.

Doh! :(

You're right, I missed that.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...