Sign in to follow this  
Followers 0
4Eyes

How to set system time under Vista with UAC on?

7 posts in this topic

#1 ·  Posted (edited)

Folks,

Is there a known working method to set the system time under Vista with UAC enabled?

I've tried _SetTime(), Jennico's method of DllCall("Kernel32.dll", "int", "SetSystemTime", "ptr", DllStructGetPtr($tSystemTime)), and _Date_Time_SetSystemTime() which is documented as 'may not work' ... well, it certainly does not work.

And just for record I hate Vista and UAC both! Don't start me....

4Eyes

PS Sorry for double post, the forums had a 'database error' just while I was trying to post.

Edited by 4Eyes

Share this post


Link to post
Share on other sites



Yes, there is.

Code you've tried?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

trancexx,

I didn't show the code before as I had listed the various techniques, however, here it is kinda sorta. The 3 different techniques are there but I've rem'ed some stuff out so that only the first is active.

And yes, I know that enabling the 2nd or 3rd techniques may not set the correct time, but that's irrelevent at the mom as it doesn't work, period. If it worked but set the wrong time then I'd debug it to make it set the correct time.

I hope you understand what I'm trying to say.

4Eyes

; Adapted from this: http://www.autoitscript.com/forum/index.php?showtopic=43079&st=0&p=765481&hl=ntp&fromsearch=1&#entry765481  post 11, last example
    Local $status, $data, $unsignedHexValue, $value, $TZinfo, $TZOffset, $UTC, $m, $d, $y, $h, $mi, $s, $tCurr

    UDPStartup()

    Dim $socket = UDPOpen(TCPNameToIP("pool.ntp.org"), 123)

    If @error <> 0
        If $Debug = True Then _FileWriteLog(@ScriptDir & "\debug.log", "Func SyncTimeToInternet(): can't open connection to pool.ntp.org")
        UDPShutdown()
        Return(0)
    EndIf

    $status = UDPSend($socket, MakePacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
    If $status = 0 Then
        If $Debug = True Then _FileWriteLog(@ScriptDir & "\debug.log", "Func SyncTimeToInternet(): error while sending UDP message = " & @error)
        UDPShutdown()
        Return(0)
    EndIf

    $data = ""
    While $data = ""
        $data = UDPRecv($socket, 100)
        Sleep(100)
    WEnd
    UDPCloseSocket($socket)
    UDPShutdown()

    $unsignedHexValue = StringMid($data, 83, 8)                                     ; Extract time from packet. Disregards the fractional second.

    $value = UnsignedHexToDec($unsignedHexValue)
    $TZinfo = _Date_Time_GetTimeZoneInformation()
    $UTC = _DateAdd("s", $value, "1900/01/01 00:00:00")

    If $TZinfo[0] <> 2 Then                                                         ; 0 = Daylight Savings not used in current time zone / 1 = Standard Time
        $TZoffset = ($TZinfo[1]) * - 1
    Else                                                                            ; 2 = Daylight Savings Time
        $TZoffset = ($TZinfo[1] + $TZinfo[7]) * - 1
    EndIf

    ;~ Extracts the data & time into vars. Date format & offsets - 2009/12/31 19:26:05 - 1234567890123456789  [1 is start of string]
    $m = StringMid($UTC, 6, 2)
    $d = StringMid($UTC, 9, 2)
    $y = StringMid($UTC, 1, 4)
    $h = StringMid($UTC, 12, 2)
    $mi = StringMid($UTC, 15, 2)
    $s = StringMid($UTC, 18, 2)

    ; This does nothing under Vista with UAC on. No change, no error, nothing.
    $tCurr = _Date_Time_EncodeSystemTime($m, $d, $y, $h, $mi, $s)                   ; Sets the new current time to the computer
    $rc = _Date_Time_SetSystemTime(DllStructGetPtr($tCurr))

    If $rc = 0 Then MsgBox(0, $ProgTitle, "Unable to set the time.")

#cs
    ; new 5/Sep/2010 - from old Jennico code - doesn't work under Vista with UAC on either
    $sys = "short Y;short M;short Dow;short D;short H;short Mi;short S;short MS"
    $system = DllStructCreate($sys)

    $tSystemTime = $system

    DllStructSetData($tSystemTime, "M", $m)
    DllStructSetData($tSystemTime, "D", $d)
    DllStructSetData($tSystemTime, "H", $h)
    DllStructSetData($tSystemTime, "S", $s)
    DllStructSetData($tSystemTime, "Mi", $mi)
    DllStructSetData($tSystemTime, "MS", "0")
    DllStructSetData($tSystemTime, "Y", $y)

    DllCall("Kernel32.dll", "int", "SetSystemTime", "ptr", DllStructGetPtr($tSystemTime))

    If @error = 0 Then
        Return(1)                                                                       ; Show succeeded
    Else
        Return(0)
    EndIf
#ce

    #cs This doesn't work under Vista with UAC either
    $rc = _SetTime($h, $m, $s)
    If $rc = 0 Then MsgBox(0, "", "_SetTime() failed")
    #ce
Edited by 4Eyes

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Add #RequireAdmin.

edit:

...and was it really that hard to post scriptlet that could actually be run without fixing it first?

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

trancexx,

Firstly, your answer does not help at all. I specifically asked "Is there a known working method to set the system time under Vista with UAC enabled?" You answered, turn off UAC.

Secondly, I had already outlined the techniques I had used.

Thirdly, the code snippet is exactly that... a snippet out of a larger program. I suppose I should've verified that it would work self-contained. I'll do that in future.

Finally, nah... I won't say is it that hard to be polite? Oops, slipped out....

4Eyes

Share this post


Link to post
Share on other sites

trancexx,

Firstly, your answer does not help at all. I specifically asked "Is there a known working method to set the system time under Vista with UAC enabled?" You answered, turn off UAC.

Could you show me where have I said that? What are you talking about? ;)

Thirdly, the code snippet is exactly that... a snippet out of a larger program. I suppose I should've verified that it would work self-contained. I'll do that in future.

Syntax was wrong. The most basic element for your code to be classified as "written in some language".

Finally, nah... I won't say is it that hard to be polite? Oops, slipped out....

4Eyes

You should be more. "Thanks" would have been nice.

There is no way of changing what you want to change without running in elevated mode for that environment. That's why there are those shields buttons on built-in windows apps made for changing time.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

trancexx,

Ok, my apologies, you said use #RequireAdmin which is not an option for me in this case.

I have more things I could say, but this time, I will hold my tongue.

4Eyes

Share this post


Link to post
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
Sign in to follow this  
Followers 0