private static ObjectQuery GetChildQuery(string className, ManagementBaseObject config) { var parent = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty); var name = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); return(GetChildQuery(className, CatalogueName.GetFullName(parent, name))); }
private static EventType CreateEventType(Catalogue catalogue, ManagementBaseObject config) { // Get the properties. var name = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); var isEnabled = (bool)WmiUtil.GetPropertyValue(config, Constants.Wmi.EventType.IsEnabledProperty); return(catalogue.CreateEventType(catalogue, name, isEnabled)); }
private static Source CreateSource(ISourceParent parent, ManagementBaseObject config) { // Get the properties. var name = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); var enabledEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Source.EnabledEventsProperty); string[] enabledEventsArray = SplitString(enabledEvents, Constants.Module.ListSeparator); return(parent.Catalogue.CreateSource(parent, name, enabledEventsArray)); }
private void DeleteEventType(IElement eventType) { // Get the configuration for this source. ManagementObject config = WmiUtil.GetObject(_scope, GetPath(eventType)); if (config != null) { DeleteEventType(config); } }
private void DeleteSource(Source source) { // Get the configuration for this source. ManagementObject config = WmiUtil.GetObject(_scope, GetPath(source)); if (config != null) { DeleteSource(config); } }
private void DeleteNamespace(Namespace ns) { // Get the configuration for this source. ManagementObject config = WmiUtil.GetObject(_scope, GetPath(ns)); if (config != null) { DeleteNamespace(config); } }
private static string GetSourceFullyQualifiedReference(EventArrivedEventArgs e) { var config = (ManagementBaseObject)WmiUtil.GetPropertyValue(e.NewEvent, "TargetInstance"); Debug.Assert(config != null, "config != null"); var name = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); var parent = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty); return(CatalogueName.GetQualifiedReferenceUnchecked(parent, name)); }
public void EventStatusChanged(CatalogueElements elementType, string fullName) { // Store the changes in static properties on the LinkMe_InstrumentationEventStatusChange class. var config = new ManagementClass(_scope, new ManagementPath(Constants.Wmi.EventStatusChange.Class), null); WmiUtil.SetPropertyValue(config, Constants.Wmi.EventStatusChange.FullNameProperty, fullName); WmiUtil.SetPropertyValue(config, Constants.Wmi.EventStatusChange.ElementTypeProperty, (int)elementType); config.Put(); }
public static IList <ProcessInfoViewModel> LoadProcesses() { var processInfoList = WmiUtil.QueryProcesses().AsParallel() .Select(mgmtObj => BuildProcessInfo(mgmtObj)) .OrderBy(p => p.ProcessId); //var processInfoList = Process.GetProcesses().AsParallel() // .Select(p => BuildProcessInfo(p)) // .OrderBy(p => p.ProcessId); return(new List <ProcessInfoViewModel>(processInfoList)); }
private void HandleEventModification(object sender, EventArrivedEventArgs e) { string name; bool isEnabled; try { // Read the details of the change. var config = (ManagementBaseObject)WmiUtil.GetPropertyValue( e.NewEvent, "TargetInstance"); Debug.Assert(config != null, "config != null"); name = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); isEnabled = (bool)WmiUtil.GetPropertyValue(config, Constants.Wmi.EventType.IsEnabledProperty); } catch (System.Exception ex) { HandleNotificationException(ex); return; } // Work through the callbacks. _lock.AcquireReaderLock(Timeout.Infinite); try { foreach (IRepositoryCallback callback in _callbacks) { try { callback.EventChanged(name, isEnabled); } catch { } } } finally { _lock.ReleaseReaderLock(); } }
private void HandleEventStatusChange(object sender, EventArrivedEventArgs e) { string fullName; CatalogueElements elementType; try { // Read the details of the change. var config = (ManagementBaseObject)WmiUtil.GetPropertyValue( e.NewEvent, "TargetClass"); Debug.Assert(config != null, "config != null"); fullName = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.EventStatusChange.FullNameProperty); elementType = (CatalogueElements)WmiUtil.GetPropertyValue(config, Constants.Wmi.EventStatusChange.ElementTypeProperty); } catch (System.Exception ex) { HandleNotificationException(ex); return; } // Work through the callbacks. _lock.AcquireReaderLock(Timeout.Infinite); try { foreach (IRepositoryCallback callback in _callbacks) { try { callback.EventStatusChanged(elementType, fullName); } catch { } } } finally { _lock.ReleaseReaderLock(); } }
private void UpdateEventType(EventType eventType) { // Try to get an existing source. ManagementObject config = WmiUtil.GetObject(_scope, GetPath(eventType)); if (config == null) { // Not found, so create a new instance. config = CreateInstance(Constants.Wmi.EventType.Class); WmiUtil.SetPropertyValue(config, Constants.Wmi.NameProperty, eventType.Name); } // Update the rest of the properties and save. WmiUtil.SetPropertyValue(config, Constants.Wmi.EventType.IsEnabledProperty, eventType.IsEnabled); config.Put(); }
private void UpdateSource(Source source) { // Try to get an existing source. ManagementObject config = WmiUtil.GetObject(_scope, GetPath(source)); if (config == null) { // Not found, so create a new instance. config = CreateInstance(Constants.Wmi.Source.Class); WmiUtil.SetPropertyValue(config, Constants.Wmi.ParentProperty, source.Parent == null ? string.Empty : source.Parent.FullName); WmiUtil.SetPropertyValue(config, Constants.Wmi.NameProperty, source.Name); } // Update the rest of the properties and save. string enabledEvents = JoinString(source.EnabledEvents, Constants.Module.ListSeparator); WmiUtil.SetPropertyValue(config, Constants.Wmi.Source.EnabledEventsProperty, enabledEvents); config.Put(); }
private void InitialiseCallback() { _lock = new ReaderWriterLock(); _callbacks = new ArrayList(); var scope = new ManagementScope(InitialisationString); scope.Connect(); // Event enable/disable notifications - these are stored as static properties on the // LinkMe_InstrumentationEventStatusChange class. var query = new WqlEventQuery("__ClassModificationEvent", "TargetClass isa \"" + Constants.Wmi.EventStatusChange.Class + "\""); WmiUtil.StartEventWatcher(scope, query, HandleEventStatusChange); // Watch for modifications of the LinkMe_InstrumentationEvent class to pick up changes to default // event status. query = new WqlEventQuery("__InstanceModificationEvent", "TargetInstance isa \"" + Constants.Wmi.EventType.Class + "\""); WmiUtil.StartEventWatcher(scope, query, HandleEventModification); // Watch for creation of new LinkMe_InstrumentationSource objects. query = new WqlEventQuery("__InstanceCreationEvent", "TargetInstance isa \"" + Constants.Wmi.Source.Class + "\""); WmiUtil.StartEventWatcher(scope, query, HandleSourceCreation); // Watch for deletion of LinkMe_InstrumentationSource objects. query = new WqlEventQuery("__InstanceDeletionEvent", "TargetInstance isa \"" + Constants.Wmi.Source.Class + "\""); WmiUtil.StartEventWatcher(scope, query, HandleSourceDeletion); // TODO: Need to work out how to stop - implement IDisposable? In practice only Instrumentation // runtime registeres a callback and it never stops until the AppDomain is shut down, anyway. }
public Namespace GetNamespace(INamespaceParent parent, string relativeName, bool ignoreCase) { // WMI is very slow, so do everything possible to minimise WMI access. We need to load all the // namespaces in the hierarchy (from the parent to the one being sought), because any of them // may have enabled events. Get them all in one query. // Build a query that gets all the namespaces we need (if they exist). string[] nsParts = relativeName.Split('.'); string parentFullName = parent.FullName; string queryString = BuildQueryStringForNamespaces(nsParts, parentFullName); // WQL is case-insensitive, so if ignoreCase is false use the Hashtable created below to filter. Hashtable namespaces = ignoreCase ? new Hashtable(StringComparer.InvariantCultureIgnoreCase) : new Hashtable(); var searcher = new ManagementObjectSearcher(_scope, new ObjectQuery(queryString)); // Read the namespaces returned and store them temporarily in a Hasthable. We cannot create // Namespace objects as we go, because they may be returned out of order (ie. children before parents). using (ManagementObjectCollection configs = searcher.Get()) { foreach (ManagementObject config in configs) { var details = new NamespaceDetails(); var nsName = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty); var nsParentName = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty); var enabledEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.EnabledEventsProperty); var mixedEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.MixedEventsProperty); details._enabledEvents = SplitString(enabledEvents, Constants.Module.ListSeparator); details._mixedEvents = SplitString(mixedEvents, Constants.Module.ListSeparator); namespaces.Add(CatalogueName.GetFullNameUnchecked(nsParentName, nsName), details); } } // Now that we have all the details create the hierarchy of Namespace objects. Catalogue catalogue = parent.Catalogue; Namespace ns = null; foreach (string nsName in nsParts) { string nsFullName = CatalogueName.GetFullNameUnchecked(parentFullName, nsName); var details = (NamespaceDetails)namespaces[nsFullName]; if (details == null) { return(null); // One of the namespaces in the path was not found in WMI. } ns = catalogue.CreateNamespace(parent, nsName, details._enabledEvents, details._mixedEvents); parent.AddIfNotLoaded(ns); parentFullName = nsFullName; parent = ns; } return(ns); }