Now calling display init. Not getting the expected error code though.
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;
17 using OpenHardwareMonitor.Hardware;
18 using OpenHardwareMonitor.Utilities;
19 using System.Runtime.InteropServices;
23 static class NativeMethods
26 public enum LoadLibraryFlags : uint
28 DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
29 LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
30 LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
31 LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
32 LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
33 LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
37 [DllImport("kernel32.dll", SetLastError = true)]
38 public static extern IntPtr LoadLibrary(string dllToLoad);
40 [DllImport("kernel32.dll")]
41 public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
43 [DllImport("kernel32.dll", SetLastError = true)]
44 public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
46 [DllImport("kernel32.dll", SetLastError = true)]
47 public static extern bool FreeLibrary(IntPtr hModule);
51 Definitions taken from public Sound Graph APIs.
53 static class SoundGraph
56 const int WM_APP = 0x8000;
57 public const int WM_DSP_PLUGIN_NOTIFY = WM_APP + 1121;
59 @brief These enumeration values represent the returned result for iMON Display API function calls.\n
60 All iMON Display API function calls return one of this result values.\n
61 For meaning of each result, refer the comment of each line below*/
62 public enum DSPResult : int
64 DSP_SUCCEEDED = 0, //// Function Call Succeeded Without Error
65 DSP_E_FAIL, //// Unspecified Failure
66 DSP_E_OUTOFMEMORY, //// Failed to Allocate Necessary Memory
67 DSP_E_INVALIDARG, //// One or More Arguments Are Not Valid
68 DSP_E_NOT_INITED, //// API is Not Initialized
69 DSP_E_POINTER, //// Pointer is Not Valid
71 DSP_S_INITED = 0x1000, //// API is Initialized
72 DSP_S_NOT_INITED, //// API is Not Initialized
73 DSP_S_IN_PLUGIN_MODE, //// API Can Control iMON Display (Display Plug-in Mode)
74 DSP_S_NOT_IN_PLUGIN_MODE, //// API Can't Control iMON Display
79 @brief These enumeration values represent the result status for requesting Display Plug-in Mode to iMON.\n
80 iMON Display API notifies one of this result values to the caller application after requesting Display Plug-in Mode to iMON.\n
81 For more information, refer the comment of each line below*/
82 public enum DSPNInitResult : int
84 DSPN_SUCCEEDED = 0, //// Display Plug-in Mode is Initialized Successfully
85 DSPN_ERR_IN_USED = 0x0100, //// Display Plug-in is Already Used by Other Application
86 DSPN_ERR_HW_DISCONNECTED, //// iMON HW is Not Connected
87 DSPN_ERR_NOT_SUPPORTED_HW, //// The Connected iMON HW doesn't Support Display Plug-in
88 DSPN_ERR_PLUGIN_DISABLED, //// Display Plug-in Mode Option is Disabled
89 DSPN_ERR_IMON_NO_REPLY, //// The Latest iMON is Not Installed or iMON Not Running
90 DSPN_ERR_UNKNOWN = 0x0200, //// Unknown Failure
95 @brief These enumeration values represent display type.\n
96 Currently iMON Display API supports VFD and LCD products.*/
97 public enum DSPType : int
100 DSPN_DSP_VFD = 0x01, //// VFD products
101 DSPN_DSP_LCD = 0x02, //// LCD products
106 @brief These enumeration values represent the notification codes.\n
107 iMON Display API will send or post message to the caller application.\n
108 The caller application should assign the message and the winodw handle to receivce message with IMON_Display_Init fucntion.\n
109 These enumeration values are used with WPARAM parameter of the message.\n
110 For more information, see the explanation of each notification code below*/
111 public enum DSPNotifyCode : int
113 /**DSPNM_PLUGIN_SUCCEED
114 @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
115 LPARAM represents DSPType. This value can be 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
116 DSPNM_PLUGIN_SUCCEED = 0,
118 /**DSPNM_PLUGIN_FAILED
119 @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
120 LPARAM represents error code with DSPNResult.*/
123 /**DSPNM_IMON_RESTARTED
124 @brief When iMON starts, API will post caller-specified message with DSPNM_IMON_RESTARTED as WPARAM parameter.\n
125 LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
126 DSPNM_IMON_RESTARTED,
129 @brief When iMON closed, API will post caller-specified message with DSPNM_IMON_CLOSED as WPARAM parameter.\n
130 LPARAM is not used.*/
133 /**DSPNM_HW_CONNECTED
134 @brief When iMON HW newly connected, API will post caller-specified message with DSPNM_HW_CONNECTED as WPARAM parameter.\n
135 LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
138 /**DSPNM_HW_DISCONNECTED
139 @brief When iMON HW disconnected, API will post caller-specified message with DSPNM_HW_DISCONNECTED as WPARAM parameter.\n
140 LPARAM is DSPNResult value, DSPN_ERR_HW_DISCONNECTED.*/
141 DSPNM_HW_DISCONNECTED,
144 /**DSPNM_LCD_TEXT_SCROLL_DONE
145 @brief When iMON LCD finishes scrolling Text, API will post caller-specified message with DSPNM_LCD_TEXT_SCROLL_DONE as WPARAM parameter.\n
146 The caller application may need to know when text scroll is finished, for sending next text.\n
147 LPARAM is not used.*/
148 DSPNM_LCD_TEXT_SCROLL_DONE = 0x1000,
152 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
153 public delegate DSPResult IMON_Display_Uninit();
155 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
156 public delegate DSPResult IMON_Display_Init(IntPtr hwndNoti, uint uMsgNotification);
158 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
159 public delegate DSPResult IMON_Display_IsInited();
161 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
162 public delegate DSPResult IMON_Display_IsPluginModeEnabled();
164 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
165 public delegate DSPResult IMON_Display_SetVfdText(
166 [MarshalAs(UnmanagedType.LPWStr)] string lpsz1stLine,
167 [MarshalAs(UnmanagedType.LPWStr)] string lpsz2ndLine);
169 //IMONDSPAPI DSPResult IMON_Display_SetVfdText(LPCTSTR lpsz1stLine, LPCTSTR lpsz2ndLine);
175 namespace OpenHardwareMonitor.GUI
177 public class SoundGraphDisplay : IDisposable
179 private IComputer computer;
180 private PersistentSettings settings;
181 private UnitManager unitManager;
182 private List<SensorFrontView> list = new List<SensorFrontView>();
183 //private bool mainIconEnabled = false;
184 //private NotifyIconAdv mainIcon;
185 IntPtr iSoundGraphDll;
186 SoundGraph.IMON_Display_Uninit iIMON_Display_Uninit;
187 SoundGraph.IMON_Display_Init iIMON_Display_Init;
188 SoundGraph.IMON_Display_IsInited iIMON_Display_IsInited;
189 SoundGraph.IMON_Display_IsPluginModeEnabled iIMON_Display_IsPluginModeEnabled;
190 SoundGraph.IMON_Display_SetVfdText iIMON_Display_SetVfdText;
192 public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
193 UnitManager unitManager)
195 this.computer = computer;
196 this.settings = settings;
197 this.unitManager = unitManager;
198 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
199 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
201 //Try loading SoundGraph iMON Disaply DLL
202 iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
203 int err=Marshal.GetLastWin32Error(); //If you 193 it means you need build for x86
206 Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
210 Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
214 Console.Write(@"iMON: DLL loaded.");
215 //Gather our function pointers
216 //TODO: Check returned pointers for validity
217 IntPtr functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Uninit");
218 iIMON_Display_Uninit = (SoundGraph.IMON_Display_Uninit)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Uninit));
220 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Init");
221 iIMON_Display_Init = (SoundGraph.IMON_Display_Init)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Init));
223 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsInited");
224 iIMON_Display_IsInited = (SoundGraph.IMON_Display_IsInited)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsInited));
226 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsPluginModeEnabled");
227 iIMON_Display_IsPluginModeEnabled = (SoundGraph.IMON_Display_IsPluginModeEnabled)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsPluginModeEnabled));
229 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_SetVfdText");
230 iIMON_Display_SetVfdText = (SoundGraph.IMON_Display_SetVfdText)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_SetVfdText));
235 private void HardwareRemoved(IHardware hardware)
237 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
238 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
239 foreach (ISensor sensor in hardware.Sensors)
240 SensorRemoved(sensor);
241 foreach (IHardware subHardware in hardware.SubHardware)
242 HardwareRemoved(subHardware);
245 private void HardwareAdded(IHardware hardware)
247 foreach (ISensor sensor in hardware.Sensors)
249 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
250 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
251 foreach (IHardware subHardware in hardware.SubHardware)
252 HardwareAdded(subHardware);
255 private void SensorAdded(ISensor sensor)
257 if (settings.GetValue(new Identifier(sensor.Identifier,
258 "tray").ToString(), false))
262 private void SensorRemoved(ISensor sensor)
264 if (Contains(sensor))
265 Remove(sensor, false);
268 public void Dispose()
270 foreach (SensorFrontView icon in list)
274 if (iSoundGraphDll != IntPtr.Zero)
276 bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
283 foreach (SensorFrontView icon in list)
287 public bool Contains(ISensor sensor)
289 foreach (SensorFrontView icon in list)
290 if (icon.Sensor == sensor)
295 public void Add(ISensor sensor, bool balloonTip)
297 if (Contains(sensor))
304 //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
305 //UpdateMainIconVisibilty();
306 //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
310 public void Remove(ISensor sensor)
312 Remove(sensor, true);
315 private void Remove(ISensor sensor, bool deleteConfig)
320 new Identifier(sensor.Identifier, "tray").ToString());
322 new Identifier(sensor.Identifier, "traycolor").ToString());
324 SensorFrontView instance = null;
325 foreach (SensorFrontView icon in list)
326 if (icon.Sensor == sensor)
328 if (instance != null)
330 list.Remove(instance);
331 UpdateMainIconVisibilty();
338 private void UpdateMainIconVisibilty()
343 mainIcon.Visible = list.Count == 0;
347 mainIcon.Visible = false;
352 public void Init(IntPtr aHWND)
354 iIMON_Display_Init(aHWND, SoundGraph.WM_DSP_PLUGIN_NOTIFY);
359 iIMON_Display_Uninit();
362 public void SetText(string aUpperLine, string aLowerLine)
368 public bool IsMainIconEnabled
370 get { return mainIconEnabled; }
373 if (mainIconEnabled != value)
375 mainIconEnabled = value;
376 UpdateMainIconVisibilty();
382 public bool IsDllLoaded
384 get { return iSoundGraphDll!=IntPtr.Zero; }
387 public void DisplayPluginMessage(int uErrCode, bool bError)
391 switch ((SoundGraph.DSPNInitResult)uErrCode)
393 case SoundGraph.DSPNInitResult.DSPN_ERR_IN_USED:
394 Console.Write("Display Plug-in is Already Used by Other Application.\n"); break;
395 case SoundGraph.DSPNInitResult.DSPN_ERR_HW_DISCONNECTED:
396 Console.Write("iMON HW is Not Connected."); break;
397 case SoundGraph.DSPNInitResult.DSPN_ERR_NOT_SUPPORTED_HW:
398 Console.Write("The Connected iMON HW doesn't Support Display Plug-in.\n"); break;
399 case SoundGraph.DSPNInitResult.DSPN_ERR_PLUGIN_DISABLED:
400 Console.Write("Display Plug-in Mode Option is Disabled.\n"); break;
401 case SoundGraph.DSPNInitResult.DSPN_ERR_IMON_NO_REPLY:
402 Console.Write("The Latest iMON is Not Installed or iMON Not Running.\n"); break;
403 case SoundGraph.DSPNInitResult.DSPN_ERR_UNKNOWN:
404 Console.Write("Unknown Failure.\n"); break;
409 switch ((SoundGraph.DSPNotifyCode)uErrCode)
411 case SoundGraph.DSPNotifyCode.DSPNM_PLUGIN_SUCCEED:
412 Console.Write("Plug-in Mode Inited Successfully.\n"); break;
413 case SoundGraph.DSPNotifyCode.DSPNM_IMON_RESTARTED:
414 Console.Write("iMON Started and Plug-in Mode Inited.\n"); break;
415 case SoundGraph.DSPNotifyCode.DSPNM_HW_CONNECTED:
416 Console.Write("iMON HW Connected and Plug-in Mode Inited.\n"); break;
420 //GetDlgItem(IDC_STATIC_INFO)->SetWindowText((LPCTSTR)strErrMsg);