Пример #1
0
 internal static bool RestoreTokenPrivilege(string privilegeName, ref Win32Native.TOKEN_PRIVILEGE previousPrivilegeState)
 {
     if (previousPrivilegeState.PrivilegeCount != 0)
     {
         bool flag = false;
         Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
         if (Win32Native.LookupPrivilegeValue(null, privilegeName, ref tOKENPRIVILEGE.Privilege.Luid) && tOKENPRIVILEGE.Privilege.Luid.HighPart == previousPrivilegeState.Privilege.Luid.HighPart && tOKENPRIVILEGE.Privilege.Luid.LowPart == previousPrivilegeState.Privilege.Luid.LowPart)
         {
             IntPtr currentProcess = Win32Native.GetCurrentProcess();
             if (currentProcess != IntPtr.Zero)
             {
                 IntPtr zero = IntPtr.Zero;
                 if (Win32Native.OpenProcessToken(currentProcess, 40, out zero))
                 {
                     int num  = Marshal.SizeOf(typeof(Win32Native.TOKEN_PRIVILEGE));
                     int num1 = 0;
                     if (Win32Native.AdjustTokenPrivileges(zero, false, ref previousPrivilegeState, num, out tOKENPRIVILEGE, ref num1) && Marshal.GetLastWin32Error() == 0)
                     {
                         flag = true;
                     }
                 }
                 if (zero != IntPtr.Zero)
                 {
                     Win32Native.CloseHandle(zero);
                 }
                 Win32Native.CloseHandle(currentProcess);
             }
         }
         return(flag);
     }
     else
     {
         return(true);
     }
 }
Пример #2
0
        public void Revert()
        {
            int num = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (this.NeedToRevert)
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    bool flag = true;
                    try
                    {
                        if (this.stateWasChanged && ((this.tlsContents.ReferenceCountValue > 1) || !this.tlsContents.IsImpersonating))
                        {
                            Win32Native.TOKEN_PRIVILEGE newState = new Win32Native.TOKEN_PRIVILEGE {
                                PrivilegeCount = 1
                            };
                            newState.Privilege.Luid       = this.luid;
                            newState.Privilege.Attributes = this.initialState ? 2 : 0;
                            Win32Native.TOKEN_PRIVILEGE structure = new Win32Native.TOKEN_PRIVILEGE();
                            uint returnLength = 0;
                            int  introduced5  = Marshal.SizeOf(structure);
                            if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref newState, (uint)introduced5, ref structure, ref returnLength))
                            {
                                num  = Marshal.GetLastWin32Error();
                                flag = false;
                            }
                        }
                    }
                    finally
                    {
                        if (flag)
                        {
                            this.Reset();
                        }
                    }
                }
                switch (num)
                {
                case 8:
                    throw new OutOfMemoryException();

                case 5:
                    throw new UnauthorizedAccessException();
                }
                if (num != 0)
                {
                    throw new InvalidOperationException();
                }
            }
        }
Пример #3
0
        public void Revert()
        {
            int num = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (!this.NeedToRevert)
            {
                return;
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                bool flag = true;
                try
                {
                    if (this.stateWasChanged && (this.tlsContents.ReferenceCountValue > 1 || !this.tlsContents.IsImpersonating))
                    {
                        Win32Native.TOKEN_PRIVILEGE token_PRIVILEGE = default(Win32Native.TOKEN_PRIVILEGE);
                        token_PRIVILEGE.PrivilegeCount       = 1U;
                        token_PRIVILEGE.Privilege.Luid       = this.luid;
                        token_PRIVILEGE.Privilege.Attributes = (this.initialState ? 2U : 0U);
                        Win32Native.TOKEN_PRIVILEGE structure = default(Win32Native.TOKEN_PRIVILEGE);
                        uint num2 = 0U;
                        if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref token_PRIVILEGE, (uint)Marshal.SizeOf <Win32Native.TOKEN_PRIVILEGE>(structure), ref structure, ref num2))
                        {
                            num  = Marshal.GetLastWin32Error();
                            flag = false;
                        }
                    }
                }
                finally
                {
                    if (flag)
                    {
                        this.Reset();
                    }
                }
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5)
            {
                throw new UnauthorizedAccessException();
            }
            if (num != 0)
            {
                throw new InvalidOperationException();
            }
        }
Пример #4
0
        internal static bool EnableTokenPrivilege(string privilegeName, ref Win32Native.TOKEN_PRIVILEGE oldPrivilegeState)
        {
            bool flag = false;

            Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
            if (Win32Native.LookupPrivilegeValue(null, privilegeName, ref tOKENPRIVILEGE.Privilege.Luid))
            {
                IntPtr currentProcess = Win32Native.GetCurrentProcess();
                if (currentProcess != IntPtr.Zero)
                {
                    IntPtr zero = IntPtr.Zero;
                    if (Win32Native.OpenProcessToken(currentProcess, 40, out zero))
                    {
                        Win32Native.PRIVILEGE_SET luid = new Win32Native.PRIVILEGE_SET();
                        luid.Privilege.Luid = tOKENPRIVILEGE.Privilege.Luid;
                        luid.PrivilegeCount = 1;
                        luid.Control        = 1;
                        bool flag1 = false;
                        if (!Win32Native.PrivilegeCheck(zero, ref luid, out flag1) || !flag1)
                        {
                            tOKENPRIVILEGE.PrivilegeCount       = 1;
                            tOKENPRIVILEGE.Privilege.Attributes = 2;
                            int num  = Marshal.SizeOf(typeof(Win32Native.TOKEN_PRIVILEGE));
                            int num1 = 0;
                            if (Win32Native.AdjustTokenPrivileges(zero, false, ref tOKENPRIVILEGE, num, out oldPrivilegeState, ref num1))
                            {
                                int lastWin32Error = Marshal.GetLastWin32Error();
                                if (lastWin32Error != 0)
                                {
                                    if (lastWin32Error == 0x514)
                                    {
                                        oldPrivilegeState.PrivilegeCount = 0;
                                        flag = true;
                                    }
                                }
                                else
                                {
                                    flag = true;
                                }
                            }
                        }
                        else
                        {
                            oldPrivilegeState.PrivilegeCount = 0;
                            flag = true;
                        }
                    }
                    if (zero != IntPtr.Zero)
                    {
                        Win32Native.CloseHandle(zero);
                    }
                    Win32Native.CloseHandle(currentProcess);
                }
            }
            return(flag);
        }
Пример #5
0
		internal static bool EnableTokenPrivilege(string privilegeName, ref Win32Native.TOKEN_PRIVILEGE oldPrivilegeState)
		{
			bool flag = false;
			Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
			if (Win32Native.LookupPrivilegeValue(null, privilegeName, ref tOKENPRIVILEGE.Privilege.Luid))
			{
				IntPtr currentProcess = Win32Native.GetCurrentProcess();
				if (currentProcess != IntPtr.Zero)
				{
					IntPtr zero = IntPtr.Zero;
					if (Win32Native.OpenProcessToken(currentProcess, 40, out zero))
					{
						Win32Native.PRIVILEGE_SET luid = new Win32Native.PRIVILEGE_SET();
						luid.Privilege.Luid = tOKENPRIVILEGE.Privilege.Luid;
						luid.PrivilegeCount = 1;
						luid.Control = 1;
						bool flag1 = false;
						if (!Win32Native.PrivilegeCheck(zero, ref luid, out flag1) || !flag1)
						{
							tOKENPRIVILEGE.PrivilegeCount = 1;
							tOKENPRIVILEGE.Privilege.Attributes = 2;
							int num = Marshal.SizeOf(typeof(Win32Native.TOKEN_PRIVILEGE));
							int num1 = 0;
							if (Win32Native.AdjustTokenPrivileges(zero, false, ref tOKENPRIVILEGE, num, out oldPrivilegeState, ref num1))
							{
								int lastWin32Error = Marshal.GetLastWin32Error();
								if (lastWin32Error != 0)
								{
									if (lastWin32Error == 0x514)
									{
										oldPrivilegeState.PrivilegeCount = 0;
										flag = true;
									}
								}
								else
								{
									flag = true;
								}
							}
						}
						else
						{
							oldPrivilegeState.PrivilegeCount = 0;
							flag = true;
						}
					}
					if (zero != IntPtr.Zero)
					{
						Win32Native.CloseHandle(zero);
					}
					Win32Native.CloseHandle(currentProcess);
				}
			}
			return flag;
		}
Пример #6
0
		internal static bool RestoreTokenPrivilege(string privilegeName, ref Win32Native.TOKEN_PRIVILEGE previousPrivilegeState)
		{
			if (previousPrivilegeState.PrivilegeCount != 0)
			{
				bool flag = false;
				Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
				if (Win32Native.LookupPrivilegeValue(null, privilegeName, ref tOKENPRIVILEGE.Privilege.Luid) && tOKENPRIVILEGE.Privilege.Luid.HighPart == previousPrivilegeState.Privilege.Luid.HighPart && tOKENPRIVILEGE.Privilege.Luid.LowPart == previousPrivilegeState.Privilege.Luid.LowPart)
				{
					IntPtr currentProcess = Win32Native.GetCurrentProcess();
					if (currentProcess != IntPtr.Zero)
					{
						IntPtr zero = IntPtr.Zero;
						if (Win32Native.OpenProcessToken(currentProcess, 40, out zero))
						{
							int num = Marshal.SizeOf(typeof(Win32Native.TOKEN_PRIVILEGE));
							int num1 = 0;
							if (Win32Native.AdjustTokenPrivileges(zero, false, ref previousPrivilegeState, num, out tOKENPRIVILEGE, ref num1) && Marshal.GetLastWin32Error() == 0)
							{
								flag = true;
							}
						}
						if (zero != IntPtr.Zero)
						{
							Win32Native.CloseHandle(zero);
						}
						Win32Native.CloseHandle(currentProcess);
					}
				}
				return flag;
			}
			else
			{
				return true;
			}
		}
Пример #7
0
        private void ToggleState(bool enable)
        {
            int num = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (this.needToRevert)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustRevertPrivilege"));
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                try
                {
                    this.tlsContents = Thread.GetData(tlsSlot) as TlsContents;
                    if (this.tlsContents == null)
                    {
                        this.tlsContents = new TlsContents();
                        Thread.SetData(tlsSlot, this.tlsContents);
                    }
                    else
                    {
                        this.tlsContents.IncrementReferenceCount();
                    }
                    Win32Native.TOKEN_PRIVILEGE newState = new Win32Native.TOKEN_PRIVILEGE {
                        PrivilegeCount = 1
                    };
                    newState.Privilege.Luid       = this.luid;
                    newState.Privilege.Attributes = enable ? 2 : 0;
                    Win32Native.TOKEN_PRIVILEGE structure = new Win32Native.TOKEN_PRIVILEGE();
                    uint returnLength = 0;
                    int  introduced4  = Marshal.SizeOf(structure);
                    if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref newState, (uint)introduced4, ref structure, ref returnLength))
                    {
                        num = Marshal.GetLastWin32Error();
                    }
                    else if (0x514 == Marshal.GetLastWin32Error())
                    {
                        num = 0x514;
                    }
                    else
                    {
                        this.initialState    = (structure.Privilege.Attributes & 2) != 0;
                        this.stateWasChanged = this.initialState != enable;
                        this.needToRevert    = this.tlsContents.IsImpersonating || this.stateWasChanged;
                    }
                }
                finally
                {
                    if (!this.needToRevert)
                    {
                        this.Reset();
                    }
                }
            }
            switch (num)
            {
            case 0x514:
                throw new PrivilegeNotHeldException(privileges[this.luid] as string);

            case 8:
                throw new OutOfMemoryException();

            case 5:
            case 0x543:
                throw new UnauthorizedAccessException();
            }
            if (num != 0)
            {
                throw new InvalidOperationException();
            }
        }
Пример #8
0
        public void Revert()
        {
            int error = 0;

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }

            if (!this.NeedToRevert)
            {
                return;
            }

            //
            // This code must be eagerly prepared and non-interruptible.
            //

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                //
                // The payload is entirely in the finally block
                // This is how we ensure that the code will not be
                // interrupted by catastrophic exceptions
                //
            }
            finally
            {
                bool success = true;

                try
                {
                    //
                    // Only call AdjustTokenPrivileges if we're not going to be reverting to self,
                    // on this Revert, since doing the latter obliterates the thread token anyway
                    //

                    if (this.stateWasChanged &&
                        (this.tlsContents.ReferenceCountValue > 1 ||
                         !this.tlsContents.IsImpersonating))
                    {
                        Win32Native.TOKEN_PRIVILEGE newState = new Win32Native.TOKEN_PRIVILEGE();
                        newState.PrivilegeCount       = 1;
                        newState.Privilege.Luid       = this.luid;
                        newState.Privilege.Attributes = (this.initialState ? Win32Native.SE_PRIVILEGE_ENABLED : Win32Native.SE_PRIVILEGE_DISABLED);

                        Win32Native.TOKEN_PRIVILEGE previousState = new Win32Native.TOKEN_PRIVILEGE();
                        uint previousSize = 0;

                        if (false == Win32Native.AdjustTokenPrivileges(
                                this.tlsContents.ThreadHandle,
                                false,
                                ref newState,
                                ( uint )Marshal.SizeOf(previousState),
                                ref previousState,
                                ref previousSize))
                        {
                            error   = Marshal.GetLastWin32Error();
                            success = false;
                        }
                    }
                }
                finally
                {
                    if (success)
                    {
                        this.Reset();
                    }
                }
            }

            if (error == Win32Native.ERROR_NOT_ENOUGH_MEMORY)
            {
                throw new OutOfMemoryException();
            }
            else if (error == Win32Native.ERROR_ACCESS_DENIED)
            {
                throw new UnauthorizedAccessException();
            }
            else if (error != 0)
            {
                Contract.Assert(false, string.Format(CultureInfo.InvariantCulture, "AdjustTokenPrivileges() failed with unrecognized error code {0}", error));
                throw new InvalidOperationException();
            }
        }
Пример #9
0
        private void ToggleState(bool enable)
        {
            int error = 0;

            //
            // All privilege operations must take place on the same thread
            //

            if (!this.currentThread.Equals(Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }

            //
            // This privilege was already altered and needs to be reverted before it can be altered again
            //

            if (this.needToRevert)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustRevertPrivilege"));
            }

            //
            // Need to make this block of code non-interruptible so that it would preserve
            // consistency of thread oken state even in the face of catastrophic exceptions
            //

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                //
                // The payload is entirely in the finally block
                // This is how we ensure that the code will not be
                // interrupted by catastrophic exceptions
                //
            }
            finally
            {
                try
                {
                    //
                    // Retrieve TLS state
                    //

                    this.tlsContents = Thread.GetData(tlsSlot) as TlsContents;

                    if (this.tlsContents == null)
                    {
                        this.tlsContents = new TlsContents();
                        Thread.SetData(tlsSlot, this.tlsContents);
                    }
                    else
                    {
                        this.tlsContents.IncrementReferenceCount();
                    }

                    Win32Native.TOKEN_PRIVILEGE newState = new Win32Native.TOKEN_PRIVILEGE();
                    newState.PrivilegeCount       = 1;
                    newState.Privilege.Luid       = this.luid;
                    newState.Privilege.Attributes = enable ? Win32Native.SE_PRIVILEGE_ENABLED : Win32Native.SE_PRIVILEGE_DISABLED;

                    Win32Native.TOKEN_PRIVILEGE previousState = new Win32Native.TOKEN_PRIVILEGE();
                    uint previousSize = 0;

                    //
                    // Place the new privilege on the thread token and remember the previous state.
                    //

                    if (false == Win32Native.AdjustTokenPrivileges(
                            this.tlsContents.ThreadHandle,
                            false,
                            ref newState,
                            ( uint )Marshal.SizeOf(previousState),
                            ref previousState,
                            ref previousSize))
                    {
                        error = Marshal.GetLastWin32Error();
                    }
                    else if (Win32Native.ERROR_NOT_ALL_ASSIGNED == Marshal.GetLastWin32Error())
                    {
                        error = Win32Native.ERROR_NOT_ALL_ASSIGNED;
                    }
                    else
                    {
                        //
                        // This is the initial state that revert will have to go back to
                        //

                        this.initialState = ((previousState.Privilege.Attributes & Win32Native.SE_PRIVILEGE_ENABLED) != 0);

                        //
                        // Remember whether state has changed at all
                        //

                        this.stateWasChanged = (this.initialState != enable);

                        //
                        // If we had to impersonate, or if the privilege state changed we'll need to revert
                        //

                        this.needToRevert = this.tlsContents.IsImpersonating || this.stateWasChanged;
                    }
                }
                finally
                {
                    if (!this.needToRevert)
                    {
                        this.Reset();
                    }
                }
            }

            if (error == Win32Native.ERROR_NOT_ALL_ASSIGNED)
            {
                throw new PrivilegeNotHeldException(privileges[this.luid] as string);
            }
            if (error == Win32Native.ERROR_NOT_ENOUGH_MEMORY)
            {
                throw new OutOfMemoryException();
            }
            else if (error == Win32Native.ERROR_ACCESS_DENIED ||
                     error == Win32Native.ERROR_CANT_OPEN_ANONYMOUS)
            {
                throw new UnauthorizedAccessException();
            }
            else if (error != 0)
            {
                Contract.Assert(false, string.Format(CultureInfo.InvariantCulture, "AdjustTokenPrivileges() failed with unrecognized error code {0}", error));
                throw new InvalidOperationException();
            }
        }
Пример #10
0
        public void Revert()
        {
            int num = 0;

            if (!this.currentThread.Equals((object)Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (!this.NeedToRevert)
            {
                return;
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                bool flag = true;
                try
                {
                    if (this.stateWasChanged)
                    {
                        if (this.tlsContents.ReferenceCountValue <= 1)
                        {
                            if (this.tlsContents.IsImpersonating)
                            {
                                goto label_16;
                            }
                        }
                        Win32Native.TOKEN_PRIVILEGE NewState = new Win32Native.TOKEN_PRIVILEGE();
                        NewState.PrivilegeCount       = 1U;
                        NewState.Privilege.Luid       = this.luid;
                        NewState.Privilege.Attributes = this.initialState ? 2U : 0U;
                        Win32Native.TOKEN_PRIVILEGE PreviousState = new Win32Native.TOKEN_PRIVILEGE();
                        uint ReturnLength = 0;
                        if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref NewState, (uint)Marshal.SizeOf <Win32Native.TOKEN_PRIVILEGE>(PreviousState), out PreviousState, out ReturnLength))
                        {
                            num  = Marshal.GetLastWin32Error();
                            flag = false;
                        }
                    }
                }
                finally
                {
                    if (flag)
                    {
                        this.Reset();
                    }
                }
                label_16 :;
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5)
            {
                throw new UnauthorizedAccessException();
            }
            if (num != 0)
            {
                throw new InvalidOperationException();
            }
        }
Пример #11
0
        private void ToggleState(bool enable)
        {
            int num = 0;

            if (!this.currentThread.Equals((object)Thread.CurrentThread))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustBeSameThread"));
            }
            if (this.needToRevert)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustRevertPrivilege"));
            }
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            finally
            {
                try
                {
                    this.tlsContents = Thread.GetData(Privilege.tlsSlot) as Privilege.TlsContents;
                    if (this.tlsContents == null)
                    {
                        this.tlsContents = new Privilege.TlsContents();
                        Thread.SetData(Privilege.tlsSlot, (object)this.tlsContents);
                    }
                    else
                    {
                        this.tlsContents.IncrementReferenceCount();
                    }
                    Win32Native.TOKEN_PRIVILEGE NewState = new Win32Native.TOKEN_PRIVILEGE();
                    NewState.PrivilegeCount       = 1U;
                    NewState.Privilege.Luid       = this.luid;
                    NewState.Privilege.Attributes = enable ? 2U : 0U;
                    Win32Native.TOKEN_PRIVILEGE PreviousState = new Win32Native.TOKEN_PRIVILEGE();
                    uint ReturnLength = 0;
                    if (!Win32Native.AdjustTokenPrivileges(this.tlsContents.ThreadHandle, false, ref NewState, (uint)Marshal.SizeOf <Win32Native.TOKEN_PRIVILEGE>(PreviousState), out PreviousState, out ReturnLength))
                    {
                        num = Marshal.GetLastWin32Error();
                    }
                    else if (1300 == Marshal.GetLastWin32Error())
                    {
                        num = 1300;
                    }
                    else
                    {
                        this.initialState    = (PreviousState.Privilege.Attributes & 2U) > 0U;
                        this.stateWasChanged = this.initialState != enable;
                        this.needToRevert    = this.tlsContents.IsImpersonating || this.stateWasChanged;
                    }
                }
                finally
                {
                    if (!this.needToRevert)
                    {
                        this.Reset();
                    }
                }
            }
            if (num == 1300)
            {
                throw new PrivilegeNotHeldException(Privilege.privileges[(object)this.luid] as string);
            }
            if (num == 8)
            {
                throw new OutOfMemoryException();
            }
            if (num == 5 || num == 1347)
            {
                throw new UnauthorizedAccessException();
            }
            if (num != 0)
            {
                throw new InvalidOperationException();
            }
        }
Пример #12
0
        private void ConnectInvokeWmi()
        {
            ManagementObject managementObject;
            object           obj;
            string           str;
            InvokeWmiMethod  invokeWmiMethod = (InvokeWmiMethod)this.wmiObject;

            this.state = WmiState.Running;
            this.RaiseWmiOperationState(null, WmiState.Running);
            if (invokeWmiMethod.InputObject == null)
            {
                ConnectionOptions connectionOption = invokeWmiMethod.GetConnectionOption();
                ManagementPath    managementPath   = null;
                if (invokeWmiMethod.Path != null)
                {
                    managementPath = new ManagementPath(invokeWmiMethod.Path);
                    if (!string.IsNullOrEmpty(managementPath.NamespacePath))
                    {
                        if (invokeWmiMethod.namespaceSpecified)
                        {
                            InvalidOperationException invalidOperationException = new InvalidOperationException("NamespaceSpecifiedWithPath");
                            this.internalException = invalidOperationException;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    else
                    {
                        managementPath.NamespacePath = invokeWmiMethod.Namespace;
                    }
                    if (!(managementPath.Server != ".") || !invokeWmiMethod.serverNameSpecified)
                    {
                        if (!(managementPath.Server == ".") || !invokeWmiMethod.serverNameSpecified)
                        {
                            this.computerName = managementPath.Server;
                        }
                    }
                    else
                    {
                        InvalidOperationException invalidOperationException1 = new InvalidOperationException("ComputerNameSpecifiedWithPath");
                        this.internalException = invalidOperationException1;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                        return;
                    }
                }
                bool flag            = false;
                bool enablePrivilege = false;
                Win32Native.TOKEN_PRIVILEGE tOKENPRIVILEGE = new Win32Native.TOKEN_PRIVILEGE();
                try
                {
                    try
                    {
                        enablePrivilege = this.NeedToEnablePrivilege(this.computerName, invokeWmiMethod.Name, ref flag);
                        if (!enablePrivilege || flag && ComputerWMIHelper.EnableTokenPrivilege("SeShutdownPrivilege", ref tOKENPRIVILEGE) || !flag && ComputerWMIHelper.EnableTokenPrivilege("SeRemoteShutdownPrivilege", ref tOKENPRIVILEGE))
                        {
                            if (invokeWmiMethod.Path == null)
                            {
                                ManagementScope managementScope = new ManagementScope(WMIHelper.GetScopeString(this.computerName, invokeWmiMethod.Namespace), connectionOption);
                                ManagementClass managementClass = new ManagementClass(invokeWmiMethod.Class);
                                managementObject       = managementClass;
                                managementObject.Scope = managementScope;
                            }
                            else
                            {
                                managementPath.Server = this.computerName;
                                if (!managementPath.IsClass)
                                {
                                    ManagementObject managementObject1 = new ManagementObject(managementPath);
                                    managementObject = managementObject1;
                                }
                                else
                                {
                                    ManagementClass managementClass1 = new ManagementClass(managementPath);
                                    managementObject = managementClass1;
                                }
                                ManagementScope managementScope1 = new ManagementScope(managementPath, connectionOption);
                                managementObject.Scope = managementScope1;
                            }
                            ManagementBaseObject methodParameters = managementObject.GetMethodParameters(invokeWmiMethod.Name);
                            if (invokeWmiMethod.ArgumentList != null)
                            {
                                int length = (int)invokeWmiMethod.ArgumentList.Length;
                                foreach (PropertyData property in methodParameters.Properties)
                                {
                                    if (length == 0)
                                    {
                                        break;
                                    }
                                    property.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length];
                                    length--;
                                }
                            }
                            if (!enablePrivilege)
                            {
                                managementObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters, null);
                            }
                            else
                            {
                                ManagementBaseObject managementBaseObject = managementObject.InvokeMethod(invokeWmiMethod.Name, methodParameters, null);
                                int num = Convert.ToInt32(managementBaseObject["ReturnValue"], CultureInfo.CurrentCulture);
                                if (num == 0)
                                {
                                    this.ShutdownComplete.SafeInvoke <EventArgs>(this, null);
                                }
                                else
                                {
                                    Win32Exception win32Exception = new Win32Exception(num);
                                    this.internalException = win32Exception;
                                    this.state             = WmiState.Failed;
                                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                                }
                            }
                        }
                        else
                        {
                            string privilegeNotEnabled = ComputerResources.PrivilegeNotEnabled;
                            string str1 = this.computerName;
                            if (flag)
                            {
                                obj = "SeShutdownPrivilege";
                            }
                            else
                            {
                                obj = "SeRemoteShutdownPrivilege";
                            }
                            string str2 = StringUtil.Format(privilegeNotEnabled, str1, obj);
                            InvalidOperationException invalidOperationException2 = new InvalidOperationException(str2);
                            this.internalException = invalidOperationException2;
                            this.state             = WmiState.Failed;
                            this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                            return;
                        }
                    }
                    catch (ManagementException managementException1)
                    {
                        ManagementException managementException = managementException1;
                        this.internalException = managementException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (COMException cOMException1)
                    {
                        COMException cOMException = cOMException1;
                        this.internalException = cOMException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                    catch (UnauthorizedAccessException unauthorizedAccessException1)
                    {
                        UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1;
                        this.internalException = unauthorizedAccessException;
                        this.state             = WmiState.Failed;
                        this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                    }
                }
                finally
                {
                    if (enablePrivilege)
                    {
                        if (flag)
                        {
                            str = "SeShutdownPrivilege";
                        }
                        else
                        {
                            str = "SeRemoteShutdownPrivilege";
                        }
                        ComputerWMIHelper.RestoreTokenPrivilege(str, ref tOKENPRIVILEGE);
                    }
                }
                return;
            }
            else
            {
                try
                {
                    ManagementBaseObject methodParameters1 = invokeWmiMethod.InputObject.GetMethodParameters(invokeWmiMethod.Name);
                    if (invokeWmiMethod.ArgumentList != null)
                    {
                        int length1 = (int)invokeWmiMethod.ArgumentList.Length;
                        foreach (PropertyData argumentList in methodParameters1.Properties)
                        {
                            if (length1 == 0)
                            {
                                break;
                            }
                            argumentList.Value = invokeWmiMethod.ArgumentList[(int)invokeWmiMethod.ArgumentList.Length - length1];
                            length1--;
                        }
                    }
                    invokeWmiMethod.InputObject.InvokeMethod(this.results, invokeWmiMethod.Name, methodParameters1, null);
                }
                catch (ManagementException managementException3)
                {
                    ManagementException managementException2 = managementException3;
                    this.internalException = managementException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    this.internalException = cOMException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException3)
                {
                    UnauthorizedAccessException unauthorizedAccessException2 = unauthorizedAccessException3;
                    this.internalException = unauthorizedAccessException2;
                    this.state             = WmiState.Failed;
                    this.RaiseOperationCompleteEvent(null, OperationState.StopComplete);
                }
                return;
            }
        }