-1

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;
        }
ProXicT
  • 1,903
  • 3
  • 22
  • 46
m am
  • 13
  • 5

1 Answers1

1

Setting the system time requires administrator privileges. That means running the process as an administrator.

Under UAC processes run as standard user by default. You will need to use one of the variety of ways to run your process as administrator. For instance by specifying the requireAdministrator option in the manifest. Best practice is to elevate only for the operations that require elevation. That involves creating a separate process for these admin tasks.

You'll also be able to get rid of all the code in the question because when running as administrator your token will by default already contain the necessary privilege to change the time.

I'm not sure what Win32SetSystemTime is. The function you need to call is SetSystemTime. This example on MSDN shows how to do it, although the author rather lazily omitted error checking: https://msdn.microsoft.com/en-us/library/ms172517(v=vs.90).aspx

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • thank you for your answer but i add a manifest file to my project and set and set the manifest refrence in My project properties but still when i want to set tSystem time i get previous error again !! – m am Sep 06 '15 at 08:00
  • You need to remove all the code in the question and just call SetSystemTime. Also, what you say doesn't tally with "whenever I run app as an administrator, they work correctly" – David Heffernan Sep 06 '15 at 08:10
  • i think maybe the manifest file doesn't work property in the project!? – m am Sep 06 '15 at 10:13
  • When you start your application, are you presented with the UAC consent dialog? – David Heffernan Sep 06 '15 at 10:27
  • no ,UAC Dialog doesn't show .when i Right click on Myproject .exe file or run VisualStudio 2013 as admin i see UAC .but when i use manifest or don't use manifest i don't show any uac, is it possible that my project does't recognize manifest? – m am Sep 06 '15 at 10:40
  • what is the problem? i right click on myproject and select new item --> manifest then i change the level to requireAdministrator !i use visual studio 2013 and windows 8.1 , – m am Sep 06 '15 at 11:12