I can't get SetSystemTime to work in Windows Vista using C# with Interop (P/Invoke).
Posted
by Andrew
on Stack Overflow
See other posts from Stack Overflow
or by Andrew
Published on 2010-03-21T06:00:49Z
Indexed on
2010/03/21
6:11 UTC
Read the original article
Hit count: 479
Hi, I'm having a hard time getting SetSystemTime working in my C# code. SetSystemtime is a kernel32.dll function. I'm using P/invoke (interop) to call it. SetSystemtime returns false and the error is "Invalid Parameter". I've posted the code below. I stress that GetSystemTime works just fine. I've tested this on Vista and Windows 7. Based on some newsgroup postings I've seen I have turned off UAC. No difference. I have done some searching for this problem. I found this link: http://groups.google.com.tw/group/microsoft.public.dotnet.framework.interop/browse_thread/thread/805fa8603b00c267
where the problem is reported but no resolution seems to be found. Notice that UAC is also mentioned but I'm not sure this is the problem. Also notice that this gentleman gets no actual Win32Error.
- Can someone try my code on XP?
- Can someone tell me what I'm doing wrong and how to fix it. If the answer is to somehow change permission settings programatically, I'd need an example. I would have thought turning off UAC should cover that though.
- I'm not required to use this particular way (SetSystemTime). I'm just trying to introduce some "clock drift" to stress test something. If there's another way to do it, please tell me. Frankly, I'm surprised I need to use Interop to change the system time. I would have thought there is a .NET method.
Thank you very much for any help or ideas. Andrew
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace SystemTimeInteropTest
{
class Program
{
#region ClockDriftSetup
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
[DllImport("kernel32.dll")]
public static extern void GetLocalTime(
out SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern void GetSystemTime(
out SystemTime systemTime);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(
ref SystemTime systemTime);
//[DllImport("kernel32.dll", SetLastError = true)]
//public static extern bool SetLocalTime(
//ref SystemTime systemTime);
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetLocalTime")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool SetLocalTime([InAttribute()] ref SystemTime lpSystemTime);
#endregion ClockDriftSetup
static void Main(string[] args)
{
try
{
SystemTime sysTime;
GetSystemTime(out sysTime);
sysTime.Milliseconds += (short)80;
sysTime.Second += (short)3000;
bool bResult = SetSystemTime(ref sysTime);
if (bResult == false)
throw new System.ComponentModel.Win32Exception();
}
catch (Exception ex)
{
Console.WriteLine("Drift Error: " + ex.Message);
}
}
}
}
© Stack Overflow or respective owner