Changed the license to the Mozilla Public License 2.0 and update the licensing information.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Paul Werelds <paul@werelds.net>
12 using System.Collections.Generic;
13 using System.Management.Instrumentation;
14 using OpenHardwareMonitor.Hardware;
16 [assembly: Instrumented("root/OpenHardwareMonitor")]
18 [System.ComponentModel.RunInstaller(true)]
19 public class InstanceInstaller : DefaultManagementProjectInstaller { }
21 namespace OpenHardwareMonitor.WMI {
24 /// This class is not exposed to WMI itself.
26 public class WmiProvider : IDisposable {
27 private List<IWmiObject> activeInstances;
29 public WmiProvider(IComputer computer) {
30 activeInstances = new List<IWmiObject>();
32 foreach (IHardware hardware in computer.Hardware)
33 ComputerHardwareAdded(hardware);
35 computer.HardwareAdded += ComputerHardwareAdded;
36 computer.HardwareRemoved += ComputerHardwareRemoved;
39 public void Update() {
40 foreach (IWmiObject instance in activeInstances)
46 private void ComputerHardwareAdded(IHardware hardware) {
47 if (!Exists(hardware.Identifier.ToString())) {
48 foreach (ISensor sensor in hardware.Sensors)
49 HardwareSensorAdded(sensor);
51 hardware.SensorAdded += HardwareSensorAdded;
52 hardware.SensorRemoved += HardwareSensorRemoved;
54 Hardware hw = new Hardware(hardware);
55 activeInstances.Add(hw);
58 Instrumentation.Publish(hw);
59 } catch (Exception) { }
62 foreach (IHardware subHardware in hardware.SubHardware)
63 ComputerHardwareAdded(subHardware);
66 private void HardwareSensorAdded(ISensor data) {
67 Sensor sensor = new Sensor(data);
68 activeInstances.Add(sensor);
71 Instrumentation.Publish(sensor);
72 } catch (Exception) { }
75 private void ComputerHardwareRemoved(IHardware hardware) {
76 hardware.SensorAdded -= HardwareSensorAdded;
77 hardware.SensorRemoved -= HardwareSensorRemoved;
79 foreach (ISensor sensor in hardware.Sensors)
80 HardwareSensorRemoved(sensor);
82 foreach (IHardware subHardware in hardware.SubHardware)
83 ComputerHardwareRemoved(subHardware);
85 RevokeInstance(hardware.Identifier.ToString());
88 private void HardwareSensorRemoved(ISensor sensor) {
89 RevokeInstance(sensor.Identifier.ToString());
96 private bool Exists(string identifier) {
97 return activeInstances.Exists(h => h.Identifier == identifier);
100 private void RevokeInstance(string identifier) {
101 int instanceIndex = activeInstances.FindIndex(
102 item => item.Identifier == identifier.ToString()
106 Instrumentation.Revoke(activeInstances[instanceIndex]);
107 } catch (Exception) { }
109 activeInstances.RemoveAt(instanceIndex);
114 public void Dispose() {
115 foreach (IWmiObject instance in activeInstances) {
117 Instrumentation.Revoke(instance);
118 } catch (Exception) { }
120 activeInstances = null;