Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Wmi / managed / System / Management / ManagementObjectSearcher.cs / 1305376 / ManagementObjectSearcher.cs
using System; using System.Collections; using System.Runtime.InteropServices; using WbemClient_v1; using System.Diagnostics; using System.ComponentModel; namespace System.Management { //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC// ////// ///Retrieves a collection of management objects based /// on a specified query. ///This class is one of the more commonly used entry points to retrieving /// management information. For example, it can be used to enumerate all disk /// drives, network adapters, processes and many more management objects on a /// system, or to query for all network connections that are up, services that are /// paused etc. ///When instantiated, an instance of this class takes as input a WMI /// query represented in an ///or it's derivatives, and optionally a representing the WMI namespace /// to execute the query in. It can also take additional advanced /// options in an object. When the Get() method on this object /// is invoked, the ManagementObjectSearcher executes the given query in the /// specified scope and returns a collection of management objects that match the /// query in a . /// //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC// [ToolboxItem(false)] public class ManagementObjectSearcher : Component { //fields private ManagementScope scope; private ObjectQuery query; private EnumerationOptions options; //default constructor ///using System; /// using System.Management; /// /// // This sample demonstrates perform a query using /// // ManagementObjectSearcher object. /// class Sample_ManagementObjectSearcher /// { /// public static int Main(string[] args) { /// ManagementObjectSearcher searcher = new /// ManagementObjectSearcher("select * from win32_share"); /// foreach (ManagementObject share in searcher.Get()) { /// Console.WriteLine("Share = " + share["Name"]); /// } /// return 0; /// } /// } ///
///Imports System /// Imports System.Management /// /// ' This sample demonstrates perform a query using /// ' ManagementObjectSearcher object. /// Class Sample_ManagementObjectSearcher /// Overloads Public Shared Function Main(args() As String) As Integer /// Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Share") /// Dim share As ManagementObject /// For Each share In searcher.Get() /// Console.WriteLine("Share = " & share("Name").ToString()) /// Next share /// Return 0 /// End Function /// End Class ///
////// Initializes a new instance of the ///class. /// /// ///Initializes a new instance of the ///class. After some properties on /// this object are set, the object can be used to invoke a query for management information. This is the default /// constructor. /// public ManagementObjectSearcher() : this((ManagementScope)null, null, null) {} //parameterized constructors ///ManagementObjectSearcher s = new ManagementObjectSearcher(); ///
///Dim s As New ManagementObjectSearcher() ///
////// /// The WMI query to be invoked by the object. ///Initializes a new instance of the ///class used /// to invoke the specified query for management information. /// public ManagementObjectSearcher(string queryString) : this(null, new ObjectQuery(queryString), null) {} ///ManagementObjectSearcher s = /// new ManagementObjectSearcher("SELECT * FROM Win32_Service"); ///
///Dim s As New ManagementObjectSearcher("SELECT * FROM Win32_Service") ///
////// /// AnInitializes a new instance of the ///class used to invoke the /// specified query for management information. representing the query to be invoked by the searcher. /// /// public ManagementObjectSearcher(ObjectQuery query) : this (null, query, null) {} ///SelectQuery q = new SelectQuery("Win32_Service", "State='Running'"); /// ManagementObjectSearcher s = new ManagementObjectSearcher(q); ///
///Dim q As New SelectQuery("Win32_Service", "State=""Running""") /// Dim s As New ManagementObjectSearcher(q) ///
////// /// The scope in which to query. /// The query to be invoked. ///Initializes a new instance of the ///class used to invoke the /// specified query in the specified scope. /// ///If no scope is specified, the default scope ( ///) is used. /// public ManagementObjectSearcher(string scope, string queryString) : this(new ManagementScope(scope), new ObjectQuery(queryString), null) {} ///ManagementObjectSearcher s = new ManagementObjectSearcher( /// "root\\MyApp", /// "SELECT * FROM MyClass WHERE MyProp=5"); ///
///Dim s As New ManagementObjectSearcher( _ /// "root\MyApp", _ /// "SELECT * FROM MyClass WHERE MyProp=5") ///
////// /// AInitializes a new instance of the ///class used to invoke the /// specified query in the specified scope. representing the scope in which to invoke the query. /// An representing the query to be invoked. /// /// ///If no scope is specified, the default scope ( ///) is /// used. /// public ManagementObjectSearcher(ManagementScope scope, ObjectQuery query) : this(scope, query, null) {} ///ManagementScope myScope = new ManagementScope("root\\MyApp"); /// SelectQuery q = new SelectQuery("Win32_Environment", "User=<system>"); /// ManagementObjectSearcher s = new ManagementObjectSearcher(myScope,q); ///
///Dim myScope As New ManagementScope("root\MyApp") /// Dim q As New SelectQuery("Win32_Environment", "User=<system>") /// Dim s As New ManagementObjectSearcher(myScope,q) ///
////// /// The scope in which the query should be invoked. /// The query to be invoked. /// AnInitializes a new instance of the ///class used to invoke the specified /// query, in the specified scope, and with the specified options. specifying additional options for the query. /// /// public ManagementObjectSearcher(string scope, string queryString, EnumerationOptions options) : this(new ManagementScope(scope), new ObjectQuery(queryString), options) {} ///ManagementObjectSearcher s = new ManagementObjectSearcher( /// "root\\MyApp", /// "SELECT * FROM MyClass", /// new EnumerationOptions(null, InfiniteTimeout, 1, true, false, true); ///
///Dim s As New ManagementObjectSearcher( _ /// "root\MyApp", _ /// "SELECT * FROM MyClass", _ /// New EnumerationOptions(Null, InfiniteTimeout, 1, True, False, True) ///
////// /// AInitializes a new instance of the ///class to be /// used to invoke the specified query in the specified scope, with the specified /// options. specifying the scope of the query /// An specifying the query to be invoked /// An specifying additional options to be used for the query. /// /// public ManagementObjectSearcher(ManagementScope scope, ObjectQuery query, EnumerationOptions options) { this.scope = ManagementScope._Clone(scope); if (null != query) this.query = (ObjectQuery)query.Clone(); else this.query = new ObjectQuery(); if (null != options) this.options = (EnumerationOptions)options.Clone(); else this.options = new EnumerationOptions(); } // //Public Properties // ///ManagementScope scope = new ManagementScope("root\\MyApp"); /// SelectQuery q = new SelectQuery("SELECT * FROM MyClass"); /// EnumerationOptions o = new EnumerationOptions(null, InfiniteTimeout, 1, true, false, true); /// ManagementObjectSearcher s = new ManagementObjectSearcher(scope, q, o); ///
///Dim scope As New ManagementScope("root\MyApp") /// Dim q As New SelectQuery("SELECT * FROM MyClass") /// Dim o As New EnumerationOptions(Null, InfiniteTimeout, 1, True, False, True) /// Dim s As New ManagementObjectSearcher(scope, q, o) ///
////// ///Gets or sets the scope in which to look for objects (the scope represents a WMI namespace). ////// ///The scope (namespace) in which to look for objects. ////// ///When the value of this property is changed, /// the ////// is re-bound to the new scope. /// public ManagementScope Scope { get { return scope; } set { if (null != value) scope = (ManagementScope) value.Clone (); else throw new ArgumentNullException ("value"); } } ///ManagementObjectSearcher s = new ManagementObjectSearcher(); /// s.Scope = new ManagementScope("root\\MyApp"); ///
///Dim s As New ManagementObjectSearcher() /// Dim ms As New ManagementScope ("root\MyApp") /// s.Scope = ms ///
////// ///Gets or sets the query to be invoked in the /// searcher (that is, the criteria to be applied to the search for management objects). ////// ///The criteria to apply to the query. ////// public ObjectQuery Query { get { return query; } set { if (null != value) query = (ObjectQuery)value.Clone (); else throw new ArgumentNullException ("value"); } } ///When the value of this property is changed, the ////// is reset to use the new query. /// ///Gets or sets the options for how to search for objects. ////// public EnumerationOptions Options { get { return options; } set { if (null != value) options = (EnumerationOptions) value.Clone (); else throw new ArgumentNullException("value"); } } //******************************************** //Get() //******************************************** ///The options for how to search for objects. ////// Invokes the specified WMI query and returns the resulting collection. /// ////// ///Invokes the specified WMI query and returns the /// resulting collection. ////// public ManagementObjectCollection Get() { Initialize (); IEnumWbemClassObject ew = null; SecurityHandler securityHandler = scope.GetSecurityHandler(); EnumerationOptions enumOptions = (EnumerationOptions)options.Clone(); int status = (int)ManagementStatus.NoError; try { //If this is a simple SelectQuery (className only), and the enumerateDeep is set, we have //to find out whether this is a class enumeration or instance enumeration and call CreateInstanceEnum/ //CreateClassEnum appropriately, because with ExecQuery we can't do a deep enumeration. if ((query.GetType() == typeof(SelectQuery)) && (((SelectQuery)query).Condition == null) && (((SelectQuery)query).SelectedProperties == null) && (options.EnumerateDeep == true)) { //Need to make sure that we're not passing invalid flags to enumeration APIs. //The only flags not valid for enumerations are EnsureLocatable & PrototypeOnly. enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false; if (((SelectQuery)query).IsSchemaQuery == false) //deep instance enumeration { status = scope.GetSecuredIWbemServicesHandler( scope.GetIWbemServices() ).CreateInstanceEnum_( ((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), ref ew); } else //deep class enumeration { status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).CreateClassEnum_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), ref ew ); } } else //we can use ExecQuery { //Make sure the EnumerateDeep flag bit is turned off because it's invalid for queries enumOptions.EnumerateDeep = true; status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).ExecQuery_( query.QueryLanguage, query.QueryString, enumOptions.Flags, enumOptions.GetContext(), ref ew ); } } catch (COMException e) { // ManagementException.ThrowWithExtendedInfo(e); } finally { securityHandler.Reset(); } if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { Marshal.ThrowExceptionForHR(status); } //Create a new collection object for the results return new ManagementObjectCollection(scope, options, ew); }//Get() //******************************************** //Get() asynchronous //******************************************** ///A ///containing the objects that match the /// specified query. /// /// The watcher that raises events triggered by the operation. public void Get(ManagementOperationObserver watcher) { if (null == watcher) throw new ArgumentNullException ("watcher"); Initialize (); IWbemServices wbemServices = scope.GetIWbemServices (); EnumerationOptions enumOptions = (EnumerationOptions)options.Clone(); // Ensure we switch off ReturnImmediately as this is invalid for async calls enumOptions.ReturnImmediately = false; // If someone has registered for progress, make sure we flag it if (watcher.HaveListenersForProgress) enumOptions.SendStatus = true; WmiEventSink sink = watcher.GetNewSink (scope, enumOptions.Context); SecurityHandler securityHandler = scope.GetSecurityHandler(); int status = (int)ManagementStatus.NoError; try { //If this is a simple SelectQuery (className only), and the enumerateDeep is set, we have //to find out whether this is a class enumeration or instance enumeration and call CreateInstanceEnum/ //CreateClassEnum appropriately, because with ExecQuery we can't do a deep enumeration. if ((query.GetType() == typeof(SelectQuery)) && (((SelectQuery)query).Condition == null) && (((SelectQuery)query).SelectedProperties == null) && (options.EnumerateDeep == true)) { //Need to make sure that we're not passing invalid flags to enumeration APIs. //The only flags not valid for enumerations are EnsureLocatable & PrototypeOnly. enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false; if (((SelectQuery)query).IsSchemaQuery == false) //deep instance enumeration { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).CreateInstanceEnumAsync_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } else { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).CreateClassEnumAsync_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } } else //we can use ExecQuery { //Make sure the EnumerateDeep flag bit is turned off because it's invalid for queries enumOptions.EnumerateDeep = true; status = scope.GetSecuredIWbemServicesHandler( wbemServices ).ExecQueryAsync_( query.QueryLanguage, query.QueryString, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } } catch (COMException e) { // watcher.RemoveSink (sink); ManagementException.ThrowWithExtendedInfo (e); } finally { securityHandler.Reset(); } if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { Marshal.ThrowExceptionForHR(status); } } private void Initialize() { //If the query is not set yet we can't do it if (null == query) throw new InvalidOperationException(); //If we're not connected yet, this is the time to do it... lock (this) { if (null == scope) scope = ManagementScope._Clone(null); } lock (scope) { if (!scope.IsConnected) scope.Initialize(); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections; using System.Runtime.InteropServices; using WbemClient_v1; using System.Diagnostics; using System.ComponentModel; namespace System.Management { //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC// ///Invokes the WMI query, asynchronously, and binds to a watcher to deliver the results. ////// ///Retrieves a collection of management objects based /// on a specified query. ///This class is one of the more commonly used entry points to retrieving /// management information. For example, it can be used to enumerate all disk /// drives, network adapters, processes and many more management objects on a /// system, or to query for all network connections that are up, services that are /// paused etc. ///When instantiated, an instance of this class takes as input a WMI /// query represented in an ///or it's derivatives, and optionally a representing the WMI namespace /// to execute the query in. It can also take additional advanced /// options in an object. When the Get() method on this object /// is invoked, the ManagementObjectSearcher executes the given query in the /// specified scope and returns a collection of management objects that match the /// query in a . /// //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC// [ToolboxItem(false)] public class ManagementObjectSearcher : Component { //fields private ManagementScope scope; private ObjectQuery query; private EnumerationOptions options; //default constructor ///using System; /// using System.Management; /// /// // This sample demonstrates perform a query using /// // ManagementObjectSearcher object. /// class Sample_ManagementObjectSearcher /// { /// public static int Main(string[] args) { /// ManagementObjectSearcher searcher = new /// ManagementObjectSearcher("select * from win32_share"); /// foreach (ManagementObject share in searcher.Get()) { /// Console.WriteLine("Share = " + share["Name"]); /// } /// return 0; /// } /// } ///
///Imports System /// Imports System.Management /// /// ' This sample demonstrates perform a query using /// ' ManagementObjectSearcher object. /// Class Sample_ManagementObjectSearcher /// Overloads Public Shared Function Main(args() As String) As Integer /// Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Share") /// Dim share As ManagementObject /// For Each share In searcher.Get() /// Console.WriteLine("Share = " & share("Name").ToString()) /// Next share /// Return 0 /// End Function /// End Class ///
////// Initializes a new instance of the ///class. /// /// ///Initializes a new instance of the ///class. After some properties on /// this object are set, the object can be used to invoke a query for management information. This is the default /// constructor. /// public ManagementObjectSearcher() : this((ManagementScope)null, null, null) {} //parameterized constructors ///ManagementObjectSearcher s = new ManagementObjectSearcher(); ///
///Dim s As New ManagementObjectSearcher() ///
////// /// The WMI query to be invoked by the object. ///Initializes a new instance of the ///class used /// to invoke the specified query for management information. /// public ManagementObjectSearcher(string queryString) : this(null, new ObjectQuery(queryString), null) {} ///ManagementObjectSearcher s = /// new ManagementObjectSearcher("SELECT * FROM Win32_Service"); ///
///Dim s As New ManagementObjectSearcher("SELECT * FROM Win32_Service") ///
////// /// AnInitializes a new instance of the ///class used to invoke the /// specified query for management information. representing the query to be invoked by the searcher. /// /// public ManagementObjectSearcher(ObjectQuery query) : this (null, query, null) {} ///SelectQuery q = new SelectQuery("Win32_Service", "State='Running'"); /// ManagementObjectSearcher s = new ManagementObjectSearcher(q); ///
///Dim q As New SelectQuery("Win32_Service", "State=""Running""") /// Dim s As New ManagementObjectSearcher(q) ///
////// /// The scope in which to query. /// The query to be invoked. ///Initializes a new instance of the ///class used to invoke the /// specified query in the specified scope. /// ///If no scope is specified, the default scope ( ///) is used. /// public ManagementObjectSearcher(string scope, string queryString) : this(new ManagementScope(scope), new ObjectQuery(queryString), null) {} ///ManagementObjectSearcher s = new ManagementObjectSearcher( /// "root\\MyApp", /// "SELECT * FROM MyClass WHERE MyProp=5"); ///
///Dim s As New ManagementObjectSearcher( _ /// "root\MyApp", _ /// "SELECT * FROM MyClass WHERE MyProp=5") ///
////// /// AInitializes a new instance of the ///class used to invoke the /// specified query in the specified scope. representing the scope in which to invoke the query. /// An representing the query to be invoked. /// /// ///If no scope is specified, the default scope ( ///) is /// used. /// public ManagementObjectSearcher(ManagementScope scope, ObjectQuery query) : this(scope, query, null) {} ///ManagementScope myScope = new ManagementScope("root\\MyApp"); /// SelectQuery q = new SelectQuery("Win32_Environment", "User=<system>"); /// ManagementObjectSearcher s = new ManagementObjectSearcher(myScope,q); ///
///Dim myScope As New ManagementScope("root\MyApp") /// Dim q As New SelectQuery("Win32_Environment", "User=<system>") /// Dim s As New ManagementObjectSearcher(myScope,q) ///
////// /// The scope in which the query should be invoked. /// The query to be invoked. /// AnInitializes a new instance of the ///class used to invoke the specified /// query, in the specified scope, and with the specified options. specifying additional options for the query. /// /// public ManagementObjectSearcher(string scope, string queryString, EnumerationOptions options) : this(new ManagementScope(scope), new ObjectQuery(queryString), options) {} ///ManagementObjectSearcher s = new ManagementObjectSearcher( /// "root\\MyApp", /// "SELECT * FROM MyClass", /// new EnumerationOptions(null, InfiniteTimeout, 1, true, false, true); ///
///Dim s As New ManagementObjectSearcher( _ /// "root\MyApp", _ /// "SELECT * FROM MyClass", _ /// New EnumerationOptions(Null, InfiniteTimeout, 1, True, False, True) ///
////// /// AInitializes a new instance of the ///class to be /// used to invoke the specified query in the specified scope, with the specified /// options. specifying the scope of the query /// An specifying the query to be invoked /// An specifying additional options to be used for the query. /// /// public ManagementObjectSearcher(ManagementScope scope, ObjectQuery query, EnumerationOptions options) { this.scope = ManagementScope._Clone(scope); if (null != query) this.query = (ObjectQuery)query.Clone(); else this.query = new ObjectQuery(); if (null != options) this.options = (EnumerationOptions)options.Clone(); else this.options = new EnumerationOptions(); } // //Public Properties // ///ManagementScope scope = new ManagementScope("root\\MyApp"); /// SelectQuery q = new SelectQuery("SELECT * FROM MyClass"); /// EnumerationOptions o = new EnumerationOptions(null, InfiniteTimeout, 1, true, false, true); /// ManagementObjectSearcher s = new ManagementObjectSearcher(scope, q, o); ///
///Dim scope As New ManagementScope("root\MyApp") /// Dim q As New SelectQuery("SELECT * FROM MyClass") /// Dim o As New EnumerationOptions(Null, InfiniteTimeout, 1, True, False, True) /// Dim s As New ManagementObjectSearcher(scope, q, o) ///
////// ///Gets or sets the scope in which to look for objects (the scope represents a WMI namespace). ////// ///The scope (namespace) in which to look for objects. ////// ///When the value of this property is changed, /// the ////// is re-bound to the new scope. /// public ManagementScope Scope { get { return scope; } set { if (null != value) scope = (ManagementScope) value.Clone (); else throw new ArgumentNullException ("value"); } } ///ManagementObjectSearcher s = new ManagementObjectSearcher(); /// s.Scope = new ManagementScope("root\\MyApp"); ///
///Dim s As New ManagementObjectSearcher() /// Dim ms As New ManagementScope ("root\MyApp") /// s.Scope = ms ///
////// ///Gets or sets the query to be invoked in the /// searcher (that is, the criteria to be applied to the search for management objects). ////// ///The criteria to apply to the query. ////// public ObjectQuery Query { get { return query; } set { if (null != value) query = (ObjectQuery)value.Clone (); else throw new ArgumentNullException ("value"); } } ///When the value of this property is changed, the ////// is reset to use the new query. /// ///Gets or sets the options for how to search for objects. ////// public EnumerationOptions Options { get { return options; } set { if (null != value) options = (EnumerationOptions) value.Clone (); else throw new ArgumentNullException("value"); } } //******************************************** //Get() //******************************************** ///The options for how to search for objects. ////// Invokes the specified WMI query and returns the resulting collection. /// ////// ///Invokes the specified WMI query and returns the /// resulting collection. ////// public ManagementObjectCollection Get() { Initialize (); IEnumWbemClassObject ew = null; SecurityHandler securityHandler = scope.GetSecurityHandler(); EnumerationOptions enumOptions = (EnumerationOptions)options.Clone(); int status = (int)ManagementStatus.NoError; try { //If this is a simple SelectQuery (className only), and the enumerateDeep is set, we have //to find out whether this is a class enumeration or instance enumeration and call CreateInstanceEnum/ //CreateClassEnum appropriately, because with ExecQuery we can't do a deep enumeration. if ((query.GetType() == typeof(SelectQuery)) && (((SelectQuery)query).Condition == null) && (((SelectQuery)query).SelectedProperties == null) && (options.EnumerateDeep == true)) { //Need to make sure that we're not passing invalid flags to enumeration APIs. //The only flags not valid for enumerations are EnsureLocatable & PrototypeOnly. enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false; if (((SelectQuery)query).IsSchemaQuery == false) //deep instance enumeration { status = scope.GetSecuredIWbemServicesHandler( scope.GetIWbemServices() ).CreateInstanceEnum_( ((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), ref ew); } else //deep class enumeration { status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).CreateClassEnum_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), ref ew ); } } else //we can use ExecQuery { //Make sure the EnumerateDeep flag bit is turned off because it's invalid for queries enumOptions.EnumerateDeep = true; status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices() ).ExecQuery_( query.QueryLanguage, query.QueryString, enumOptions.Flags, enumOptions.GetContext(), ref ew ); } } catch (COMException e) { // ManagementException.ThrowWithExtendedInfo(e); } finally { securityHandler.Reset(); } if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { Marshal.ThrowExceptionForHR(status); } //Create a new collection object for the results return new ManagementObjectCollection(scope, options, ew); }//Get() //******************************************** //Get() asynchronous //******************************************** ///A ///containing the objects that match the /// specified query. /// /// The watcher that raises events triggered by the operation. public void Get(ManagementOperationObserver watcher) { if (null == watcher) throw new ArgumentNullException ("watcher"); Initialize (); IWbemServices wbemServices = scope.GetIWbemServices (); EnumerationOptions enumOptions = (EnumerationOptions)options.Clone(); // Ensure we switch off ReturnImmediately as this is invalid for async calls enumOptions.ReturnImmediately = false; // If someone has registered for progress, make sure we flag it if (watcher.HaveListenersForProgress) enumOptions.SendStatus = true; WmiEventSink sink = watcher.GetNewSink (scope, enumOptions.Context); SecurityHandler securityHandler = scope.GetSecurityHandler(); int status = (int)ManagementStatus.NoError; try { //If this is a simple SelectQuery (className only), and the enumerateDeep is set, we have //to find out whether this is a class enumeration or instance enumeration and call CreateInstanceEnum/ //CreateClassEnum appropriately, because with ExecQuery we can't do a deep enumeration. if ((query.GetType() == typeof(SelectQuery)) && (((SelectQuery)query).Condition == null) && (((SelectQuery)query).SelectedProperties == null) && (options.EnumerateDeep == true)) { //Need to make sure that we're not passing invalid flags to enumeration APIs. //The only flags not valid for enumerations are EnsureLocatable & PrototypeOnly. enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false; if (((SelectQuery)query).IsSchemaQuery == false) //deep instance enumeration { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).CreateInstanceEnumAsync_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } else { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).CreateClassEnumAsync_(((SelectQuery)query).ClassName, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } } else //we can use ExecQuery { //Make sure the EnumerateDeep flag bit is turned off because it's invalid for queries enumOptions.EnumerateDeep = true; status = scope.GetSecuredIWbemServicesHandler( wbemServices ).ExecQueryAsync_( query.QueryLanguage, query.QueryString, enumOptions.Flags, enumOptions.GetContext(), sink.Stub); } } catch (COMException e) { // watcher.RemoveSink (sink); ManagementException.ThrowWithExtendedInfo (e); } finally { securityHandler.Reset(); } if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { Marshal.ThrowExceptionForHR(status); } } private void Initialize() { //If the query is not set yet we can't do it if (null == query) throw new InvalidOperationException(); //If we're not connected yet, this is the time to do it... lock (this) { if (null == scope) scope = ManagementScope._Clone(null); } lock (scope) { if (!scope.IsConnected) scope.Initialize(); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.Invokes the WMI query, asynchronously, and binds to a watcher to deliver the results. ///
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BaseCollection.cs
- MarginCollapsingState.cs
- Slider.cs
- PeerCollaboration.cs
- List.cs
- PasswordTextNavigator.cs
- DetailsViewDeleteEventArgs.cs
- Geometry3D.cs
- selecteditemcollection.cs
- ToolTip.cs
- ValidationEventArgs.cs
- ValueUtilsSmi.cs
- PageCache.cs
- BatchParser.cs
- UIntPtr.cs
- ServiceNameElement.cs
- HttpCachePolicy.cs
- UserPreferenceChangedEventArgs.cs
- PartialArray.cs
- ObjectPersistData.cs
- PkcsMisc.cs
- KnownBoxes.cs
- DiffuseMaterial.cs
- DispatcherTimer.cs
- FontCollection.cs
- ClassicBorderDecorator.cs
- Process.cs
- MembershipPasswordException.cs
- BulletDecorator.cs
- ServiceBusyException.cs
- SizeChangedEventArgs.cs
- DefaultProxySection.cs
- EdgeProfileValidation.cs
- TraceHandler.cs
- NavigationProperty.cs
- SQLDecimalStorage.cs
- Array.cs
- ActivityLocationReferenceEnvironment.cs
- XmlIlTypeHelper.cs
- DiscoveryServerProtocol.cs
- Token.cs
- DataTableMappingCollection.cs
- DbFunctionCommandTree.cs
- XamlDesignerSerializationManager.cs
- Glyph.cs
- BinaryObjectReader.cs
- RowCache.cs
- DrawingContextWalker.cs
- BaseProcessor.cs
- UnsafeNativeMethods.cs
- CustomPopupPlacement.cs
- StatusBar.cs
- Triplet.cs
- TypeReference.cs
- SequenceNumber.cs
- LocalBuilder.cs
- DataBindingList.cs
- EncodingInfo.cs
- PersonalizableTypeEntry.cs
- PopupRoot.cs
- InheritanceAttribute.cs
- CompilerLocalReference.cs
- TaiwanCalendar.cs
- AnnotationResourceCollection.cs
- RuleSetCollection.cs
- UnsafeNativeMethods.cs
- LinearGradientBrush.cs
- DocumentPropertiesDialog.cs
- URI.cs
- ModelVisual3D.cs
- WebAdminConfigurationHelper.cs
- VectorAnimationBase.cs
- InternalBufferOverflowException.cs
- DesignSurfaceEvent.cs
- Queue.cs
- PrintControllerWithStatusDialog.cs
- FileNotFoundException.cs
- ExpressionBuilderContext.cs
- ClientBuildManager.cs
- RefreshEventArgs.cs
- StrokeNodeOperations.cs
- ApplicationDirectoryMembershipCondition.cs
- LambdaCompiler.Statements.cs
- DataTrigger.cs
- ListViewInsertedEventArgs.cs
- DataGridViewSelectedColumnCollection.cs
- ColorInterpolationModeValidation.cs
- DataGridHelper.cs
- DetailsViewCommandEventArgs.cs
- FlowchartStart.xaml.cs
- Win32.cs
- DeviceFilterEditorDialog.cs
- ImportCatalogPart.cs
- SerializationObjectManager.cs
- ServiceRoute.cs
- _UncName.cs
- InstallerTypeAttribute.cs
- GridViewRowEventArgs.cs
- ContextBase.cs
- DateTimeConverter.cs