SetSystemTime crashes Windows 10 if write filters are on

216 Views Asked by At

I have a problem whereby sometimes (I haven't yet managed to figure out a pattern, it only happens every few times I try) if my application changes the windows time using SetSystemTime(), Windows crashes.

And by this I mean a message pops up with title,

Microsoft Windows

and text

The application is not responding. The program may respond again if you wait.

...with an [End Process] and [Cancel] button.

I've waited quite a while (like 20 mins) and it hasn't gone away.

If I End Process or Cancel, the taskbar disappears, and the message box goes away. But if I press any key on the keyboard, the message box comes back. I can't view the Task Manager with the system in this state. Pressing Ctrl-Alt-Delete does nothing, neither does Ctrl-Shift-Esc.

The thing is, this doesn't happen every time, and it only happens when the Write Filters (UWF) are on.

Here is my code to set the system time:

    [StructLayout(LayoutKind.Sequential)]
    internal struct SystemTime
    {
        public short Year;
        public short Month;
        public short DayOfWeek;
        public short Day;
        public short Hour;
        public short Minute;
        public short Second;
        public short Milliseconds;
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool SetSystemTime(ref SystemTime st);

then

        short year, month, day, hour, minute, second;
        SystemTime systemTime = new SystemTime();

        // code to populate all the components of the time.

        systemTime.Year = year;
        systemTime.Month = month;
        systemTime.Day = day;
        systemTime.Hour = hour;
        systemTime.Minute = minute;
        systemTime.Second = second;

        if (SetSystemTime(ref systemTime))
        {
            MessageBox.Show("Time changed!");
        }
        else 
        {
            MessageBox.Show("Time change failed.");
        }
    }

What's causing this crash and how do I prevent it? Is there something I have to exclude from the write filters?

0

There are 0 best solutions below