You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.0 KiB

6 years ago
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Microsoft.Win32.Security.Win32Structs;
  4. namespace Microsoft.Win32.Security
  5. {
  6. using HANDLE = IntPtr;
  7. using DWORD = UInt32;
  8. using BOOL = Int32;
  9. using LPVOID = IntPtr;
  10. using PACL = IntPtr;
  11. using PTOKEN_PRIVILEGES = IntPtr;
  12. using PSECURITY_DESCRIPTOR = IntPtr;
  13. using SECURITY_DESCRIPTOR_CONTROL = SecurityDescriptorControlFlags;
  14. using LPCTSTR = String;
  15. using HKEY = IntPtr;
  16. using LONG = Int32;
  17. /// <summary>
  18. /// Summary description for Win32Interop.
  19. /// </summary>
  20. public class Win32
  21. {
  22. public const BOOL FALSE = 0;
  23. public const BOOL TRUE = 1;
  24. public const int SUCCESS = 0;
  25. public const int ERROR_SUCCESS = 0;
  26. public const int ERROR_INSUFFICIENT_BUFFER = 122;
  27. public const int ERROR_NOT_ALL_ASSIGNED = 1300;
  28. public const int ERROR_NONE_MAPPED = 1332;
  29. private const string Kernel32 = "kernel32.dll";
  30. private const string Advapi32 = "Advapi32.dll";
  31. public static DWORD GetLastError()
  32. {
  33. return (DWORD) Marshal.GetLastWin32Error();
  34. }
  35. public static void ThrowLastError()
  36. {
  37. Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
  38. }
  39. public static void CheckCall(bool funcResult)
  40. {
  41. if (! funcResult)
  42. {
  43. ThrowLastError();
  44. }
  45. }
  46. public static void CheckCall(BOOL funcResult)
  47. {
  48. CheckCall(funcResult != 0);
  49. }
  50. public static void CheckCall(HANDLE funcResult)
  51. {
  52. CheckCall(!IsNullHandle(funcResult));
  53. }
  54. public static bool IsNullHandle(HANDLE ptr)
  55. {
  56. return (ptr == IntPtr.Zero);
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////
  59. ///
  60. /// KERNEL32.DLL
  61. ///
  62. [DllImport(Kernel32, CallingConvention = CallingConvention.Winapi)]
  63. public static extern void SetLastError(DWORD dwErrCode);
  64. /*
  65. [DllImport(Kernel32, CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Auto)]
  66. public static extern HANDLE CreateEvent(
  67. [In, Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(SecurityAttributesMarshaler))]
  68. SecurityAttributes lpEventAttributes, // SA
  69. BOOL bManualReset, // reset type
  70. BOOL bInitialState, // initial state
  71. string lpName // object name
  72. );
  73. */
  74. [DllImport(Kernel32, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
  75. public static extern HANDLE OpenProcess(ProcessAccessType dwDesiredAccess, BOOL bInheritHandle,
  76. DWORD dwProcessId);
  77. [DllImport(Kernel32, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
  78. public static extern BOOL CloseHandle(HANDLE handle);
  79. ///////////////////////////////////////////////////////////////////////////////
  80. ///
  81. /// ADVAPI32.DLL
  82. ///
  83. [DllImport(Advapi32, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
  84. public static extern BOOL OpenProcessToken(HANDLE hProcess, TokenAccessType dwDesiredAccess, out HANDLE hToken);
  85. [DllImport(Advapi32, CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
  86. public static extern BOOL LookupPrivilegeValue(string lpSystemName, string lpName, out LUID Luid);
  87. [DllImport(Advapi32, CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
  88. public static extern BOOL AdjustTokenPrivileges(
  89. HANDLE TokenHandle,
  90. BOOL DisableAllPrivileges,
  91. PTOKEN_PRIVILEGES NewState,
  92. DWORD BufferLength,
  93. PTOKEN_PRIVILEGES PreviousState,
  94. out DWORD ReturnLength);
  95. }
  96. }