SetSystemTime

Windows specific forum
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

SetSystemTime

Post by RichardL »

I have a GPS unit that outputs location and time as a NEMA string. No problems so far :D

I can parse the string and get useful data. :D

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 :shock: 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

RunProgram("cmd", "/c time 14:56:22", "", #PB_Program_Hide)
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

You might find something useful in some of my old PB3.94 code here:
http://elfecc.no-ip.info/purebasic#ClockSync
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: SetSystemTime

Post 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)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Extract from Win32 Programmers's Reference for SetSystemTime_()

Who's using SetSystemTime? ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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).
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post 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!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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")
BERESHEIT
Post Reply