GUI/SoundGraphDisplay.cs
branchMiniDisplay
changeset 436 e9aefd454d1e
parent 435 9ffcd8ed8537
child 437 38e7b78cf732
     1.1 --- a/GUI/SoundGraphDisplay.cs	Mon Feb 04 00:47:01 2013 +0100
     1.2 +++ b/GUI/SoundGraphDisplay.cs	Mon Feb 02 12:51:06 2015 +0100
     1.3 @@ -12,164 +12,18 @@
     1.4  using System.Collections.Generic;
     1.5  using System.Drawing;
     1.6  using System.Text;
     1.7 +using System.Diagnostics;
     1.8  using System.Windows.Forms;
     1.9  using System.Windows;
    1.10  using OpenHardwareMonitor.Hardware;
    1.11  using OpenHardwareMonitor.Utilities;
    1.12  using System.Runtime.InteropServices;
    1.13 +using UacHelpers;
    1.14  
    1.15  
    1.16  
    1.17 -static class NativeMethods
    1.18 -{
    1.19 -    [System.Flags]
    1.20 -    public enum LoadLibraryFlags : uint
    1.21 -    {
    1.22 -        DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
    1.23 -        LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
    1.24 -        LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
    1.25 -        LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
    1.26 -        LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
    1.27 -        LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
    1.28 -    }
    1.29  
    1.30  
    1.31 -    [DllImport("kernel32.dll", SetLastError = true)]
    1.32 -    public static extern IntPtr LoadLibrary(string dllToLoad);
    1.33 -
    1.34 -    [DllImport("kernel32.dll")]
    1.35 -    public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
    1.36 -
    1.37 -    [DllImport("kernel32.dll", SetLastError = true)]
    1.38 -    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    1.39 -
    1.40 -    [DllImport("kernel32.dll", SetLastError = true)]
    1.41 -    public static extern bool FreeLibrary(IntPtr hModule);
    1.42 -}
    1.43 -
    1.44 -/**
    1.45 -Definitions taken from public Sound Graph APIs.
    1.46 - */
    1.47 -static class SoundGraph
    1.48 -{
    1.49 -
    1.50 -    const int WM_APP = 0x8000;
    1.51 -    public const int WM_DSP_PLUGIN_NOTIFY = WM_APP + 1121;
    1.52 -    /**DSPResult
    1.53 -    @brief	These enumeration values represent the returned result for iMON Display API function calls.\n
    1.54 -			    All iMON Display API function calls return one of this result values.\n
    1.55 -			    For meaning of each result, refer the comment of each line below*/
    1.56 -    public enum DSPResult : int
    1.57 -    {
    1.58 -	    DSP_SUCCEEDED = 0,				//// Function Call Succeeded Without Error
    1.59 -	    DSP_E_FAIL,						//// Unspecified Failure
    1.60 -	    DSP_E_OUTOFMEMORY,				//// Failed to Allocate Necessary Memory
    1.61 -	    DSP_E_INVALIDARG,				//// One or More Arguments Are Not Valid
    1.62 -	    DSP_E_NOT_INITED,				//// API is Not Initialized
    1.63 -	    DSP_E_POINTER,					//// Pointer is Not Valid
    1.64 -
    1.65 -	    DSP_S_INITED = 0x1000,			//// API is Initialized
    1.66 -	    DSP_S_NOT_INITED,				//// API is Not Initialized
    1.67 -	    DSP_S_IN_PLUGIN_MODE,			//// API Can Control iMON Display (Display Plug-in Mode)
    1.68 -	    DSP_S_NOT_IN_PLUGIN_MODE,		//// API Can't Control iMON Display
    1.69 -    };
    1.70 -
    1.71 -
    1.72 -    /**DSPNInitResult
    1.73 -    @brief	These enumeration values represent the result status for requesting Display Plug-in Mode to iMON.\n
    1.74 -			    iMON Display API notifies one of this result values to the caller application after requesting Display Plug-in Mode to iMON.\n
    1.75 -			    For more information, refer the comment of each line below*/
    1.76 -    public enum DSPNInitResult : int
    1.77 -    {
    1.78 -	    DSPN_SUCCEEDED = 0,				//// Display Plug-in Mode is Initialized Successfully
    1.79 -	    DSPN_ERR_IN_USED = 0x0100,		//// Display Plug-in is Already Used by Other Application
    1.80 -	    DSPN_ERR_HW_DISCONNECTED,		//// iMON HW is Not Connected
    1.81 -	    DSPN_ERR_NOT_SUPPORTED_HW,		//// The Connected iMON HW doesn't Support Display Plug-in
    1.82 -	    DSPN_ERR_PLUGIN_DISABLED,		//// Display Plug-in Mode Option is Disabled
    1.83 -	    DSPN_ERR_IMON_NO_REPLY,			//// The Latest iMON is Not Installed or iMON Not Running
    1.84 -	    DSPN_ERR_UNKNOWN = 0x0200,		//// Unknown Failure
    1.85 -    };
    1.86 -
    1.87 -
    1.88 -    /**DSPType
    1.89 -    @brief	These enumeration values represent display type.\n
    1.90 -			    Currently iMON Display API supports VFD and LCD products.*/
    1.91 -    public enum DSPType : int
    1.92 -    {
    1.93 -	    DSPN_DSP_NONE	= 0,
    1.94 -	    DSPN_DSP_VFD	= 0x01,			//// VFD products
    1.95 -	    DSPN_DSP_LCD	= 0x02,			//// LCD products
    1.96 -    };
    1.97 -
    1.98 -
    1.99 -    /**DSPNotifyCode
   1.100 -    @brief	These enumeration values represent the notification codes.\n
   1.101 -			    iMON Display API will send or post message to the caller application.\n
   1.102 -			    The caller application should assign the message and the winodw handle to receivce message with IMON_Display_Init fucntion.\n
   1.103 -			    These enumeration values are used with WPARAM parameter of the message.\n 
   1.104 -			    For more information, see the explanation of each notification code below*/
   1.105 -    public enum DSPNotifyCode : int
   1.106 -    {
   1.107 -	    /**DSPNM_PLUGIN_SUCCEED
   1.108 -	    @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
   1.109 -				    LPARAM represents DSPType. This value can be 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   1.110 -	    DSPNM_PLUGIN_SUCCEED = 0,
   1.111 -
   1.112 -	    /**DSPNM_PLUGIN_FAILED
   1.113 -	    @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
   1.114 -				    LPARAM represents error code with DSPNResult.*/
   1.115 -	    DSPNM_PLUGIN_FAILED,
   1.116 -
   1.117 -	    /**DSPNM_IMON_RESTARTED
   1.118 -	    @brief	When iMON starts, API will post caller-specified message with DSPNM_IMON_RESTARTED as WPARAM parameter.\n
   1.119 -				    LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   1.120 -	    DSPNM_IMON_RESTARTED,
   1.121 -
   1.122 -	    /**DSPNM_IMON_CLOSED
   1.123 -	    @brief	When iMON closed, API will post caller-specified message with DSPNM_IMON_CLOSED as WPARAM parameter.\n
   1.124 -				    LPARAM is not used.*/
   1.125 -	    DSPNM_IMON_CLOSED,
   1.126 -
   1.127 -	    /**DSPNM_HW_CONNECTED
   1.128 -	    @brief	When iMON HW newly connected, API will post caller-specified message with DSPNM_HW_CONNECTED as WPARAM parameter.\n
   1.129 -				    LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   1.130 -	    DSPNM_HW_CONNECTED,
   1.131 -
   1.132 -	    /**DSPNM_HW_DISCONNECTED
   1.133 -	    @brief	When iMON HW disconnected, API will post caller-specified message with DSPNM_HW_DISCONNECTED as WPARAM parameter.\n
   1.134 -				    LPARAM is DSPNResult value, DSPN_ERR_HW_DISCONNECTED.*/
   1.135 -	    DSPNM_HW_DISCONNECTED,
   1.136 -
   1.137 -
   1.138 -	    /**DSPNM_LCD_TEXT_SCROLL_DONE
   1.139 -	    @brief	When iMON LCD finishes scrolling Text, API will post caller-specified message with DSPNM_LCD_TEXT_SCROLL_DONE as WPARAM parameter.\n
   1.140 -				    The caller application may need to know when text scroll is finished, for sending next text.\n
   1.141 -				    LPARAM is not used.*/
   1.142 -	    DSPNM_LCD_TEXT_SCROLL_DONE = 0x1000,
   1.143 -    };
   1.144 -
   1.145 -    /// Functions
   1.146 -    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   1.147 -    public delegate DSPResult IMON_Display_Uninit();
   1.148 -
   1.149 -    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   1.150 -    public delegate DSPResult IMON_Display_Init(IntPtr hwndNoti, uint uMsgNotification);
   1.151 -
   1.152 -    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   1.153 -    public delegate DSPResult IMON_Display_IsInited();
   1.154 -
   1.155 -    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   1.156 -    public delegate DSPResult IMON_Display_IsPluginModeEnabled();
   1.157 -
   1.158 -    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   1.159 -    public delegate DSPResult IMON_Display_SetVfdText(
   1.160 -        [MarshalAs(UnmanagedType.LPWStr)] string lpsz1stLine,
   1.161 -        [MarshalAs(UnmanagedType.LPWStr)] string lpsz2ndLine);
   1.162 -
   1.163 -    //IMONDSPAPI DSPResult IMON_Display_SetVfdText(LPCTSTR lpsz1stLine, LPCTSTR lpsz2ndLine);
   1.164 -
   1.165 -}
   1.166 -
   1.167  
   1.168  
   1.169  namespace OpenHardwareMonitor.GUI
   1.170 @@ -180,14 +34,8 @@
   1.171          private PersistentSettings settings;
   1.172          private UnitManager unitManager;
   1.173          private List<SensorFrontView> list = new List<SensorFrontView>();
   1.174 -        //private bool mainIconEnabled = false;
   1.175 -        //private NotifyIconAdv mainIcon;
   1.176 -        IntPtr iSoundGraphDll;
   1.177 -        SoundGraph.IMON_Display_Uninit iIMON_Display_Uninit;
   1.178 -        SoundGraph.IMON_Display_Init iIMON_Display_Init;
   1.179 -        SoundGraph.IMON_Display_IsInited iIMON_Display_IsInited;
   1.180 -        SoundGraph.IMON_Display_IsPluginModeEnabled iIMON_Display_IsPluginModeEnabled;
   1.181 -        SoundGraph.IMON_Display_SetVfdText iIMON_Display_SetVfdText;
   1.182 +        private SoundGraph.Server iServer;
   1.183 +
   1.184  
   1.185          public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
   1.186            UnitManager unitManager)
   1.187 @@ -198,38 +46,23 @@
   1.188              computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   1.189              computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   1.190  
   1.191 +            //Start our client if needed
   1.192 +            Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
   1.193 +            if (!(processes.Length > 0))
   1.194 +            {
   1.195 +
   1.196 +                //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
   1.197 +                    /*
   1.198 +                Process client = new Process();
   1.199 +                client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
   1.200 +                client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
   1.201 +                client.Start();*/
   1.202 +            }
   1.203 +
   1.204              //Try loading SoundGraph iMON Disaply DLL
   1.205 -            iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
   1.206 -            int err=Marshal.GetLastWin32Error(); //If you 193  it means you need build for x86
   1.207 -            if (err == 193)
   1.208 -            {
   1.209 -                Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
   1.210 -            }
   1.211 -            else if (err != 0)
   1.212 -            {
   1.213 -                Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
   1.214 -            }
   1.215 -            else
   1.216 -            {
   1.217 -                Console.Write(@"iMON: DLL loaded.");
   1.218 -                //Gather our function pointers
   1.219 -                //TODO: Check returned pointers for validity
   1.220 -                IntPtr functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Uninit");
   1.221 -                iIMON_Display_Uninit = (SoundGraph.IMON_Display_Uninit)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Uninit));
   1.222 -                
   1.223 -                functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Init");
   1.224 -                iIMON_Display_Init = (SoundGraph.IMON_Display_Init)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Init));
   1.225 -                
   1.226 -                functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsInited");
   1.227 -                iIMON_Display_IsInited = (SoundGraph.IMON_Display_IsInited)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsInited));
   1.228 -                
   1.229 -                functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsPluginModeEnabled");
   1.230 -                iIMON_Display_IsPluginModeEnabled = (SoundGraph.IMON_Display_IsPluginModeEnabled)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsPluginModeEnabled));
   1.231 -
   1.232 -                functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_SetVfdText");
   1.233 -                iIMON_Display_SetVfdText = (SoundGraph.IMON_Display_SetVfdText)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_SetVfdText));
   1.234 -
   1.235 -            }
   1.236 +            iServer = new SoundGraph.Server(@"\\.\pipe\sga-receiver", @"\\.\pipe\sga-sender");
   1.237 +            iServer.Start();
   1.238 +            //iServer.SendMessage("init:");
   1.239          }
   1.240  
   1.241          private void HardwareRemoved(IHardware hardware)
   1.242 @@ -270,11 +103,7 @@
   1.243              foreach (SensorFrontView icon in list)
   1.244                  icon.Dispose();
   1.245  
   1.246 -            //Unload our DLL
   1.247 -            if (iSoundGraphDll != IntPtr.Zero)
   1.248 -            {
   1.249 -                bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
   1.250 -            }
   1.251 +            iServer.Stop();
   1.252  
   1.253          }
   1.254  
   1.255 @@ -349,19 +178,19 @@
   1.256               */
   1.257          }
   1.258  
   1.259 -        public void Init(IntPtr aHWND)
   1.260 +        public void Init()
   1.261          {
   1.262 -            iIMON_Display_Init(aHWND, SoundGraph.WM_DSP_PLUGIN_NOTIFY);
   1.263 +            iServer.SendMessage("init:");
   1.264          }
   1.265  
   1.266          public void Uninit()
   1.267          {
   1.268 -            iIMON_Display_Uninit();
   1.269 +            iServer.SendMessage("uninit:");
   1.270          }
   1.271  
   1.272          public void SetText(string aUpperLine, string aLowerLine)
   1.273          {
   1.274 -
   1.275 +            iServer.SendMessage("set-vfd-text:" + aUpperLine);
   1.276          }
   1.277  
   1.278          /*
   1.279 @@ -378,47 +207,13 @@
   1.280              }
   1.281          }*/
   1.282  
   1.283 -
   1.284 +        /*
   1.285          public bool IsDllLoaded
   1.286          {
   1.287              get { return iSoundGraphDll!=IntPtr.Zero; }
   1.288          }
   1.289 +         */
   1.290  
   1.291 -        public void DisplayPluginMessage(int uErrCode, bool bError)
   1.292 -        {
   1.293 -	        if(bError)
   1.294 -	        {
   1.295 -                switch ((SoundGraph.DSPNInitResult)uErrCode)
   1.296 -		        {
   1.297 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_IN_USED: 
   1.298 -                        Console.Write("Display Plug-in is Already Used by Other Application.\n"); break;
   1.299 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_HW_DISCONNECTED: 
   1.300 -                        Console.Write("iMON HW is Not Connected."); break;
   1.301 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_NOT_SUPPORTED_HW: 
   1.302 -                        Console.Write("The Connected iMON HW doesn't Support Display Plug-in.\n"); break;
   1.303 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_PLUGIN_DISABLED: 
   1.304 -                        Console.Write("Display Plug-in Mode Option is Disabled.\n"); break;
   1.305 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_IMON_NO_REPLY: 
   1.306 -                        Console.Write("The Latest iMON is Not Installed or iMON Not Running.\n"); break;
   1.307 -                    case SoundGraph.DSPNInitResult.DSPN_ERR_UNKNOWN: 
   1.308 -                        Console.Write("Unknown Failure.\n"); break;
   1.309 -		        }
   1.310 -    	    }
   1.311 -	        else
   1.312 -            {
   1.313 -                switch ((SoundGraph.DSPNotifyCode)uErrCode)
   1.314 -		        {
   1.315 -                    case SoundGraph.DSPNotifyCode.DSPNM_PLUGIN_SUCCEED: 
   1.316 -                        Console.Write("Plug-in Mode Inited Successfully.\n"); break;
   1.317 -                    case SoundGraph.DSPNotifyCode.DSPNM_IMON_RESTARTED: 
   1.318 -                        Console.Write("iMON Started and Plug-in Mode Inited.\n"); break;
   1.319 -                    case SoundGraph.DSPNotifyCode.DSPNM_HW_CONNECTED: 
   1.320 -                        Console.Write("iMON HW Connected and Plug-in Mode Inited.\n"); break;
   1.321 -		        }
   1.322 -	        }
   1.323 -	    
   1.324 -            //GetDlgItem(IDC_STATIC_INFO)->SetWindowText((LPCTSTR)strErrMsg);
   1.325 -        }
   1.326  
   1.327      }
   1.328  }