Ejemplo n.º 1
0
 internal static extern bool AdjustTokenPrivileges(
     IntPtr htok,
     bool disall,
     ref TokenPrivelege newst,
     Int32 len,
     IntPtr prev,
     IntPtr relen);
Ejemplo n.º 2
0
        public static void ObtainPrivileges(string privilege)
        {
            var hToken = IntPtr.Zero;

            if (!NativeMethods.OpenProcessToken(NativeMethods.GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken))
            {
                throw new InvalidOperationException("OpenProcessToken failed!");
            }

            var tp = new TokenPrivelege {
                Count = 1, Luid = 0, Attr = SE_PRIVILEGE_ENABLED
            };

            if (!NativeMethods.LookupPrivilegeValue(null, privilege, ref tp.Luid))
            {
                throw new InvalidOperationException("LookupPrivilegeValue failed!");
            }
            if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero))
            {
                throw new InvalidOperationException("AdjustTokenPrivileges failed!");
            }
        }