-4

I need to speed up PC System Time by adding a random number of seconds for each (for example) 200 milliseconds. I tried to solve this problem by looking at Change system date programmatically thread.

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace TimeSpeedUp
{
    struct SystemTime
    {
        public ushort Year;
        public ushort Month;
        public ushort DayOfWeek;
        public ushort Day;
        public ushort Hour;
        public ushort Minute;
        public ushort Second;
        public ushort Millisecond;
    }
    class Program
    {
        [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
        public extern static void Win32GetSystemTime(ref SystemTime sysTime);

        [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
        public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
        public static Random Random = new Random();

        static void Main(string[] args)
        {
            Timer timer = new Timer(SetTime);
            timer.Change(0, 200);
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                if (keyInfo.Key == ConsoleKey.Escape)
                {
                    break;
                }
            }
        }

        public static void SetTime(object obj)
        {
            // Current time
            SystemTime currentTime = new SystemTime();
            Win32GetSystemTime(ref currentTime);
            // Set system date and time
            SystemTime updatedTime = new SystemTime();
            updatedTime.Year = (ushort)currentTime.Year;
            updatedTime.Month = (ushort)currentTime.Month;
            updatedTime.Day = (ushort)currentTime.Day;
            updatedTime.Hour = (ushort)currentTime.Hour;
            updatedTime.Minute = (ushort)currentTime.Minute;
            updatedTime.Second = (ushort)(currentTime.Second + Random.Next(1, 3));
            // Call the unmanaged function that sets the new date and time instantly
            Win32SetSystemTime(ref updatedTime);
        }
    }
}
Borko Djurovic
  • 313
  • 2
  • 17

1 Answers1

-1

Application was unable to change System date because it didn't had Administrative privilegies. Running app as Administrator resolves problem.

Borko Djurovic
  • 313
  • 2
  • 17
  • 2
    Your code doesn't check the return value of `Win32SetSystemTime()`, which presumably would have been `false` with `Marshal.GetLastWin32Error()` yielding `5` (`ERROR_ACCESS_DENIED`) when run without elevation. By the way, on the question you linked to there are two answers and three comments that mention the need for elevation/special privileges in order to set the time. – Lance U. Matthews Aug 19 '19 at 06:48
  • 1
    While your posted answer does actually answer the question of why it was not working I am downvoting it because, as @BACON points out, the very question you linked mentions multiple times that the solution requires elevated privileges. I honestly made the assumption that you had already ruled that out since you posted this question and linked to the other. Instead of answering your own question it would be better to just close it, since the linked question actually solves the problem. – pstrjds Aug 19 '19 at 12:07