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