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.

49 lines
1.5 KiB

6 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32.Security.Win32Structs;
  6. namespace Microsoft.Win32.Security
  7. {
  8. /// <summary>
  9. /// Summary description for TokenPrivileges.
  10. /// </summary>
  11. public class TokenPrivileges : CollectionBase
  12. {
  13. public TokenPrivilege this[int index]
  14. {
  15. get { return (TokenPrivilege) base.InnerList[index]; }
  16. }
  17. public void Add(TokenPrivilege privilege)
  18. {
  19. base.InnerList.Add(privilege);
  20. }
  21. public unsafe byte[] GetNativeTokenPrivileges()
  22. {
  23. Debug.Assert(Marshal.SizeOf(typeof (TOKEN_PRIVILEGES)) == 4);
  24. TOKEN_PRIVILEGES tp;
  25. tp.PrivilegeCount = (uint) Count;
  26. int cbLength =
  27. Marshal.SizeOf(typeof (TOKEN_PRIVILEGES)) +
  28. Marshal.SizeOf(typeof (LUID_AND_ATTRIBUTES))*Count;
  29. var res = new byte[cbLength];
  30. fixed (byte* privs = res)
  31. {
  32. Marshal.StructureToPtr(tp, (IntPtr) privs, false);
  33. }
  34. int resOffset = Marshal.SizeOf(typeof (TOKEN_PRIVILEGES));
  35. for (int i = 0; i < Count; i++)
  36. {
  37. byte[] luida = this[i].GetNativeLUID_AND_ATTRIBUTES();
  38. Array.Copy(luida, 0, res, resOffset, luida.Length);
  39. resOffset += luida.Length;
  40. }
  41. return res;
  42. }
  43. }
  44. }