简体   繁体   English

SetLocalTime使用远程调试提供错误87

[英]SetLocalTime gives Error 87 with Remote Debugging

I have the following C# code: 我有以下C#代码:

 public static void SetDateTime(System.DateTime datetime)
    {
        var systemNew = new SystemTime
            {
                wDay = (ushort)datetime.Day,
                wMonth = (ushort)datetime.Month,
                wYear = (ushort)datetime.Year,
                wHour = (ushort)datetime.Hour,
                wMinute = (ushort)datetime.Minute,
                wSecond = (ushort)datetime.Second,
                wMilliseconds = (ushort)datetime.Millisecond
            };

        // update system clock
        NativeMethods.SetLocalTime(ref systemNew);
        int errorCode = Marshal.GetLastWin32Error();
        if (errorCode != 0)
        {
            Debug.WriteLine("SetDateTime error: {0}", errorCode);
        }
    }

internal struct SystemTime
{
    internal ushort wDay;
    internal ushort wDayOfWeek;
    internal ushort wHour;
    internal ushort wMilliseconds;
    internal ushort wMinute;
    internal ushort wMonth;
    internal ushort wSecond;
    internal ushort wYear;
}

internal static class NativeMethods
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int SetLocalTime(ref SystemTime lpSystemTime);
}

However, this gives me errorcode 87 everytime I call the method, any ideas? 但是,每当我调用方法时,这给了我错误代码87,任何想法?

Edit: I checked it on my local machine and it works fine there. 编辑:我在我的本地机器上检查过,它在那里工作正常。 It doesn't work with Remote Debugging from Visual Studio, even though the remote process runs under the same account as my local account and it's an administrator on that system. 它不适用于Visual Studio中的远程调试,即使远程进程在与我的本地帐户相同的帐户下运行,并且它是该系统的管理员。

The code which you posted don't contain any value of datetime which you used to reproduce the error. 您发布的代码不包含用于重现错误的datetime

For example DateTime.Year property can have the value between 1 and 9999 (see here ). 例如, DateTime.Year属性的值可以介于1和9999之间(请参见此处 )。 On the other side the wYear field of SYSTEMTIME can be between 1601 and 30827. 另一方面, SYSTEMTIMEwYear字段可以在1601和30827之间。

So I recommend you first of all to verify old values of input struct which you use as the input to SetLocalTime . 因此,我建议您首先验证输入结构的旧值 ,并将其用作SetLocalTime的输入。

UPDATED : It seems to me that I know the origin of the problem now. 更新 :在我看来,我现在知道问题的根源。 You defined SystemTime with the wrong order of fields as: 您使用错误的字段顺序定义SystemTime

internal struct SystemTime
{
    internal ushort wDay;
    internal ushort wDayOfWeek;
    internal ushort wHour;
    internal ushort wMilliseconds;
    internal ushort wMinute;
    internal ushort wMonth;
    internal ushort wSecond;
    internal ushort wYear;
}

but SYSTEMTIME is defined as SYSTEMTIME定义为

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM