選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NativeMethods.Utilities.cs 6.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using Alphaleonis.Win32.Security;
  22. using System;
  23. using System.Diagnostics.CodeAnalysis;
  24. using System.Globalization;
  25. using System.Runtime.InteropServices;
  26. using System.Security;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. internal static partial class NativeMethods
  30. {
  31. internal static uint GetHighOrderDword(long highPart)
  32. {
  33. return (uint)((highPart >> 32) & 0xFFFFFFFF);
  34. }
  35. internal static uint GetLowOrderDword(long lowPart)
  36. {
  37. return (uint)(lowPart & 0xFFFFFFFF);
  38. }
  39. /// <summary>Check is the current handle is not null, not closed and not invalid.</summary>
  40. /// <param name="handle">The current handle to check.</param>
  41. /// <param name="throwException"><see langword="true"/> will throw an <exception cref="Resources.Handle_Is_Invalid"/>, <see langword="false"/> will not raise this exception..</param>
  42. /// <returns><see langword="true"/> on success, <see langword="false"/> otherwise.</returns>
  43. /// <exception cref="ArgumentException"/>
  44. internal static bool IsValidHandle(SafeHandle handle, bool throwException = true)
  45. {
  46. if (handle == null || handle.IsClosed || handle.IsInvalid)
  47. {
  48. if (handle != null)
  49. handle.Close();
  50. if (throwException)
  51. throw new ArgumentException(Resources.Handle_Is_Invalid);
  52. return false;
  53. }
  54. return true;
  55. }
  56. /// <summary>Check is the current handle is not null, not closed and not invalid.</summary>
  57. /// <param name="handle">The current handle to check.</param>
  58. /// <param name="lastError">The result of Marshal.GetLastWin32Error()</param>
  59. /// <param name="throwException"><see langword="true"/> will throw an <exception cref="Resources.Handle_Is_Invalid_Win32Error"/>, <see langword="false"/> will not raise this exception..</param>
  60. /// <returns><see langword="true"/> on success, <see langword="false"/> otherwise.</returns>
  61. /// <exception cref="ArgumentException"/>
  62. internal static bool IsValidHandle(SafeHandle handle, int lastError, bool throwException = true)
  63. {
  64. if (handle == null || handle.IsClosed || handle.IsInvalid)
  65. {
  66. if (handle != null)
  67. handle.Close();
  68. if (throwException)
  69. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.Handle_Is_Invalid_Win32Error, lastError));
  70. return false;
  71. }
  72. return true;
  73. }
  74. internal static long LuidToLong(Luid luid)
  75. {
  76. ulong high = (((ulong)luid.HighPart) << 32);
  77. ulong low = (((ulong)luid.LowPart) & 0x00000000FFFFFFFF);
  78. return unchecked((long)(high | low));
  79. }
  80. internal static Luid LongToLuid(long lluid)
  81. {
  82. return new Luid { HighPart = (uint)(lluid >> 32), LowPart = (uint)(lluid & 0xFFFFFFFF) };
  83. }
  84. /// <summary>
  85. /// Controls whether the system will handle the specified types of serious errors or whether the process will handle them.
  86. /// </summary>
  87. /// <remarks>
  88. /// Because the error mode is set for the entire process, you must ensure that multi-threaded applications do not set different error-
  89. /// mode attributes. Doing so can lead to inconsistent error handling.
  90. /// </remarks>
  91. /// <remarks>Minimum supported client: Windows XP [desktop apps only].</remarks>
  92. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only].</remarks>
  93. /// <param name="uMode">The mode.</param>
  94. /// <returns>The return value is the previous state of the error-mode bit attributes.</returns>
  95. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  96. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  97. [return: MarshalAs(UnmanagedType.U4)]
  98. private static extern ErrorMode SetErrorMode(ErrorMode uMode);
  99. /// <summary>
  100. /// Controls whether the system will handle the specified types of serious errors or whether the calling thread will handle them.
  101. /// </summary>
  102. /// <remarks>
  103. /// Because the error mode is set for the entire process, you must ensure that multi-threaded applications do not set different error-
  104. /// mode attributes. Doing so can lead to inconsistent error handling.
  105. /// </remarks>
  106. /// <remarks>Minimum supported client: Windows 7 [desktop apps only].</remarks>
  107. /// <remarks>Minimum supported server: Windows Server 2008 R2 [desktop apps only].</remarks>
  108. /// <param name="dwNewMode">The new mode.</param>
  109. /// <param name="lpOldMode">[out] The old mode.</param>
  110. /// <returns>The return value is the previous state of the error-mode bit attributes.</returns>
  111. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  112. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  113. [return: MarshalAs(UnmanagedType.Bool)]
  114. private static extern bool SetThreadErrorMode(ErrorMode dwNewMode, [MarshalAs(UnmanagedType.U4)] out ErrorMode lpOldMode);
  115. internal static long ToLong(uint highPart, uint lowPart)
  116. {
  117. return (((long)highPart) << 32) | (((long)lowPart) & 0xFFFFFFFF);
  118. }
  119. }
  120. }