Now fetching some of our Sound Graph DLL function pointers.
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-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
15 using System.Windows.Forms;
16 using OpenHardwareMonitor.Hardware;
17 using OpenHardwareMonitor.Utilities;
18 using System.Runtime.InteropServices;
22 static class NativeMethods
25 public enum LoadLibraryFlags : uint
27 DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
28 LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
29 LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
30 LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
31 LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
32 LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
36 [DllImport("kernel32.dll", SetLastError = true)]
37 public static extern IntPtr LoadLibrary(string dllToLoad);
39 [DllImport("kernel32.dll")]
40 public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
42 [DllImport("kernel32.dll", SetLastError = true)]
43 public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
45 [DllImport("kernel32.dll", SetLastError = true)]
46 public static extern bool FreeLibrary(IntPtr hModule);
50 Definitions taken from public Sound Graph APIs.
52 static class SoundGraph
55 @brief These enumeration values represent the returned result for iMON Display API function calls.\n
56 All iMON Display API function calls return one of this result values.\n
57 For meaning of each result, refer the comment of each line below*/
58 public enum DSPResult : uint
60 DSP_SUCCEEDED = 0, //// Function Call Succeeded Without Error
61 DSP_E_FAIL, //// Unspecified Failure
62 DSP_E_OUTOFMEMORY, //// Failed to Allocate Necessary Memory
63 DSP_E_INVALIDARG, //// One or More Arguments Are Not Valid
64 DSP_E_NOT_INITED, //// API is Not Initialized
65 DSP_E_POINTER, //// Pointer is Not Valid
67 DSP_S_INITED = 0x1000, //// API is Initialized
68 DSP_S_NOT_INITED, //// API is Not Initialized
69 DSP_S_IN_PLUGIN_MODE, //// API Can Control iMON Display (Display Plug-in Mode)
70 DSP_S_NOT_IN_PLUGIN_MODE, //// API Can't Control iMON Display
75 @brief These enumeration values represent the result status for requesting Display Plug-in Mode to iMON.\n
76 iMON Display API notifies one of this result values to the caller application after requesting Display Plug-in Mode to iMON.\n
77 For more information, refer the comment of each line below*/
78 public enum DSPNInitResult : uint
80 DSPN_SUCCEEDED = 0, //// Display Plug-in Mode is Initialized Successfully
81 DSPN_ERR_IN_USED = 0x0100, //// Display Plug-in is Already Used by Other Application
82 DSPN_ERR_HW_DISCONNECTED, //// iMON HW is Not Connected
83 DSPN_ERR_NOT_SUPPORTED_HW, //// The Connected iMON HW doesn't Support Display Plug-in
84 DSPN_ERR_PLUGIN_DISABLED, //// Display Plug-in Mode Option is Disabled
85 DSPN_ERR_IMON_NO_REPLY, //// The Latest iMON is Not Installed or iMON Not Running
86 DSPN_ERR_UNKNOWN = 0x0200, //// Unknown Failure
91 @brief These enumeration values represent display type.\n
92 Currently iMON Display API supports VFD and LCD products.*/
93 public enum DSPType : uint
96 DSPN_DSP_VFD = 0x01, //// VFD products
97 DSPN_DSP_LCD = 0x02, //// LCD products
102 @brief These enumeration values represent the notification codes.\n
103 iMON Display API will send or post message to the caller application.\n
104 The caller application should assign the message and the winodw handle to receivce message with IMON_Display_Init fucntion.\n
105 These enumeration values are used with WPARAM parameter of the message.\n
106 For more information, see the explanation of each notification code below*/
107 public enum DSPNotifyCode : uint
109 /**DSPNM_PLUGIN_SUCCEED
110 @brief When API succeeds to get the control for the display, API will post caller-specified message with DSPNM_PLUGIN_SUCCEED as WPARAM parameter.\n
111 LPARAM represents DSPType. This value can be 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
112 DSPNM_PLUGIN_SUCCEED = 0,
114 /**DSPNM_PLUGIN_FAILED
115 @brief When API fails to get the control for the display, API will post caller-specified message with DSPNM_PLUGIN_FAILED as WPARAM parameter.\n
116 LPARAM represents error code with DSPNResult.*/
119 /**DSPNM_IMON_RESTARTED
120 @brief When iMON starts, API will post caller-specified message with DSPNM_IMON_RESTARTED as WPARAM parameter.\n
121 LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
122 DSPNM_IMON_RESTARTED,
125 @brief When iMON closed, API will post caller-specified message with DSPNM_IMON_CLOSED as WPARAM parameter.\n
126 LPARAM is not used.*/
129 /**DSPNM_HW_CONNECTED
130 @brief When iMON HW newly connected, API will post caller-specified message with DSPNM_HW_CONNECTED as WPARAM parameter.\n
131 LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
134 /**DSPNM_HW_DISCONNECTED
135 @brief When iMON HW disconnected, API will post caller-specified message with DSPNM_HW_DISCONNECTED as WPARAM parameter.\n
136 LPARAM is DSPNResult value, DSPN_ERR_HW_DISCONNECTED.*/
137 DSPNM_HW_DISCONNECTED,
140 /**DSPNM_LCD_TEXT_SCROLL_DONE
141 @brief When iMON LCD finishes scrolling Text, API will post caller-specified message with DSPNM_LCD_TEXT_SCROLL_DONE as WPARAM parameter.\n
142 The caller application may need to know when text scroll is finished, for sending next text.\n
143 LPARAM is not used.*/
144 DSPNM_LCD_TEXT_SCROLL_DONE = 0x1000,
148 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
149 public delegate DSPResult IMON_Display_Uninit();
151 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
152 public delegate DSPResult IMON_Display_Init(IntPtr hwndNoti, uint uMsgNotification);
154 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
155 public delegate DSPResult IMON_Display_IsInited();
157 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
158 public delegate DSPResult IMON_Display_IsPluginModeEnabled();
160 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
161 public delegate DSPResult IMON_Display_SetVfdText(
162 [MarshalAs(UnmanagedType.LPWStr)] string lpsz1stLine,
163 [MarshalAs(UnmanagedType.LPWStr)] string lpsz2ndLine);
165 //IMONDSPAPI DSPResult IMON_Display_SetVfdText(LPCTSTR lpsz1stLine, LPCTSTR lpsz2ndLine);
171 namespace OpenHardwareMonitor.GUI
173 public class SoundGraphDisplay : IDisposable
175 private IComputer computer;
176 private PersistentSettings settings;
177 private UnitManager unitManager;
178 private List<SensorFrontView> list = new List<SensorFrontView>();
179 //private bool mainIconEnabled = false;
180 //private NotifyIconAdv mainIcon;
181 IntPtr iSoundGraphDll;
182 SoundGraph.IMON_Display_Uninit iIMON_Display_Uninit;
183 SoundGraph.IMON_Display_Init iIMON_Display_Init;
184 SoundGraph.IMON_Display_IsInited iIMON_Display_IsInited;
185 SoundGraph.IMON_Display_IsPluginModeEnabled iIMON_Display_IsPluginModeEnabled;
186 SoundGraph.IMON_Display_SetVfdText iIMON_Display_SetVfdText;
188 public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
189 UnitManager unitManager)
191 this.computer = computer;
192 this.settings = settings;
193 this.unitManager = unitManager;
194 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
195 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
197 //Try loading SoundGraph iMON Disaply DLL
198 iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
199 int err=Marshal.GetLastWin32Error(); //If you 193 it means you need build for x86
202 Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
206 Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
210 Console.Write(@"iMON: DLL loaded.");
211 //Gather our function pointers
212 //TODO: Check returned pointers for validity
213 IntPtr functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Uninit");
214 iIMON_Display_Uninit = (SoundGraph.IMON_Display_Uninit)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Uninit));
216 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Init");
217 iIMON_Display_Init = (SoundGraph.IMON_Display_Init)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Init));
219 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsInited");
220 iIMON_Display_IsInited = (SoundGraph.IMON_Display_IsInited)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsInited));
222 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsPluginModeEnabled");
223 iIMON_Display_IsPluginModeEnabled = (SoundGraph.IMON_Display_IsPluginModeEnabled)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsPluginModeEnabled));
225 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_SetVfdText");
226 iIMON_Display_SetVfdText = (SoundGraph.IMON_Display_SetVfdText)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_SetVfdText));
231 private void HardwareRemoved(IHardware hardware)
233 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
234 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
235 foreach (ISensor sensor in hardware.Sensors)
236 SensorRemoved(sensor);
237 foreach (IHardware subHardware in hardware.SubHardware)
238 HardwareRemoved(subHardware);
241 private void HardwareAdded(IHardware hardware)
243 foreach (ISensor sensor in hardware.Sensors)
245 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
246 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
247 foreach (IHardware subHardware in hardware.SubHardware)
248 HardwareAdded(subHardware);
251 private void SensorAdded(ISensor sensor)
253 if (settings.GetValue(new Identifier(sensor.Identifier,
254 "tray").ToString(), false))
258 private void SensorRemoved(ISensor sensor)
260 if (Contains(sensor))
261 Remove(sensor, false);
264 public void Dispose()
266 foreach (SensorFrontView icon in list)
270 if (iSoundGraphDll != IntPtr.Zero)
272 bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
279 foreach (SensorFrontView icon in list)
283 public bool Contains(ISensor sensor)
285 foreach (SensorFrontView icon in list)
286 if (icon.Sensor == sensor)
291 public void Add(ISensor sensor, bool balloonTip)
293 if (Contains(sensor))
300 //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
301 //UpdateMainIconVisibilty();
302 //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
306 public void Remove(ISensor sensor)
308 Remove(sensor, true);
311 private void Remove(ISensor sensor, bool deleteConfig)
316 new Identifier(sensor.Identifier, "tray").ToString());
318 new Identifier(sensor.Identifier, "traycolor").ToString());
320 SensorFrontView instance = null;
321 foreach (SensorFrontView icon in list)
322 if (icon.Sensor == sensor)
324 if (instance != null)
326 list.Remove(instance);
327 UpdateMainIconVisibilty();
334 private void UpdateMainIconVisibilty()
339 mainIcon.Visible = list.Count == 0;
343 mainIcon.Visible = false;
348 private void Init(IntPtr aHWND)
353 private void Uninit()
355 iIMON_Display_Uninit();
358 private void SetText(string aUpperLine, string aLowerLine)
364 public bool IsMainIconEnabled
366 get { return mainIconEnabled; }
369 if (mainIconEnabled != value)
371 mainIconEnabled = value;
372 UpdateMainIconVisibilty();
378 public bool IsDllLoaded
380 get { return iSoundGraphDll!=IntPtr.Zero; }