我在一个应用程序中编写了一个小工具,用于从时间服务器同步时间,它使用 Windows API 函数GetSystemTime
和SetSystemTime
. 一切正常,但现在每次我打电话时Get/SetSystemTime
都会出错:
FatalExecutionEngineError was detected Message: The runtime has encountered a fatal error. The address of the error was at 0x792bee10, on thread 0x48c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
现在,由于唯一不安全的代码是对 Win32 函数的调用(这甚至是不安全的吗??)它似乎不能在我的代码中 - 而且我没有改变任何东西,因为它实际上正在工作......
有什么想法可能会在这里发生吗?
Win32 调用使用:
public class SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Millisecond;
public static implicit operator SystemTime(DateTime dt)
{
SystemTime rval = new SystemTime();
rval.Year = (ushort)dt.Year;
rval.Month = (ushort)dt.Month;
rval.Day = (ushort)dt.Day;
rval.DayOfWeek = (ushort)dt.DayOfWeek;
rval.Hour = (ushort)dt.Hour;
rval.Minute = (ushort)dt.Minute;
rval.Second = (ushort)dt.Second;
rval.Millisecond = (ushort)dt.Millisecond;
return rval;
}
public static implicit operator DateTime(SystemTime st)
{
return new DateTime(st.Year, st.Month, st.Day, st.Hour, st.Minute, st.Second, st.Millisecond);
}
};
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime")]
public extern static void Win32GetSystemTime(ref SystemTime sysTime);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime")]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
编辑:现在相同的代码(未修改)给了我一个 AccessViolation ???
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.