Page 1 of 1
SetSystemTime
Posted: Mon Sep 11, 2006 4:50 pm
by RichardL
I have a GPS unit that outputs location and time as a NEMA string. No problems so far
I can parse the string and get useful data.
The customer now says 'Can you jam the PC's clock from the GPS time?' I took a quick look at SetSystemTime_() which first involves creating a SYSTIME structure

and then calling SetSystemTime_().
However... the documents imply that first I have to get involved with AdjustTokenPrivileges_(), which for me is treading in uncharted territory.
Does anyone have any code that works to set the system time?
Thanks in advance
RichardL
Posted: Mon Sep 11, 2006 5:22 pm
by Trond
Code: Select all
RunProgram("cmd", "/c time 14:56:22", "", #PB_Program_Hide)
Posted: Mon Sep 11, 2006 6:56 pm
by TerryHough
You might find something useful in some of my old PB3.94 code here:
http://elfecc.no-ip.info/purebasic#ClockSync
Re: SetSystemTime
Posted: Mon Sep 11, 2006 9:39 pm
by PB
I've always set the PC's time using this, with no AdjustTokenPrivileges:
Code: Select all
NewTime.SYSTEMTIME
NewTime\wDay=25
NewTime\wMonth=12
NewTime\wYear=2006
NewTime\wHour=18
NewTime\wMinute=30
NewTime\wSecond=00
SetLocalTime_(NewTime)
Posted: Tue Sep 12, 2006 8:21 am
by RichardL
Thanks guys... it looks like Windoze XP does not need any permissions to reset the clock, which makes the task much easier. Fortunately for me none of my users run NT, which it appears does need extra work.
RichardL
Posted: Tue Sep 12, 2006 11:28 am
by PB
Huh? The tip I posted works on all Windows. I even just tested it again now
for you with both Win 98 SE and XP and it changed the time/date on both.
Posted: Tue Sep 12, 2006 12:12 pm
by RichardL
Extract from Win32 Programmers's Reference for SetSystemTime_()
Remarks
Windows NT: The SetSystemTime function fails if the calling process does not have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. Use the AdjustTokenPrivileges function to enable this privilege and again to disable it after the time has been set. For more information about security privileges, see Privileges
Windows 95: Security privileges are not supported or required.
I assumed that the later the operating system the more protection
RichardL
Posted: Tue Sep 12, 2006 1:01 pm
by PB
> Extract from Win32 Programmers's Reference for SetSystemTime_()
Who's using SetSystemTime?

Posted: Tue Sep 12, 2006 1:33 pm
by Trond
RichardL wrote:Thanks guys... it looks like Windoze XP does not need any permissions to reset the clock,
You do, but if you don't have the permissions you can't simply get them (that's the idea of permissions).
Posted: Tue Sep 12, 2006 2:12 pm
by RichardL
Extract from the Win32 programmer's Reference for SetLocalTime():
Remarks
Windows NT: The SetLocalTime function fails if the calling process does not have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. Use the AdjustTokenPrivileges function to enable this privilege and again to disable it after the time has been set. For more information about security privileges, see Privileges.
Windows 95: Security privileges are not supported or required.
Oops.. I forgot to mention that PB's code successfully set my clock OK... no permissions... I'm usually resting at that time after a heavy Xmas lunch!
Posted: Tue Sep 12, 2006 2:35 pm
by netmaestro
Just for fun if you want to display the new time setting after you applied it and give the user a chance to see and/or adjust it, you can programmatically call up the timedate applet from the control panel:
Code: Select all
Procedure RunControlPanelApplet(Appletname.s)
Protected runstr.s = "rundll32.exe shell32.dll, Control_RunDLL " + appletname
ProcedureReturn WinExec_(runstr, #SW_SHOWNORMAL)
EndProcedure
runcontrolpanelapplet("timedate.cpl")