Sign in to follow this  
Followers 0
Docfxit

Setting System Time with DLL

13 posts in this topic

#1 ·  Posted (edited)

I'm trying to set the system time in XP sp1 with this code:

#include <A3LTime.au3>

Global $sec, $minute, $hour, $light, $time, $clocklabel, $Once = ""
GUICreate("*PCT Watch*", 220, 50, (@DesktopWidth - 188) / 2, (@DesktopHeight - 59) / 2)
$clocklabel = GUICtrlCreateLabel("Loading...", 5, 5, 210, 40, 0x1000)
GUICtrlSetFont($clocklabel, 24)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Sleep(100)
    If $Once <> "Done" Then
        $tCurTime = _Time_GetLocalTime()        ; Save time before we test to see if DST is installed correctly
        $hour = Number(@HOUR-@HOUR+1)           ; Change time to test to see if DST is installed correctly
        $minute = Number(59)
        $sec = Number(59)
        $Mon = 03
        $Day = 11
        $tNewTime = _Time_EncodeSystemTime($Mon, $Day, @YEAR, $hour, $minute, $sec)
        _Time_SetLocalTime(_tagGetPtr($tNewTime))
        $Once = "Done"
    ElseIf $msg = -3 Then           ; X in top right hit to close the window
        _Time_SetLocalTime(_tagGetPtr($tCurTime))   ; Reset to Time before this script started
        Exit
    EndIf
    GUICtrlSetData($clocklabel, TimeDisplay())
WEnd

Func TimeDisplay()
    $tNewTime = _Time_GetLocalTime()
    $light = " AM"
    $minute = @MIN
    $hour = @HOUR
    $sec = @SEC
    If $hour = 0 Then
        $hour = 12
    ElseIf $hour = 12 Then
        $light = " PM"
    ElseIf $hour > 12 Then
        $hour = (@HOUR) - 12
        $light = " PM"
    EndIf
    $time = $hour & ":" & $minute & ":" & $sec & $light
    Return $time
EndFunc   ;==>TimeDisplay

This script is setting the date and time correctly now.

I'm looking for ways to make it less complicated.

I know about _SetTime. I couldn't get it to work so I'm trying it this way.

Thank you,

Docfxit

Edited by docfxit

Share this post


Link to post
Share on other sites



Why go to this trouble? Just write the proper regisry entries to patch the system.

If you need them I have the entries for Win 98 and I think they are the same for XP. I'm not sure because I cheated and used the MS patch for the XP machines.

Also there should be no need to change anything except the hour

$Hour = @hour +1

If @Month = 3 And @Mday = 11 And #Hour = 02 Then

$Str = "ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort"

$Systime = DllStructCreate($Str)

$blah = dllopen("kernel32.dll")

$arg = DLLCALL($blah,"long","GetSystemTime","ptr",DllStructGetPtr($Systime));; << I don't think you need this line either.

DllStructSetData($Systime,5,$hour)

$arg = DLLCALL($blah,"long","SetSystemTime","ptr",DllStructGetPtr($Systime))

dllclose($blah)

$Systime=0

EndIf


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Why go to this trouble? Just write the proper regisry entries to patch the system.

If you need them I have the entries for Win 98 and I think they are the same for XP. I'm not sure because I cheated and used the MS patch for the XP machines.

Also there should be no need to change anything except the hour

$Hour = @hour +1

If @Month = 3 And @Mday = 11 And #Hour = 02 Then

$Str = "ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort"

$Systime = DllStructCreate($Str)

$blah = dllopen("kernel32.dll")

$arg = DLLCALL($blah,"long","GetSystemTime","ptr",DllStructGetPtr($Systime));; << I don't think you need this line either.

DllStructSetData($Systime,5,$hour)

$arg = DLLCALL($blah,"long","SetSystemTime","ptr",DllStructGetPtr($Systime))

dllclose($blah)

$Systime=0

EndIf

Thank you very much for putting this together.

This would be great for someone that would like to run this all the time. I have cut this script down to make it easier to test. If I would have posted the complete script it might have shown that my reasoning for creating this is to test if the new Daylight savings updates have been applied. By setting the month, day, year, hour, minute and second I see if the system is going to adjust the clock on it's own. If it does adjust the clock on it's own I run a program to synchronize the clock over the internet. If it doesn't I apply the registry entries so it will in the future. If this was only for my own PC's I wouldn't create a script. Since I support a number of PC's I thought it would be nice to have a script to test which ones need the update.

I have had a few people say it works fine on their PC. I'm trying to figure out what is different about my PC. I'm running XP sp1.

So I'm still trying to figure out why this isn't working.

Thank you,

Docfxit

Share this post


Link to post
Share on other sites

Maybe I'm not initializing the hour variable correctly. Is there a more appropriate way to initialize the variable?

Thank you,

Docfxit

Share this post


Link to post
Share on other sites

I don't know if you realize it or not, but there is a difference between the system time and the local time on your PC. The system time is in UTC and is typically (depending on where you are in the world) 6 hours different than the local time. If you are getting the local time and then using that to set the system time, you are going to be off by several hours.

If you want to see how the time functions work, take a look at the Time demo script in Auto3Lib. It shows how to set the local and system time and it has functions for reading/setting DST settings. The A3LTime module has all of the Windows Time functions in it.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

I don't know if you realize it or not, but there is a difference between the system time and the local time on your PC. The system time is in UTC and is typically (depending on where you are in the world) 6 hours different than the local time. If you are getting the local time and then using that to set the system time, you are going to be off by several hours.

If you want to see how the time functions work, take a look at the Time demo script in Auto3Lib. It shows how to set the local and system time and it has functions for reading/setting DST settings. The A3LTime module has all of the Windows Time functions in it.

Thank you very much for pointing out the difference between local time and system time. Thank you also for creating/sharing Auto3Lib. While I didn't see any instructions for Auto3Lib I'm guessing to use the examples a person is supposed to:

1. Copy Auto3Lib/Examples to C:\Program Files\AutoIt3\Examples

2. Copy Auto3Lib/Help to C:\Program Files\AutoIt3

3. Copy Auto3Lib/Include to C:\Program Files\AutoIt3\Include

4. Copy Auto3Lib/SciTE to C:\Program Files\AutoIt3\SciTE

I changed the example in post #1 to work with LocalTime instead of SystemTime. When I update LocalTime I would expect @HOUR to be the same. It doesn't say in the help what @HOUR is. It appears @HOUR is the local time plus Daylight Saving offset.

I'm surprised LocalTime doesn't include Daylight Saving offset. I ran the example Time.au3. The only thing I saw that might help me adjust for Daylight Saving offset is Daylight bias......: -60.

When I change the LocalTime Hour to 01 @HOUR gives me 00.

Am I supposed to offset the hour I use to set LocalTime with the Daylight bias......: -60?

LocalTime hour that I want = 01

LocalTime hour input = 01 + 60 minutes = 02?

Thank you,

Docfxit

Share this post


Link to post
Share on other sites

Thank you very much for pointing out the difference between local time and system time. Thank you also for creating/sharing Auto3Lib. While I didn't see any instructions for Auto3Lib I'm guessing to use the examples a person is supposed to:

1. Copy Auto3Lib/Examples to C:\Program Files\AutoIt3\Examples

2. Copy Auto3Lib/Help to C:\Program Files\AutoIt3

3. Copy Auto3Lib/Include to C:\Program Files\AutoIt3\Include

4. Copy Auto3Lib/SciTE to C:\Program Files\AutoIt3\SciTE

I changed the example in post #1 to work with LocalTime instead of SystemTime. When I update LocalTime I would expect @HOUR to be the same. It doesn't say in the help what @HOUR is. It appears @HOUR is the local time plus Daylight Saving offset.

I'm surprised LocalTime doesn't include Daylight Saving offset. I ran the example Time.au3. The only thing I saw that might help me adjust for Daylight Saving offset is Daylight bias......: -60.

When I change the LocalTime Hour to 01 @HOUR gives me 00.

Am I supposed to offset the hour I use to set LocalTime with the Daylight bias......: -60?

LocalTime hour that I want = 01

LocalTime hour input = 01 + 60 minutes = 02?

Thank you,

Docfxit

If you run the Time demo (in SciTE), it prints the local time (with correct DST adjustment) and the system time (which is always UTC). If you want to set your PC clock, just use _Time_SetLocalTime (see the Time demo function "SetLocalTime").

As for your @HOUR question, run the script below. It shows you the current time and @HOUR value, then bumps the hour up by 1 and shows you the new time and @HOUR. Afterward, it resets the time to what it was. If you are not getting the correct results from this script, then I can't imagine what the problem might be unless your DST settings are messed up.

#include <A3LTime.au3>

$tCurTime = _Time_GetLocalTime()
_Lib_ConsoleWrite("Current Time: " & _Time_SystemTimeToTimeStr($tCurTime))
_Lib_ConsoleWrite("@Hour ......: " & @HOUR)

$tNewTime = _Time_EncodeSystemTime(@MON, @MDAY, @YEAR, @HOUR + 1, @MIN, @SEC)
_Time_SetLocalTime(_tagGetPtr($tNewTime))
$tNewTime = _Time_GetLocalTime()
_Lib_ConsoleWrite("New Time ...: " & _Time_SystemTimeToTimeStr($tNewTime))
_Lib_ConsoleWrite("@Hour ......: " & @HOUR)

_Time_SetLocalTime(_tagGetPtr($tCurTime))

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

As for your @HOUR question, run the script below. It shows you the current time and @HOUR value, then bumps the hour up by 1 and shows you the new time and @HOUR. Afterward, it resets the time to what it was. If you are not getting the correct results from this script, then I can't imagine what the problem might be unless your DST settings are messed up.

#include <A3LTime.au3>

$tCurTime = _Time_GetLocalTime()
_Lib_ConsoleWrite("Current Time: " & _Time_SystemTimeToTimeStr($tCurTime))
_Lib_ConsoleWrite("@Hour ......: " & @HOUR)

$tNewTime = _Time_EncodeSystemTime(@MON, @MDAY, @YEAR, @HOUR + 1, @MIN, @SEC)
_Time_SetLocalTime(_tagGetPtr($tNewTime))
$tNewTime = _Time_GetLocalTime()
_Lib_ConsoleWrite("New Time ...: " & _Time_SystemTimeToTimeStr($tNewTime))
_Lib_ConsoleWrite("@Hour ......: " & @HOUR)

_Time_SetLocalTime(_tagGetPtr($tCurTime))
That solved my problem. It's working great now. Thank you very much for helping me resolve it.

I have updated post #1 to show my current script that works.

The first post example looks a little complicated.

Do you have any thoughts on how I could make it simpler?

Thank you,

Docfxit

Edited by docfxit

Share this post


Link to post
Share on other sites

That solved my problem. It's working great now. Thank you very much for helping me resolve it.

I have updated post #1 to show my current script that works.

I am getting one Warning and I don't know how to fix it. WARNING: $tCurTime: possibly used before declaration. _Time_SetLocalTime(_tagGetPtr($tCurTime)

The first post example looks a little complicated.

Do you have any thoughts on how I could make it simpler?

Thank you,

Docfxit

The warning is due to using the variable before it is declared. Don't know if it's less complicated, but here's what I would do:

#include <A3LTime.au3>
#include <GUIConstants.au3>

Opt("MustDeclareVars", 1)

Global $iClock, $tCurTime, $tNewTime, $iTime, $iHour, $sAMPM

GUICreate("*PCT Watch*", 220, 50)
$iClock = GUICtrlCreateLabel("Loading...", 5, 5, 210, 40, $SS_SUNKEN)
GUICtrlSetFont($iClock, 24)
GUISetState()

$iTime    = TimerInit()
$tCurTime = _Time_GetLocalTime()
$tNewTime = _Time_EncodeSystemTime(3, 11, @YEAR, 1, 59, 59)
_Time_SetLocalTime(_tagGetPtr($tNewTime))

do
  if TimerDiff($iTime) >= 1000 then
    $iTime = TimerInit()
    $iHour = @HOUR
    $sAMPM = "AM"
    Select
      case $iHour = 0
        $iHour = 12
      case $iHour = 12
        $sAMPM = "PM"
      case $iHour > 12
        $iHour = $iHour - 12
        $sAMPM = "PM"
    EndSelect
    GUICtrlSetData($iClock, StringFormat("%02d:%02d:%02d %s", $iHour, @MIN, @SEC, $sAMPM))
  endif
until GUIGetMsg() = $GUI_EVENT_CLOSE

_Time_SetLocalTime(_tagGetPtr($tCurTime))

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

The warning is due to using the variable before it is declared. Don't know if it's less complicated, but here's what I would do:

#include <A3LTime.au3>
#include <GUIConstants.au3>

Opt("MustDeclareVars", 1)

Global $iClock, $tCurTime, $tNewTime, $iTime, $iHour, $sAMPM

GUICreate("*PCT Watch*", 220, 50)
$iClock = GUICtrlCreateLabel("Loading...", 5, 5, 210, 40, $SS_SUNKEN)
GUICtrlSetFont($iClock, 24)
GUISetState()

$iTime    = TimerInit()
$tCurTime = _Time_GetLocalTime()
$tNewTime = _Time_EncodeSystemTime(3, 11, @YEAR, 1, 59, 59)
_Time_SetLocalTime(_tagGetPtr($tNewTime))

do
  if TimerDiff($iTime) >= 1000 then
    $iTime = TimerInit()
    $iHour = @HOUR
    $sAMPM = "AM"
    Select
      case $iHour = 0
        $iHour = 12
      case $iHour = 12
        $sAMPM = "PM"
      case $iHour > 12
        $iHour = $iHour - 12
        $sAMPM = "PM"
    EndSelect
    GUICtrlSetData($iClock, StringFormat("%02d:%02d:%02d %s", $iHour, @MIN, @SEC, $sAMPM))
  endif
until GUIGetMsg() = $GUI_EVENT_CLOSE

_Time_SetLocalTime(_tagGetPtr($tCurTime))
That is really great. It looks much simpler now and it works. :-) That's really great of you to show me how to do it.

Thank you very much.

Docfxit

Share this post


Link to post
Share on other sites

That is really great. It looks much simpler now and it works. :-) That's really great of you to show me how to do it.

Thank you very much.

Docfxit

You're welcome. If you have any further questions/problems with Auto3Lib, just drop me a line in the Auto3Lib thread.

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

I want to use this and want to make sure that I am doin it right.

Open script

Check the time on the script

Close it and it sets the time (your time) back and if the time is correct - it is GOOD?


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I want to use this and want to make sure that I am doin it right.

Open script

Check the time on the script

Close it and it sets the time (your time) back and if the time is correct - it is GOOD?

Almost. I didn't put much of an explanation as to what it is doing on this one.

What this does is it sets the date to March 11th and the time to 1:59am. It then displays the clock.

If the clock changes to 2:00am the Daylight Savings is set for the old settings (Original)

If the clock changes to 3:00am the Daylight Savings is set for the new settings. You can exit out of it.

I do return the date and time back to what it was when you first run this. But be aware the time will be off by how long you left the clock up.

I have put together another script that:

1. Checks the OS you are running.

2. If you are running XP sp2 and the clock says 3:00am you are all set

3. If you are running XP sp2 and the clock says 2:00am you need the MS updates.

4. If you are running XP sp1 or 2000 or 2003 or ME it will apply the DST update for you

5. It will install a program to update your time through the internet.

6. It will correct your clock to the Time Zone your PC is set to.

7. If your clock is set correctly you are in good shape.

8. If your clock is off your time zone is probably not set correctly.

You can find the script at:

http://www.autoitscript.com/forum/index.ph...mp;#entry312617

If you need to change '98 to the new DST settings I have a separate update. Let me know.

Docfxit

Edited by docfxit

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