Example #1
0
		public void InvokeMethod(ManagementOperationObserver watcher, string methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
		{
			InvokeMethodOptions invokeMethodOption;
			if (this.path == null || this.path.Path.Length == 0)
			{
				throw new InvalidOperationException();
			}
			else
			{
				if (watcher != null)
				{
					if (methodName != null)
					{
						this.Initialize(false);
						if (options != null)
						{
							invokeMethodOption = (InvokeMethodOptions)options.Clone();
						}
						else
						{
							invokeMethodOption = new InvokeMethodOptions();
						}
						InvokeMethodOptions invokeMethodOption1 = invokeMethodOption;
						if (watcher.HaveListenersForProgress)
						{
							invokeMethodOption1.SendStatus = true;
						}
						WmiEventSink newSink = watcher.GetNewSink(this.scope, invokeMethodOption1.Context);
						SecurityHandler securityHandler = this.scope.GetSecurityHandler();
						IWbemClassObjectFreeThreaded wbemClassObjectFreeThreaded = null;
						if (inParameters != null)
						{
							wbemClassObjectFreeThreaded = inParameters.wbemObject;
						}
						int num = this.scope.GetSecuredIWbemServicesHandler(this.scope.GetIWbemServices()).ExecMethodAsync_(this.path.RelativePath, methodName, invokeMethodOption1.Flags, invokeMethodOption1.GetContext(), wbemClassObjectFreeThreaded, newSink.Stub);
						if (securityHandler != null)
						{
							securityHandler.Reset();
						}
						if (num < 0)
						{
							watcher.RemoveSink(newSink);
							if (((long)num & (long)-4096) != (long)-2147217408)
							{
								Marshal.ThrowExceptionForHR(num);
							}
							else
							{
								ManagementException.ThrowWithExtendedInfo((ManagementStatus)num);
								return;
							}
						}
						return;
					}
					else
					{
						throw new ArgumentNullException("methodName");
					}
				}
				else
				{
					throw new ArgumentNullException("watcher");
				}
			}
		}
        /// <summary>
        ///    <para>Invokes a method on the object, asynchronously.</para>
        /// </summary>
        /// <param name='watcher'>A <see cref='System.Management.ManagementOperationObserver'/> used to handle the asynchronous execution's progress and results.</param>
        /// <param name=' methodName'>The name of the method to be executed.</param>
        /// <param name=' inParameters'><para>A <see cref='System.Management.ManagementBaseObject'/> containing the input parameters for the method.</para></param>
        /// <param name=' options'>An <see cref='System.Management.InvokeMethodOptions'/> containing additional options used to execute the method.</param>
        /// <remarks>
        ///    <para>The method invokes the specified method execution and then 
        ///       returns. Progress and results are reported through events on the <see cref='System.Management.ManagementOperationObserver'/>.</para>
        /// </remarks>
        public void InvokeMethod(
            ManagementOperationObserver watcher, 
            string methodName, 
            ManagementBaseObject inParameters, 
            InvokeMethodOptions options)
        {
            if ((null == path) || (path.Path.Length==0))
                throw new InvalidOperationException();
            else if (null == watcher)
                throw new ArgumentNullException("watcher");
            else if (null == methodName)
                throw new ArgumentNullException("methodName");
            else
            {
                Initialize ( false ) ;
                InvokeMethodOptions o = (null != options) ? 
                    (InvokeMethodOptions) options.Clone() : new InvokeMethodOptions();

                // If someone has registered for progress, make sure we flag it
                if (watcher.HaveListenersForProgress)
                    o.SendStatus = true;
    
                WmiEventSink sink = watcher.GetNewSink(scope, o.Context);

                SecurityHandler securityHandler = null;
                int status                        = (int)ManagementStatus.NoError;

                securityHandler = scope.GetSecurityHandler();

                IWbemClassObjectFreeThreaded inParams = null;

                if (null != inParameters)
                    inParams = inParameters.wbemObject;

                status = scope.GetSecuredIWbemServicesHandler( scope.GetIWbemServices() ).ExecMethodAsync_(
                    path.RelativePath, 
                    methodName,
                    o.Flags, 
                    o.GetContext(),
                    inParams,
                    sink.Stub );

                if (securityHandler != null)
                    securityHandler.Reset();

                if (status < 0)
                {
                    watcher.RemoveSink(sink);
                    if ((status & 0xfffff000) == 0x80041000)
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    else
                        Marshal.ThrowExceptionForHR(status);
                }
            }
        }