Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Code To Change System Date/Time Doesn't Work

C# Code To Change System Date/Time Doesn't Work

Scheduled Pinned Locked Moved C#
csharpquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm trying to change the system date and time on XP Pro: In the form's class header I have:

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

    public struct structSystemTime
    {
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
    }

    Then in Form_Load I have:

    structSystemTime st = new structSystemTime ();
    st.wYear = 2003;
    st.wMonth = 5;
    st.wDay = 22;
    st.wHour = 22;
    st.wMinute = 15;
    st.wSecond = 08;

    SetSystemTime(ref st);

    It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?

    Everything Makes Sense In Someones Mind

    P M T 3 Replies Last reply
    0
    • K Kevin Marois

      I'm trying to change the system date and time on XP Pro: In the form's class header I have:

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

      public struct structSystemTime
      {
      public short wYear;
      public short wMonth;
      public short wDayOfWeek;
      public short wDay;
      public short wHour;
      public short wMinute;
      public short wSecond;
      public short wMilliseconds;
      }

      Then in Form_Load I have:

      structSystemTime st = new structSystemTime ();
      st.wYear = 2003;
      st.wMonth = 5;
      st.wDay = 22;
      st.wHour = 22;
      st.wMinute = 15;
      st.wSecond = 08;

      SetSystemTime(ref st);

      It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?

      Everything Makes Sense In Someones Mind

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      KMAROIS wrote:

      It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?

      Change the definitions of the fields from short to ushort (unsigned short).

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      • K Kevin Marois

        I'm trying to change the system date and time on XP Pro: In the form's class header I have:

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

        public struct structSystemTime
        {
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
        }

        Then in Form_Load I have:

        structSystemTime st = new structSystemTime ();
        st.wYear = 2003;
        st.wMonth = 5;
        st.wDay = 22;
        st.wHour = 22;
        st.wMinute = 15;
        st.wSecond = 08;

        SetSystemTime(ref st);

        It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?

        Everything Makes Sense In Someones Mind

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        If fixing the structure doesn't help, does GetLastWin32Error() return a useful error code?

        if (!SetSystemTime(ref st))
        {
        int errcode = Marshal.GetLastWin32Error();
        }

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        K 1 Reply Last reply
        0
        • M Mark Salsbery

          If fixing the structure doesn't help, does GetLastWin32Error() return a useful error code?

          if (!SetSystemTime(ref st))
          {
          int errcode = Marshal.GetLastWin32Error();
          }

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          I changed the struct and it did not work. It returned 1314, which is:

          if (!SetSystemTime(ref st))
          {
          int errcode = Marshal.GetLastWin32Error();
          string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;

          MessageBox.Show(errorMessage); 	//A required privilege is not held by the client
          

          }

          Turns out the Sys Admin has restricted users from chaning the date/time. Now to go find the sys admin.... :mad:

          Everything Makes Sense In Someones Mind

          M 1 Reply Last reply
          0
          • K Kevin Marois

            I changed the struct and it did not work. It returned 1314, which is:

            if (!SetSystemTime(ref st))
            {
            int errcode = Marshal.GetLastWin32Error();
            string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;

            MessageBox.Show(errorMessage); 	//A required privilege is not held by the client
            

            }

            Turns out the Sys Admin has restricted users from chaning the date/time. Now to go find the sys admin.... :mad:

            Everything Makes Sense In Someones Mind

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            There is usually a good reason, good luck with the Admin, take your flame proof suit!

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • K Kevin Marois

              I'm trying to change the system date and time on XP Pro: In the form's class header I have:

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

              public struct structSystemTime
              {
              public short wYear;
              public short wMonth;
              public short wDayOfWeek;
              public short wDay;
              public short wHour;
              public short wMinute;
              public short wSecond;
              public short wMilliseconds;
              }

              Then in Form_Load I have:

              structSystemTime st = new structSystemTime ();
              st.wYear = 2003;
              st.wMonth = 5;
              st.wDay = 22;
              st.wHour = 22;
              st.wMinute = 15;
              st.wSecond = 08;

              SetSystemTime(ref st);

              It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?

              Everything Makes Sense In Someones Mind

              T Offline
              T Offline
              Thomas Stockwell
              wrote on last edited by
              #6

              Whenever I use WinAPI, the equivelant of any integer (short or int) in API documentation, I always have to use Int32.

              Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

              A 1 Reply Last reply
              0
              • T Thomas Stockwell

                Whenever I use WinAPI, the equivelant of any integer (short or int) in API documentation, I always have to use Int32.

                Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

                A Offline
                A Offline
                alefaga
                wrote on last edited by
                #7

                I have the same problem but the marshal tells me that error 57 Have you managed to solve it?:confused:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups