Exemplo n.º 1
0
 public ManagementEventWatcher(ManagementScope scope, EventQuery query, EventWatcherOptions options)
 {
     if (scope != null)
     {
         this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
     }
     else
     {
         this.scope = ManagementScope._Clone(null, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
     }
     if (query != null)
     {
         this.query = (EventQuery)query.Clone();
     }
     else
     {
         this.query = new EventQuery();
     }
     this.query.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
     if (options != null)
     {
         this.options = (EventWatcherOptions)options.Clone();
     }
     else
     {
         this.options = new EventWatcherOptions();
     }
     this.options.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
     this.enumWbem        = null;
     this.cachedCount     = 0;
     this.cacheIndex      = 0;
     this.sink            = null;
     this.delegateInvoker = new WmiDelegateInvoker(this);
 }
 public ManagementEventWatcher(ManagementScope scope, EventQuery query, EventWatcherOptions options)
 {
     if (scope != null)
     {
         this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
     }
     else
     {
         this.scope = ManagementScope._Clone(null, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
     }
     if (query != null)
     {
         this.query = (EventQuery) query.Clone();
     }
     else
     {
         this.query = new EventQuery();
     }
     this.query.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
     if (options != null)
     {
         this.options = (EventWatcherOptions) options.Clone();
     }
     else
     {
         this.options = new EventWatcherOptions();
     }
     this.options.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
     this.enumWbem = null;
     this.cachedCount = 0;
     this.cacheIndex = 0;
     this.sink = null;
     this.delegateInvoker = new WmiDelegateInvoker(this);
 }
 internal void FireStopped(StoppedEventArgs args)
 {
     try {
         delegateInvoker.FireEventToDelegates(Stopped, args);
         // We are done with the sink
         sink = null;
     } catch {}
 }
Exemplo n.º 4
0
        private void HackToCreateStubInMTA(object param)
        {
            SinkForEventQuery getDemultiplexedStubF = (SinkForEventQuery)param;
            object            obj = null;

            getDemultiplexedStubF.Status = WmiNetUtilsHelper.GetDemultiplexedStub_f(getDemultiplexedStubF, getDemultiplexedStubF.isLocal, out obj);
            getDemultiplexedStubF.stub   = (IWbemObjectSink)obj;
        }
Exemplo n.º 5
0
        private void HackToCreateStubInMTA(object param)
        {
            SinkForEventQuery pIUnknown  = (SinkForEventQuery)param;
            object            ppIUnknown = null;

            pIUnknown.Status = WmiNetUtilsHelper.GetDemultiplexedStub_f(pIUnknown, pIUnknown.isLocal, out ppIUnknown);
            pIUnknown.stub   = (IWbemObjectSink)ppIUnknown;
        }
Exemplo n.º 6
0
        void HackToCreateStubInMTA(object param)
        {
            SinkForEventQuery obj      = (SinkForEventQuery)param;
            object            dmuxStub = null;

            obj.Status = WmiNetUtilsHelper.GetDemultiplexedStub_f(obj, obj.isLocal, out dmuxStub);
            obj.stub   = (IWbemObjectSink)dmuxStub;
        }
        void HackToCreateStubInMTA(object param)
        {
            SinkForEventQuery     obj      = (SinkForEventQuery)param;
            IWmiSinkDemultiplexor sinkDmux = (IWmiSinkDemultiplexor) new WmiSinkDemultiplexor();
            object dmuxStub = null;

            sinkDmux.GetDemultiplexedStub(obj, out dmuxStub);
            obj.stub = (IWbemObjectSink)dmuxStub;
        }
Exemplo n.º 8
0
        //********************************************
        //Start
        //********************************************
        /// <summary>
        ///    <para>Subscribes to events with the given query and delivers
        ///       them, asynchronously, through the <see cref='System.Management.ManagementEventWatcher.EventArrived'/> event.</para>
        /// </summary>
        public void Start()
        {
            Initialize();

            // Cancel any current event query
            Stop();

            // Submit a new query
            SecurityHandler securityHandler = Scope.GetSecurityHandler();
            IWbemServices   wbemServices    = scope.GetIWbemServices();

            try
            {
                sink = new SinkForEventQuery(this, options.Context, wbemServices);
                if (sink.Status < 0)
                {
                    Marshal.ThrowExceptionForHR(sink.Status, WmiNetUtilsHelper.GetErrorInfo_f());
                }

                // For async event queries we should ensure 0 flags as this is
                // the only legal value
                int status = scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_(
                    query.QueryLanguage,
                    query.QueryString,
                    0,
                    options.GetContext(),
                    sink.Stub);


                if (status < 0)
                {
                    if (sink != null)
                    {
                        sink.ReleaseStub();
                        sink = null;
                    }

                    if ((status & 0xfffff000) == 0x80041000)
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
                    }
                }
            }
            finally
            {
                securityHandler.Reset();
            }
        }
Exemplo n.º 9
0
 public void Stop()
 {
     if (this.enumWbem != null)
     {
         Marshal.ReleaseComObject(this.enumWbem);
         this.enumWbem = null;
         this.FireStopped(new StoppedEventArgs(this.options.Context, 0x40006));
     }
     if (this.sink != null)
     {
         this.sink.Cancel();
         this.sink = null;
     }
 }
        //********************************************
        //Start
        //********************************************
        /// <summary>
        ///    <para>Subscribes for events with the given query and delivers them asynchronously, through the EventArrived event.</para>
        /// </summary>
        public void Start()
        {
            Initialize();

            // Cancel any current event query
            Stop();

            SecurityHandler securityHandler = Scope.GetSecurityHandler();

            int status = (int)ManagementStatus.NoError;

            // Submit a new query
            try
            {
                IWbemServices wbemServices = scope.GetIWbemServices();
                sink = new SinkForEventQuery(this, options.GetContext(), wbemServices);

                // For async event queries we should ensure 0 flags as this is
                // the only legal value
                status = wbemServices.ExecNotificationQueryAsync_(
                    query.QueryLanguage,
                    query.QueryString,
                    0,
                    options.GetContext(),
                    sink.Stub);
            } catch (Exception e) {
                // BUGBUG : securityHandler.Reset()?
                if (sink != null)                               // BUGBUG : This looks dangerous
                {
                    sink.ReleaseStub();
                    sink = null;
                }
                ManagementException.ThrowWithExtendedInfo(e);
            } finally {
                securityHandler.Reset();
            }

            if ((status & 0xfffff000) == 0x80041000)
            {
                ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
            }
            else if ((status & 0x80000000) != 0)
            {
                Marshal.ThrowExceptionForHR(status);
            }
        }
Exemplo n.º 11
0
        //********************************************
        //Stop
        //********************************************
        /// <summary>
        ///    <para>Cancels the subscription whether it is synchronous or asynchronous.</para>
        /// </summary>
        public void Stop()
        {
            //For semi-synchronous, release the WMI enumerator to cancel the subscription
            if (null != enumWbem)
            {
                Marshal.ReleaseComObject(enumWbem);
                enumWbem = null;
                FireStopped(new StoppedEventArgs(options.Context, (int)ManagementStatus.OperationCanceled));
            }

            // In async mode cancel the call to the sink - this will
            // unwind the operation and cause a Stopped message
            if (null != sink)
            {
                sink.Cancel();
                sink = null;
            }
        }
        /// <summary>
        ///    <para>Creates a new watcher that will listen for events conforming to the given WMI
        ///       event query, in the given WMI scope, and according to the specified options. For
        ///       this variant the query and the scope are specified objects. The options object
        ///       specifies options such as a timeout and possibly context information.</para>
        /// </summary>
        public ManagementEventWatcher(
            ManagementScope scope,
            EventQuery query,
            EventWatcherOptions options)
        {
            if (null != scope)
            {
                Scope = scope;
            }
            else
            {
                Scope = new ManagementScope();
            }

            if (null != query)
            {
                Query = query;
            }
            else
            {
                Query = new EventQuery();
            }

            if (null != options)
            {
                Options = options;
            }
            else
            {
                Options = new EventWatcherOptions();
            }

            enumWbem        = null;
            cachedCount     = 0;
            cacheIndex      = 0;
            sink            = null;
            delegateInvoker = new WmiDelegateInvoker(this);
        }
Exemplo n.º 13
0
        public void Start()
        {
            this.Initialize();
            this.Stop();
            SecurityHandler securityHandler = this.Scope.GetSecurityHandler();
            IWbemServices   iWbemServices   = this.scope.GetIWbemServices();

            try
            {
                this.sink = new SinkForEventQuery(this, this.options.Context, iWbemServices);
                if (this.sink.Status < 0)
                {
                    Marshal.ThrowExceptionForHR(this.sink.Status);
                }
                int errorCode = this.scope.GetSecuredIWbemServicesHandler(iWbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub);
                if (errorCode < 0)
                {
                    if (this.sink != null)
                    {
                        this.sink.ReleaseStub();
                        this.sink = null;
                    }
                    if ((errorCode & 0xfffff000L) == 0x80041000L)
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode);
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR(errorCode);
                    }
                }
            }
            finally
            {
                securityHandler.Reset();
            }
        }
Exemplo n.º 14
0
        public void Start()
        {
            this.Initialize();
            this.Stop();
            SecurityHandler securityHandler = this.Scope.GetSecurityHandler();
            IWbemServices   wbemServices    = this.scope.GetIWbemServices();

            try
            {
                this.sink = new SinkForEventQuery(this, this.options.Context, wbemServices);
                if (this.sink.Status < 0)
                {
                    Marshal.ThrowExceptionForHR(this.sink.Status);
                }
                int num = this.scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub);
                if (num < 0)
                {
                    if (this.sink != null)
                    {
                        this.sink.ReleaseStub();
                        this.sink = null;
                    }
                    if (((long)num & (long)-4096) != (long)-2147217408)
                    {
                        Marshal.ThrowExceptionForHR(num);
                    }
                    else
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)num);
                    }
                }
            }
            finally
            {
                securityHandler.Reset();
            }
        }
        /// <summary>
        /// <para> Initializes a new instance of the <see cref='System.Management.ManagementEventWatcher'/> class 
        ///    that listens for events conforming to the given WMI event query, according to the specified
        ///    options. For this variant, the query and the scope are specified objects. The
        ///    options object can specify options such as timeout and context information.</para>
        /// </summary>
        /// <param name='scope'>A <see cref='System.Management.ManagementScope'/> object representing the scope (namespace) in which the watcher will listen for events.</param>
        /// <param name=' query'>An <see cref='System.Management.EventQuery'/> object representing a WMI event query, which determines the events for which the watcher will listen.</param>
        /// <param name='options'>An <see cref='System.Management.EventWatcherOptions'/> object representing additional options used to watch for events. </param>
        public ManagementEventWatcher(
            ManagementScope scope, 
            EventQuery query, 
            EventWatcherOptions options)
        {
            if (null != scope)
                this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(HandleIdentifierChange));
            else
                this.scope = ManagementScope._Clone(null, new IdentifierChangedEventHandler(HandleIdentifierChange));

            if (null != query)
                this.query = (EventQuery)query.Clone();
            else
                this.query = new EventQuery();
            this.query.IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange);

            if (null != options)
                this.options = (EventWatcherOptions)options.Clone();
            else
                this.options = new EventWatcherOptions();
            this.options.IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange);

            enumWbem = null;
            cachedCount = 0; 
            cacheIndex = 0;
            sink = null;
            delegateInvoker = new WmiDelegateInvoker (this);
        }
 public void Stop()
 {
     if (this.enumWbem != null)
     {
         Marshal.ReleaseComObject(this.enumWbem);
         this.enumWbem = null;
         this.FireStopped(new StoppedEventArgs(this.options.Context, 0x40006));
     }
     if (this.sink != null)
     {
         this.sink.Cancel();
         this.sink = null;
     }
 }
        //********************************************
        //Stop
        //********************************************
        /// <summary>
        ///    <para>Cancels the subscription whether it is synchronous or asynchronous.</para>
        /// </summary>
        public void Stop()
        {
            //For semi-synchronous, release the WMI enumerator to cancel the subscription
            if (null != enumWbem)
            {
                Marshal.ReleaseComObject(enumWbem);
                enumWbem = null;
                FireStopped (new StoppedEventArgs (options.Context, (int)ManagementStatus.OperationCanceled));
            }

            // In async mode cancel the call to the sink - this will
            // unwind the operation and cause a Stopped message
            if (null != sink)
            {
                sink.Cancel ();
                sink = null;
            }
        }
 public void Start()
 {
     this.Initialize();
     this.Stop();
     SecurityHandler securityHandler = this.Scope.GetSecurityHandler();
     IWbemServices iWbemServices = this.scope.GetIWbemServices();
     try
     {
         this.sink = new SinkForEventQuery(this, this.options.Context, iWbemServices);
         if (this.sink.Status < 0)
         {
             Marshal.ThrowExceptionForHR(this.sink.Status);
         }
         int errorCode = this.scope.GetSecuredIWbemServicesHandler(iWbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub);
         if (errorCode < 0)
         {
             if (this.sink != null)
             {
                 this.sink.ReleaseStub();
                 this.sink = null;
             }
             if ((errorCode & 0xfffff000L) == 0x80041000L)
             {
                 ManagementException.ThrowWithExtendedInfo((ManagementStatus) errorCode);
             }
             else
             {
                 Marshal.ThrowExceptionForHR(errorCode);
             }
         }
     }
     finally
     {
         securityHandler.Reset();
     }
 }
        //********************************************
        //Start
        //********************************************
        /// <summary>
        ///    <para>Subscribes to events with the given query and delivers 
        ///       them, asynchronously, through the <see cref='System.Management.ManagementEventWatcher.EventArrived'/> event.</para>
        /// </summary>
        public void Start()
        {
            Initialize ();

            // Cancel any current event query
            Stop ();
            
            // Submit a new query
            SecurityHandler securityHandler = Scope.GetSecurityHandler();
            IWbemServices wbemServices = scope.GetIWbemServices();

            try
            {
                sink = new SinkForEventQuery(this, options.Context, wbemServices);
                if (sink.Status < 0) 
                {
                    Marshal.ThrowExceptionForHR(sink.Status);
                }

                // For async event queries we should ensure 0 flags as this is
                // the only legal value
                int status = scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_(
                    query.QueryLanguage,
                    query.QueryString,
                    0,
                    options.GetContext(),
                    sink.Stub);


                if (status < 0)
                {
                    if (sink != null)
                    {
                        sink.ReleaseStub();
                        sink = null;
                    }

                    if ((status & 0xfffff000) == 0x80041000)

                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    else
                        Marshal.ThrowExceptionForHR(status);
                }
            }
            finally
            {
                securityHandler.Reset();
            }
        }
Exemplo n.º 20
0
		public void Start()
		{
			this.Initialize();
			this.Stop();
			SecurityHandler securityHandler = this.Scope.GetSecurityHandler();
			IWbemServices wbemServices = this.scope.GetIWbemServices();
			try
			{
				this.sink = new SinkForEventQuery(this, this.options.Context, wbemServices);
				if (this.sink.Status < 0)
				{
					Marshal.ThrowExceptionForHR(this.sink.Status);
				}
				int num = this.scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub);
				if (num < 0)
				{
					if (this.sink != null)
					{
						this.sink.ReleaseStub();
						this.sink = null;
					}
					if (((long)num & (long)-4096) != (long)-2147217408)
					{
						Marshal.ThrowExceptionForHR(num);
					}
					else
					{
						ManagementException.ThrowWithExtendedInfo((ManagementStatus)num);
					}
				}
			}
			finally
			{
				securityHandler.Reset();
			}
		}