public static PortMonitor AddPortMonitor(string portName, string dllName, string environment = null, string computerName = null) { if (string.IsNullOrEmpty(portName)) { throw new ArgumentException("Contract assertion not met: !string.IsNullOrEmpty(portName)", nameof(portName)); } if (string.IsNullOrEmpty(dllName)) { throw new ArgumentException("Contract assertion not met: !string.IsNullOrEmpty(dllName)", nameof(dllName)); } if (computerName == null) { var portMonPath = Path.Combine(Environment.SystemDirectory, dllName); if (!File.Exists(portMonPath)) { throw new FileNotFoundException($"Port dll does not exist at '{portMonPath}'", portMonPath); } } var info = new MONITOR_INFO_2 { pName = portName, pDLLName = dllName, pEnvironment = environment }; if (!AddMonitor(computerName, 2, ref info)) { throw new Win32Exception(); } return(new PortMonitor(computerName, info)); }
public GenericResult AddPrinterMonitor(string monitorName) { GenericResult retVal = new GenericResult("AddPrinterMonitor"); MONITOR_INFO_2 mi2 = new MONITOR_INFO_2(); mi2.pName = monitorName; mi2.pEnvironment = null; mi2.pDLLName = "mfilemon.dll"; try { if (AddMonitor(null, 2, ref mi2) == 0) { retVal.Exception = new Win32Exception(Marshal.GetLastWin32Error()); retVal.Message = retVal.Exception.Message; } } catch (Exception ex) { retVal.Exception = ex; retVal.Message = retVal.Exception.Message; } if (string.IsNullOrWhiteSpace(retVal.Message)) { retVal.Success = true; } return(retVal); }
public static IEnumerable <PortMonitor> GetPortMonitors(string computerName = null) { int cbNeeded, cReturned; if (EnumMonitors(computerName, 2, IntPtr.Zero, 0, out cbNeeded, out cReturned) || Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(); } IntPtr pBuffer = Marshal.AllocHGlobal(cbNeeded); try { if (!EnumMonitors(computerName, 2, pBuffer, cbNeeded, out cbNeeded, out cReturned)) { throw new Win32Exception(); } var results = new List <PortMonitor>(); for (int i = 0; i < cReturned; i++) { MONITOR_INFO_2 pm = Marshal.PtrToStructure <MONITOR_INFO_2>(pBuffer); results.Add(new PortMonitor(computerName, pm)); pBuffer = pBuffer + Marshal.SizeOf <MONITOR_INFO_2>(); } return(results); } finally { Marshal.FreeHGlobal(pBuffer); } }
public void AddMonitor(string monitorName, string environment, string dllName) { var mi2 = new MONITOR_INFO_2 { pName = monitorName, pEnvironment = environment, pDLLName = dllName }; if (AddMonitor(null, 2, ref mi2) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
// ReSharper restore InconsistentNaming public List <String> GetInstalledMonitors() { uint pcbNeeded = 0; uint pcReturned = 0; if (EnumMonitors(null, 2, IntPtr.Zero, 0, ref pcbNeeded, ref pcReturned)) { //succeeds, but must not, because buffer is zero (too small)! throw new Exception("EnumPorts should fail!"); } int lastWin32Error = Marshal.GetLastWin32Error(); //ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception if (lastWin32Error != 122) { throw new Win32Exception(lastWin32Error); } IntPtr pMonitors = Marshal.AllocHGlobal((int)pcbNeeded); if (EnumMonitors(null, 2, pMonitors, pcbNeeded, ref pcbNeeded, ref pcReturned)) { IntPtr currentMonitor = pMonitors; MONITOR_INFO_2[] minfo = new MONITOR_INFO_2[pcReturned]; for (int i = 0; i < pcReturned; i++) { minfo[i] = (MONITOR_INFO_2)Marshal.PtrToStructure(currentMonitor, typeof(MONITOR_INFO_2)); currentMonitor = (IntPtr)(currentMonitor.ToInt32() + Marshal.SizeOf(typeof(MONITOR_INFO_2))); } Marshal.FreeHGlobal(pMonitors); List <String> result = new List <string>(); for (int i = 0; i < pcReturned; i++) { result.Add(minfo[i].pName); } return(result); } throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static void Install() { MONITOR_INFO_2 mi2 = new MONITOR_INFO_2 { pName = DEFAULT_MONITOR_NAME, pEnvironment = null, pDLLName = DEFAULT_MONITOR_DLL }; try { if (!AddMonitor(null, 2, ref mi2)) { int code = Marshal.GetLastWin32Error(); if (code != ERROR_PRINT_MONITOR_ALREADY_INSTALLED) { throw new Win32Exception(code); } } } catch { throw; } }
public static extern bool AddMonitor(string portName, uint level, ref MONITOR_INFO_2 monitors);
private static extern int AddMonitor(string pName, uint Level, ref MONITOR_INFO_2 pMonitors);
private static extern Int32 AddMonitor(String pName, UInt32 Level, ref MONITOR_INFO_2 pMonitors);
public static extern Int32 AddMonitor(String pName, UInt32 level, ref MONITOR_INFO_2 pMonitors);
public void RemovePrinterMonitor(string monitorName) { var myMon = GetMonitors().SingleOrDefault(m => m.pName == monitorName); if (String.IsNullOrWhiteSpace(myMon.pName)) { _logHelper.Log("The monitor is not installed"); return; } MONITOR_INFO_2 mi2 = new MONITOR_INFO_2(); mi2.pName = monitorName; mi2.pEnvironment = null; mi2.pDLLName = "mfilemon.dll"; if (DeleteMonitor(null, null, monitorName) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
internal PortMonitor(string computerName, MONITOR_INFO_2 info) { this.computerName = computerName; this.info = info; }