SafeNativeMethods.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Runtime;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.Versioning;
  9. using System.Security;
  10. [SuppressUnmanagedCodeSecurity]
  11. internal static class SafeNativeMethods
  12. {
  13. public const string KERNEL32 = "kernel32.dll";
  14. [DllImport(KERNEL32, SetLastError = false)]
  15. [ResourceExposure(ResourceScope.None)]
  16. static extern uint GetSystemTimeAdjustment(
  17. [Out] out int adjustment,
  18. [Out] out uint increment,
  19. [Out] out uint adjustmentDisabled
  20. );
  21. [DllImport(KERNEL32, SetLastError = true)]
  22. [ResourceExposure(ResourceScope.None)]
  23. public static extern void GetSystemTimeAsFileTime(out long time);
  24. [Fx.Tag.SecurityNote(Critical = "Calls critical method GetSystemTimeAdjustment.",
  25. Safe = "Method is a SafeNativeMethod.")]
  26. [SecuritySafeCritical]
  27. internal static long GetSystemTimeResolution()
  28. {
  29. int dummyAdjustment;
  30. uint increment;
  31. uint dummyAdjustmentDisabled;
  32. if (GetSystemTimeAdjustment(out dummyAdjustment, out increment, out dummyAdjustmentDisabled) != 0)
  33. {
  34. return (long)increment;
  35. }
  36. // Assume the default, which is around 15 milliseconds.
  37. return 15 * TimeSpan.TicksPerMillisecond;
  38. }
  39. }
  40. }