0

我很难让 SetSystemTime 在我的 C# 代码中工作。SetSystemtime 是一个 kernel32.dll 函数。我正在使用 P/invoke (interop) 来调用它。SetSystemtime 返回 false,错误为“无效参数”。我已经发布了下面的代码。我强调 GetSystemTime 工作得很好。我已经在 Vista 和 Windows 7 上对此进行了测试。根据我看到的一些新闻组帖子,我已经关闭了 UAC。没有不同。我已经为这个问题做了一些搜索。我找到了这个链接: http ://groups.google.com.tw/group/microsoft.public.dotnet.framework.interop/browse_thread/thread/805fa8603b00c267

哪里报告了问题,但似乎没有找到解决方案。请注意,还提到了 UAC,但我不确定这是问题所在。另请注意,这位先生并没有得到实际的 Win32Error。

  1. 有人可以在 XP 上尝试我的代码吗?
  2. 有人可以告诉我我做错了什么以及如何解决它。如果答案是以某种方式以编程方式更改权限设置,我需要一个示例。我会认为关闭 UAC 应该涵盖这一点。
  3. 我不需要使用这种特殊方式(SetSystemTime)。我只是想引入一些“时钟漂移”来对某些东西进行压力测试。如果有其他方法可以做到这一点,请告诉我。坦率地说,我很惊讶我需要使用 Interop 来更改系统时间。我会认为有一个 .NET 方法。

非常感谢您的任何帮助或想法。安德鲁

代码:

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);
            }
        }
    }
}
4

2 回答 2

6

有一个.Net方法来设置时间;默认情况下,它在 C# 中不可用。如果您添加对项目的引用,Microsoft.VisualBasic那么您可以编写:

Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.Now.AddSeconds(3000).AddMilliseconds(80);
于 2010-03-21T06:37:55.120 回答
0

您不能只将 3000 添加到秒字段。您需要指定 0 到 60 之间的秒数。您需要调整秒、分钟、小时等字段,使它们都在有效范围内。

编辑 最简单的方法实际上是调用SystemTimeToFileTime然后添加numSeconds * 10000到它返回的值,然后调用FileTimeToSystemTime以转换回 SystemTime。

于 2010-03-21T06:23:11.070 回答