PermissionAPI.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace Azylee.Core.WindowsUtils.APIUtils
  14. {
  15. public class PermissionAPI
  16. {
  17. /// <summary>
  18. /// The AllocateAndInitializeSid function allocates and initializes a security identifier (SID) with up to eight subauthorities.
  19. /// </summary>
  20. /// <param name="pIdentifierAuthority">Pointer to a SID_IDENTIFIER_AUTHORITY structure, giving the top-level identifier authority value to set in the SID.</param>
  21. /// <param name="nSubAuthorityCount">Specifies the number of subauthorities to place in the SID. This parameter also identifies how many of the subauthority parameters have meaningful values. This parameter must contain a value from 1 to 8.</param>
  22. /// <param name="dwSubAuthority0">Subauthority value to place in the SID.</param>
  23. /// <param name="dwSubAuthority1">Subauthority value to place in the SID.</param>
  24. /// <param name="dwSubAuthority2">Subauthority value to place in the SID.</param>
  25. /// <param name="dwSubAuthority3">Subauthority value to place in the SID.</param>
  26. /// <param name="dwSubAuthority4">Subauthority value to place in the SID.</param>
  27. /// <param name="dwSubAuthority5">Subauthority value to place in the SID.</param>
  28. /// <param name="dwSubAuthority6">Subauthority value to place in the SID.</param>
  29. /// <param name="dwSubAuthority7">Subauthority value to place in the SID.</param>
  30. /// <param name="pSid">Pointer to a variable that receives the pointer to the allocated and initialized SID structure.</param>
  31. /// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
  32. [DllImport("advapi32.dll")]
  33. public extern static int AllocateAndInitializeSid(byte[] pIdentifierAuthority, byte nSubAuthorityCount, int dwSubAuthority0, int dwSubAuthority1, int dwSubAuthority2, int dwSubAuthority3, int dwSubAuthority4, int dwSubAuthority5, int dwSubAuthority6, int dwSubAuthority7, out IntPtr pSid);
  34. /// <summary>
  35. /// The CheckTokenMembership function determines whether a specified SID is enabled in an access token.
  36. /// </summary>
  37. /// <param name="TokenHandle">Handle to an access token. The handle must have TOKEN_QUERY access to the token. The token must be an impersonation token.</param>
  38. /// <param name="SidToCheck">Pointer to a SID structure. The CheckTokenMembership function checks for the presence of this SID in the user and group SIDs of the access token.</param>
  39. /// <param name="IsMember">Pointer to a variable that receives the results of the check. If the SID is present and has the SE_GROUP_ENABLED attribute, IsMember returns TRUE; otherwise, it returns FALSE.</param>
  40. /// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
  41. [DllImport("advapi32.dll")]
  42. public extern static int CheckTokenMembership(IntPtr TokenHandle, IntPtr SidToCheck, ref int IsMember);
  43. /// <summary>
  44. /// The FreeSid function frees a security identifier (SID) previously allocated by using the AllocateAndInitializeSid function.
  45. /// </summary>
  46. /// <param name="pSid">Pointer to the SID structure to free.</param>
  47. /// <returns>This function does not return a value.</returns>
  48. [DllImport("advapi32.dll")]
  49. public extern static IntPtr FreeSid(IntPtr pSid);
  50. }
  51. }