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. problem with SetSystemTime() fucntion of Kernel32.dll

problem with SetSystemTime() fucntion of Kernel32.dll

Scheduled Pinned Locked Moved C#
4 Posts 3 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.
  • C Offline
    C Offline
    cppwxwidgetsss
    wrote on last edited by
    #1

    hi all i want to use the following code to set the time of the system by some numericUpDown (hour,minute,second) objects that user input, but i don't know where is the problem when i set the time it changes but it does n't correctly change, please have a look at it, it should be something that i may do not know about it, i just have changed a code that i found on the internet, here is the code:

    [StructLayout(LayoutKind.Sequential)]
    public struct MyDateTime
    {

            public ushort Year;
            public ushort Month;
            public ushort wDayOfWeek;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Milisecond;
    
        }
    
    
        
        \[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)\]
    
        private static extern bool SetSystemTime(ref MyDateTime st);
    
    
    
        \[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)\]
    
        private extern static void GetSystemTime(ref MyDateTime sysTime);
    
    
        MyDateTime Persian;
    

    .
    .
    .
    GetSystemTime(ref Persian);
    Persian.Hour = (ushort) hour.Value;
    Persian.Minute = (ushort) minute.Value;
    Persian.Second = (ushort) second.Value;

             SetSystemTime(ref Persian );
    

    thank you everybody

    D 1 Reply Last reply
    0
    • C cppwxwidgetsss

      hi all i want to use the following code to set the time of the system by some numericUpDown (hour,minute,second) objects that user input, but i don't know where is the problem when i set the time it changes but it does n't correctly change, please have a look at it, it should be something that i may do not know about it, i just have changed a code that i found on the internet, here is the code:

      [StructLayout(LayoutKind.Sequential)]
      public struct MyDateTime
      {

              public ushort Year;
              public ushort Month;
              public ushort wDayOfWeek;
              public ushort Day;
              public ushort Hour;
              public ushort Minute;
              public ushort Second;
              public ushort Milisecond;
      
          }
      
      
          
          \[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)\]
      
          private static extern bool SetSystemTime(ref MyDateTime st);
      
      
      
          \[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)\]
      
          private extern static void GetSystemTime(ref MyDateTime sysTime);
      
      
          MyDateTime Persian;
      

      .
      .
      .
      GetSystemTime(ref Persian);
      Persian.Hour = (ushort) hour.Value;
      Persian.Minute = (ushort) minute.Value;
      Persian.Second = (ushort) second.Value;

               SetSystemTime(ref Persian );
      

      thank you everybody

      D Offline
      D Offline
      dojohansen
      wrote on last edited by
      #2

      I don't know the structure the winapi uses for this, but I suppose I would look for clues by seeing how the method to *get* system time works. Or just double-check the structure. It immediately seems odd to me that it would use shorts for day of week, day, hour, minute, and second, all of which logically require just a byte, but I have almost no experience with system level programming so it may well be that I simply don't know what I'm talking about!

      D 1 Reply Last reply
      0
      • D dojohansen

        I don't know the structure the winapi uses for this, but I suppose I would look for clues by seeing how the method to *get* system time works. Or just double-check the structure. It immediately seems odd to me that it would use shorts for day of week, day, hour, minute, and second, all of which logically require just a byte, but I have almost no experience with system level programming so it may well be that I simply don't know what I'm talking about!

        D Offline
        D Offline
        dojohansen
        wrote on last edited by
        #3

        Hm... according to this it would appear the structure is even less compact and uses four bytes (on a 32-bit platform) for each..?? Best of luck!

        S 1 Reply Last reply
        0
        • D dojohansen

          Hm... according to this it would appear the structure is even less compact and uses four bytes (on a 32-bit platform) for each..?? Best of luck!

          S Offline
          S Offline
          Sujeevan Kumar
          wrote on last edited by
          #4

          I see nothing wrong in what you are doing. Its just that, when you use SetSystemTime(), the time set is UTC - not your local time. So, in order to set your local time, then you need to calculate the difference between your local timezone and UTC (use Offset class to calculate the difference like Offset diff = DataTime.Now - DateTime.UtcNow). Once you know the time difference, update the hours and minutes by the Offset.Hours, Offset.Minutes and you will get the correct local time set. Hope that helps!!

          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