What is the function to update the system time from within your program??

I am using microsoft visual c++ 6.0 and I want to know the function to update the system time on your pc while running your program application.  I am using the SYSTEMTIME class and to time tag my files but I want to update the system time once every 24 hours using the gps that is connected to the system.
Who is Participating?
jimbucciConnect With a Mentor Commented:
This code worked on NT:
BOOL set_system_time()
  SYSTEMTIME     st;          
  DWORD          rc;
  BOOL          bResult = FALSE;

  HANDLE     hToken;     // privilege info

  // Set system time
  GetLocalTime(&st);  // or get time from GPS

   /* enable system-time privilege, set time, disable privilege */
    if( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
       memset( (void *)&tp, 0, sizeof(tp) );
             tp.PrivilegeCount            = 1;
             tp.Privileges[0].Attributes |= SE_PRIVILEGE_ENABLED;

          if( AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL ) )
               bResult = SetSystemTime(&st);
               AdjustTokenPrivileges( hToken, TRUE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL );

// Notify all windows of time change
  if( bResult )
    SendNotifyMessage(HWND_BROADCAST, WM_TIMECHANGE, 0,  
  return bResult;

Kinda messy but it should paste fine.
Let me know if this works on your platform.  There may be some changes for Win2k and XP.
Not too difficult just use SetSystemTime() to set the system clock. Here is a link to it's MSDN description http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_9xyd.asp and also a link to all the Windows time functions http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_4po3.asp

Let me know if this is what your looking for.

My 2 cents...
SetSystemTime() works if you are not on a secure box.  For WinNT and up, you need to make sure you have the required privileges to set the system time.  Administrators have all privileges so this is not an issue.  However, if you are a regular user you must query the system to determine whether the time can be changed - otherwise setting the system time will not work.  
I have code to illustrate this if you are interested.
adatdAuthor Commented:
Please I am interested.....   Lets see the code or email it to me at rogerslouis@otc.army.mil
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.