internal TokenPrivileges(UnmanagedHeapAlloc ptr) { MemoryMarshaler m = new MemoryMarshaler(ptr.Ptr); TOKEN_PRIVILEGES privs = (TOKEN_PRIVILEGES)m.ParseStruct(typeof(TOKEN_PRIVILEGES)); for(int i = 0 ; i < privs.PrivilegeCount; i++) { TokenPrivilege priv = new TokenPrivilege(m); base.InnerList.Add(priv); } }
/// <summary> /// Generic call to Win32.GetTokenInformation. The returned object contains the /// token information in unmanaged heap (UnmanagedHeapAlloc). /// It must be disposed by the caller. /// </summary> /// <param name="TokenInformationClass">The type of info to retrieve</param> /// <returns>The ptr to the token information in unmanaged memory</returns> private UnmanagedHeapAlloc GetTokenInformation(TokenInformationClass TokenInformationClass) { DWORD cbLength; BOOL rc = Win32.GetTokenInformation(_handle, TokenInformationClass, IntPtr.Zero, 0, out cbLength); switch(Marshal.GetLastWin32Error()) { case Win32.SUCCESS: throw new ArgumentException("Unexpected error code returned by GetTokenInformation"); case Win32.ERROR_BAD_LENGTH: // Special case for TokenSessionId. Win32 doesn't want to return // us the size of a DWORD. cbLength = (DWORD)Marshal.SizeOf(typeof(DWORD)); goto case Win32.ERROR_INSUFFICIENT_BUFFER; case Win32.ERROR_INSUFFICIENT_BUFFER: UnmanagedHeapAlloc res = new UnmanagedHeapAlloc(cbLength); try { rc = Win32.GetTokenInformation(_handle, TokenInformationClass, res.Ptr, res.Size, out cbLength); Win32.CheckCall(rc); } catch { res.Dispose(); throw; } return res; default: Win32.ThrowLastError(); return null; // uneeded } }