?
Solved

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

Posted on 2002-06-23
4
Medium Priority
?
219 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:adatd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:cypherljk
ID: 7102524
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...
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7107102
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.
Jim
0
 

Author Comment

by:adatd
ID: 7107502
Please I am interested.....   Lets see the code or email it to me at rogerslouis@otc.army.mil
0
 
LVL 3

Accepted Solution

by:
jimbucci earned 300 total points
ID: 7107915
This code worked on NT:
BOOL set_system_time()
{
  SYSTEMTIME     st;          
  DWORD          rc;
  BOOL          bResult = FALSE;

  HANDLE     hToken;     // privilege info
  TOKEN_PRIVILEGES  tp;

  // 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) );
       LookupPrivilegeValue(  
                  NULL,
                 "SeSystemTimePrivilege",
                 &tp.Privileges[0].Luid
                 );
             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,  
                      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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question