Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Tuesday, September 27, 2011

How to change system time by program

Changing system time needs special privileges... This example will show you how to get special privileges and set the system time for windows


#include <iostream>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <windows.h>

using namespace std;


int main()
{
cout << "Started ..." << endl;

HANDLE hPrivilegeToken = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hPrivilegeToken))
hPrivilegeToken = NULL;

if (hPrivilegeToken == NULL)
{
cout << "#Error : Failed to OpenProcess for Privilages" << endl;
return 0;
}

TOKEN_PRIVILEGES tok;
tok.PrivilegeCount = 1;
tok.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tok.Privileges[0].Luid))
if (AdjustTokenPrivileges(hPrivilegeToken, FALSE, &tok, sizeof(tok), NULL, NULL))
{
SYSTEMTIME  time;
GetSystemTime(&time);

time.wYear = 2043;
time.wDay = 6;
time.wMonth = 8;

SetSystemTime(&time);


return true;
}

cout << "#Error : Failed to get Privilages" << endl;
return 0;
}