コード例 #1
0
        public static bool EnablePrivilege(string privilege)
        {
            try
            {
                int token = 0;
                int retVal = 0;

                var tp = new TOKEN_PRIVILEGE();
                var ld = new LUID();

                retVal = OpenProcessToken(GetCurrentProcess(), TokenAdjustPrivileges | TokenQuery, ref token);
                retVal = LookupPrivilegeValue(null, privilege, ref ld);
                tp.PrivilegeCount = 1;

                var luidAndAtt = new LUID_AND_ATTRIBUTES {Attributes = SePrivilegeEnabled, Luid = ld};
                tp.Privilege = luidAndAtt;

                retVal = AdjustTokenPrivileges(token, 0, ref tp, 1024, 0, 0);
                return true;
            }
            catch
            {
                return false;
            }
        }
コード例 #2
0
        public static bool DisablePrivilege(string privilege)
        {
            try
            {
                var token = 0;
                var retVal = 0;

                var tp = new TOKEN_PRIVILEGE();
                var ld = new LUID();

                retVal = OpenProcessToken(GetCurrentProcess(), TokenAdjustPrivileges | TokenQuery, ref token);
                retVal = LookupPrivilegeValue(null, privilege, ref ld);
                tp.PrivilegeCount = 1;
                // TP.Attributes should be none (not set) to disable privilege
                var luidAndAtt = new LUID_AND_ATTRIBUTES {Luid = ld};
                tp.Privilege = luidAndAtt;

                retVal = AdjustTokenPrivileges(token, 0, ref tp, 1024, 0, 0);
                return true;
            }
            catch
            {
                return false;
            }
        }