3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Paul Werelds <paul@werelds.net>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Management.Instrumentation;
41 using OpenHardwareMonitor.Hardware;
43 [assembly: Instrumented("root/OpenHardwareMonitor")]
45 [System.ComponentModel.RunInstaller(true)]
46 public class InstanceInstaller : DefaultManagementProjectInstaller { }
48 namespace OpenHardwareMonitor.WMI {
51 /// This class is not exposed to WMI itself.
53 public class WmiProvider : IDisposable {
54 private List<IWmiObject> activeInstances;
56 public WmiProvider(IComputer computer) {
57 activeInstances = new List<IWmiObject>();
59 foreach (IHardware hardware in computer.Hardware)
60 ComputerHardwareAdded(hardware);
62 computer.HardwareAdded += ComputerHardwareAdded;
63 computer.HardwareRemoved += ComputerHardwareRemoved;
66 public void Update() {
67 foreach (IWmiObject instance in activeInstances)
73 private void ComputerHardwareAdded(IHardware hardware) {
74 if (!Exists(hardware.Identifier.ToString())) {
75 foreach (ISensor sensor in hardware.Sensors)
76 HardwareSensorAdded(sensor);
78 hardware.SensorAdded += HardwareSensorAdded;
79 hardware.SensorRemoved += HardwareSensorRemoved;
81 Hardware hw = new Hardware(hardware);
82 activeInstances.Add(hw);
84 Instrumentation.Publish(hw);
87 foreach (IHardware subHardware in hardware.SubHardware)
88 ComputerHardwareAdded(subHardware);
91 private void HardwareSensorAdded(ISensor data) {
92 Sensor sensor = new Sensor(data);
93 activeInstances.Add(sensor);
95 Instrumentation.Publish(sensor);
98 private void ComputerHardwareRemoved(IHardware hardware) {
99 hardware.SensorAdded -= HardwareSensorAdded;
100 hardware.SensorRemoved -= HardwareSensorRemoved;
102 foreach (ISensor sensor in hardware.Sensors)
103 HardwareSensorRemoved(sensor);
105 foreach (IHardware subHardware in hardware.SubHardware)
106 ComputerHardwareRemoved(subHardware);
108 RevokeInstance(hardware.Identifier.ToString());
111 private void HardwareSensorRemoved(ISensor sensor) {
112 RevokeInstance(sensor.Identifier.ToString());
119 private bool Exists(string identifier) {
120 return activeInstances.Exists(h => h.Identifier == identifier);
123 private void RevokeInstance(string identifier) {
124 int instanceIndex = activeInstances.FindIndex(
125 item => item.Identifier == identifier.ToString()
128 Instrumentation.Revoke(activeInstances[instanceIndex]);
130 activeInstances.RemoveAt(instanceIndex);
135 public void Dispose() {
136 foreach (IWmiObject instance in activeInstances)
137 Instrumentation.Revoke(instance);
138 activeInstances = null;