C# Code To Change System Date/Time Doesn't Work
-
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
-
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
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.
-
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
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:
-
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:
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
-
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
There is usually a good reason, good luck with the Admin, take your flame proof suit!
Never underestimate the power of human stupidity RAH
-
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
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
-
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