User Tools

Site Tools

한국어

cuwin:setting_the_date_and_time_on_the_cwr:index

Setting the Date and Time on the CWR

If you need to set the date and time on the CWR from within a software program, please use the following code.

using System;
using System.Runtime.InteropServices;
 
namespace SetTimeConsole
{
    class Program
    {
        private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
 
        private const uint GENERIC_READ = 0x80000000;
        private const uint GENERIC_WRITE = 0x40000000;
        private const uint OPEN_EXISTING = 3;
        private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
        private const uint FILE_FLAG_OVERLAPPED = 0x40000000;
 
        private const uint IOCTL_RTC_WRITE = 0x0022A05C;
 
        [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr CreateFile(
            string lpFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr lpSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr hTemplateFile);
 
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool DeviceIoControl(
            IntPtr hDevice,
            uint dwIoControlCode,
            IntPtr lpInBuffer,
            uint nInBufferSize,
            IntPtr lpOutBuffer,
            uint nOutBufferSize,
            IntPtr lpBytesReturned,
            IntPtr lpOverlapped);
 
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hObject);
 
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
 
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool SetLocalTime(ref SYSTEMTIME st);
 
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int GetLastError();
 
        static int Main(string[] args)
        {
            // Example: set device local time to 2026-04-10 08:30:00.000
            DateTime dt = new DateTime(2026, 4, 10, 8, 30, 0);
 
            SYSTEMTIME st = new SYSTEMTIME();
            st.wYear = (ushort)dt.Year;
            st.wMonth = (ushort)dt.Month;
            st.wDay = (ushort)dt.Day;
            st.wDayOfWeek = (ushort)dt.DayOfWeek;
            st.wHour = (ushort)dt.Hour;
            st.wMinute = (ushort)dt.Minute;
            st.wSecond = (ushort)dt.Second;
            st.wMilliseconds = (ushort)dt.Millisecond;
 
            bool ok = SetLocalTime(ref st);
 
            if (!ok)
            {
                int err = GetLastError();
                Console.WriteLine("SetLocalTime failed. GetLastError = " + err);
                return 1;
            }
 
            IntPtr hCom = CreateFile(
                "RTC1:",
                GENERIC_READ | GENERIC_WRITE,
                0,
                IntPtr.Zero,
                OPEN_EXISTING,
                FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,
                IntPtr.Zero);
 
            if (hCom == INVALID_HANDLE_VALUE)
            {
                int err = GetLastError();
                Console.WriteLine("CreateFile failed. GetLastError = " + err);
                return 1;
            }
 
            ok = DeviceIoControl(
                hCom,
                IOCTL_RTC_WRITE,
                IntPtr.Zero,
                0,
                IntPtr.Zero,
                0,
                IntPtr.Zero,
                IntPtr.Zero);
 
            if (!ok)
            {
                int err = GetLastError();
                Console.WriteLine("DeviceIoControl failed. GetLastError = " + err);
                CloseHandle(hCom);
                return 1;
            }
 
            CloseHandle(hCom);
 
            Console.WriteLine("Time was set and saved successfully");
            return 0;
        }
    }
}
cuwin/setting_the_date_and_time_on_the_cwr/index.txt · Last modified: by mfranklin