private void AddPort(string portName) { PORT_INFO_1 pInfo = new PORT_INFO_1 { szPortName = portName }; int nResult = AddPortEx(null, 1, ref pInfo, _portMonitor); if (nResult == 0) { nResult = Marshal.GetLastWin32Error(); if (nResult == 87) { // Returned if the port exists - but may be for other circumstances too // Double check that the port does, in fact, exist if (PortExists(portName)) { throw new XPSPrintException(string.Format("Port {0} already exists", portName)); } } throw new Win32Exception(nResult); } }
/// <summary> /// Get the list of port /// </summary> public static List <string> GetPortList() { List <string> alRet = new List <string>(); int pcReturned = 0; int pcbNeeded = 0; IntPtr outb = IntPtr.Zero; EnumPorts(null, 1, outb, 0, out pcbNeeded, out pcReturned); outb = Marshal.AllocHGlobal(pcbNeeded + 1); EnumPorts(null, 1, outb, pcbNeeded, out pcbNeeded, out pcReturned); PORT_INFO_1[] portsArray = new PORT_INFO_1[pcReturned]; IntPtr current = outb; for (int i = 0; i < pcReturned; i++) { portsArray[i] = (PORT_INFO_1)Marshal.PtrToStructure(current, typeof(PORT_INFO_1)); current = (IntPtr)((int)current + Marshal.SizeOf(typeof(PORT_INFO_1))); alRet.Add(portsArray[i].pName); } return(alRet); }
private string[] GetInstalledPorts() { uint pcbNeeded = 0; uint pcReturned = 0; if (EnumPorts(null, 1, IntPtr.Zero, 0, ref pcbNeeded, ref pcReturned)) { //succeeds, but must not, because buffer is zero (too small)! return(null); } int lastWin32Error = Marshal.GetLastWin32Error(); //ERROR_INSUFFICIENT_BUFFER expected, if not -> Exception if (lastWin32Error != ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(lastWin32Error); } IntPtr pPorts = Marshal.AllocHGlobal((int)pcbNeeded); if (EnumPorts(null, 1, pPorts, pcbNeeded, ref pcbNeeded, ref pcReturned)) { IntPtr currentPort = pPorts; string[] sPorts = new string[pcReturned]; for (int i = 0; i < pcReturned; i++) { PORT_INFO_1 pinfo = (PORT_INFO_1)Marshal.PtrToStructure(currentPort, typeof(PORT_INFO_1)); sPorts[i] = pinfo.szPortName; currentPort = (IntPtr)(currentPort.ToInt32() + Marshal.SizeOf(typeof(PORT_INFO_1))); } Marshal.FreeHGlobal(pPorts); return(sPorts); } throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static extern int AddPortEx(string pName, int pLevel, ref PORT_INFO_1 lpBuffer, string pMonitorName);
/// <summary> /// Get the list of port /// </summary> public static List<string> GetPortList() { List<string> alRet = new List<string>(); int pcReturned = 0; int pcbNeeded = 0; IntPtr outb = IntPtr.Zero; EnumPorts(null, 1, outb, 0, out pcbNeeded, out pcReturned); outb = Marshal.AllocHGlobal(pcbNeeded + 1); EnumPorts(null, 1, outb, pcbNeeded, out pcbNeeded, out pcReturned); PORT_INFO_1[] portsArray = new PORT_INFO_1[pcReturned]; IntPtr current = outb; for (int i = 0; i < pcReturned; i++) { portsArray[i] = (PORT_INFO_1)Marshal.PtrToStructure(current, typeof(PORT_INFO_1)); current = (IntPtr)((int)current + Marshal.SizeOf(typeof(PORT_INFO_1))); alRet.Add(portsArray[i].pName); } return alRet; }