Ejemplo n.º 1
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;
            }
        }