Exemple #1
0
        /// <summary>
        /// Adds a new port with the given name
        /// </summary>
        /// <param name="portName">The name of the port to add</param>
        /// <exception cref="Win32Exception">Raised when an underlying call fails</exception>
        public void AddPort(string portName)
        {
            Precondition.NotNullOrEmpty(portName, "portName");

            SafeNativeMethods.PORT_INFO_1 pInfo = new SafeNativeMethods.PORT_INFO_1 {
                szPortName = portName
            };

            int result = SafeNativeMethods.AddPortEx(null, 1, ref pInfo, PortMonitor);

            if (result != 0)
            {
                Logger.InfoFormat("Port created with name of {0}", portName);
                return;
            }

            result = Marshal.GetLastWin32Error();
            if (result != 87)
            {
                throw new Win32Exception(result);
            }

            // Double check that the port does, in fact, exist
            if (this.PortExists(portName))
            {
                throw new VirtualPrinterException(string.Format("Port {0} already exists", portName));
            }

            throw new Win32Exception(result);
        }