I want to change system time Programatically in C# (in Windows 8.1) and I used Win32SetSystemTime and setSystemTime:
static extern bool Win32SetSystemTime([InAttribute()] ref SYSTEMTIME sysTime) static extern bool SetSystemTime(ref SYSTEMTIME time);
I set privileges before I call these functions and set privileges return true but when I call Win32SetSystemTime or setSystemTime, I get error 1300 and 1314 and system time doesn't change at all. These errors are about privileges!! And whenever I run app as an administrator, they work correctly! I use below code to set privilege:
string privilege = "SE_SYSTEMTIME_NAME";
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc,TOKEN_ALL_ACCESS , ref htok);
// retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw ex;
}