我已經在一個應用程序中編寫了一個小實用程序,它將時間服務器與使用Windows API函數GetSystemTime
和SetSystemTime
的時間服務器進行同步。這是所有工作正常,但現在我每次打電話Get/SetSystemTime
時候,我得到一個錯誤:調用GetSystemTime/SetSystemTime時發生FatalExecutionEngineError
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函數的調用(是,即使是不安全的?)好像它不能在我的代碼 - 這並沒有改變anythig,因爲它實際上正在工作...
任何想法可能會發生什麼嗎?
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);
編輯:
Win32的調用都是使用由現在非常相同的代碼(未修改)是給我一個AccessViolation ???
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
您使用GetSystemTime的P/Invoke簽名? – Mormegil 2011-03-14 10:53:41
請注意,通常使用http://pinvoke.net/中的P/Invoke定義是最好的選擇。 (至少作爲一個起點。) – Mormegil 2011-03-18 23:44:53