本文介绍了创建打印机端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我需要有关在C#中以编程方式创建打印机端口的帮助.
这就是我拥有的

ManagementClass portClass = new ManagementClass("Win32_TCPIPPrinterPort");
ManagementObject portObject = portClass. CreateInstance();

portObject [名称"] = portName; PortNumber] = portNumber;
portObject [" Protocol] = 1; ;
portObject ["SNMPDevIndex"] = 1;

PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;

(选项);

这可以正常工作,但是会创建一个TCP/IP打印机端口.我需要创建一个重定向的打印机端口,该端口类型是RedMon创建的.
如何创建任何指定类型的端口?是否可以编写以下形式的方法:

void CreatePort(string portName,string portType)

创建一个名为portName并键入portType的端口,假设

Hi. I need help with creating a printer port programmatically in C#.
This is what I have

            ManagementClass portClass = new ManagementClass("Win32_TCPIPPrinterPort");
            ManagementObject portObject = portClass.CreateInstance();

            portObject["Name"] = portName;
            portObject["HostAddress"] = "174.30.164.15";
            portObject["PortNumber"] = portNumber;
            portObject["Protocol"] = 1;
            portObject["SNMPCommunity"] = "public";
            portObject["SNMPEnabled"] = true;
            portObject["SNMPDevIndex"] = 1;

            PutOptions options = new PutOptions();
            options.Type = PutType.UpdateOrCreate;
            portObject.Put(options);

This works fine, but it creates a TCP/IP printer port. I need to create a redirected printer port, a port type which is created by RedMon.
How do I create a port of any specified type? Is it possible to write a method of the form

void CreatePort(string portName,string portType)

which creates a port with the name portName and type portType, assuming that a port type with the name portType exists?

推荐答案

        private const int MAX_PORTNAME_LEN = 64;
        private const int MAX_NETWORKNAME_LEN = 49;
        private const int MAX_SNMP_COMMUNITY_STR_LEN = 33;
        private const int MAX_QUEUENAME_LEN = 33;
        private const int MAX_IPADDR_STR_LEN = 16;
        private const int RESERVED_BYTE_ARRAY_SIZE = 540;

        private enum PrinterAccess
        {
            ServerAdmin=0x01,
            ServerEnum=0x02,
            PrinterAdmin=0x04,
            PrinterUse=0x08,
            JobAdmin=0x10,
            JobRead=0x20,
            StandardRightsRequired=0x000f0000,
            PrinterAllAccess=(StandardRightsRequired | PrinterAdmin | PrinterUse)
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct PrinterDefaults
        {
            public IntPtr pDataType;
            public IntPtr pDevMode;
            public PrinterAccess DesiredAccess;
        }

        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
        private struct PortData
        {
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=MAX_PORTNAME_LEN)]
            public string sztPortName;
            public UInt32 dwVersion;
            public UInt32 dwProtocol;
            public UInt32 cbSize;
            public UInt32 dwReserved;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=MAX_NETWORKNAME_LEN)]
            public string sztHostAddress;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=MAX_SNMP_COMMUNITY_STR_LEN)]
            public string sztSNMPCommunity;
            public UInt32 dwDoubleSpool;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=MAX_QUEUENAME_LEN)]
            public string sztQueue;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=MAX_IPADDR_STR_LEN)]
            public string sztIPAddress;
            [MarshalAs(UnmanagedType.ByValArray,SizeConst=RESERVED_BYTE_ARRAY_SIZE)]
            public byte[] Reserved;
            public UInt32 dwPortNumber;
            public UInt32 dwSNMPEnabled;
            public UInt32 dwSNMPDevIndex;
        }

这是我们需要的Windows API函数的声明:

And here are the declarations of the Windows API functions that we'll need:

        [DllImport("winspool.drv")]
        private static extern bool OpenPrinter(string printerName,out IntPtr phPrinter,ref PrinterDefaults printerDefaults);

        [DllImport("winspool.drv")]
        private static extern bool ClosePrinter(IntPtr phPrinter);

        [DllImport("winspool.drv",CharSet=CharSet.Unicode)]
        private static extern bool XcvDataW(IntPtr hXcv,string pszDataName,IntPtr pInputData,UInt32 cbInputData,out IntPtr pOutputData,UInt32 cbOutputData,out UInt32 pcbOutputNeeded,out UInt32 pdwStatus);


这是添加监视器端口的功能:


And here is the function which adds the monitor port:

         /**
         * <summary>Adds a monitor printer port with the given name and type.</summary>
         * <param name="portName">The name of the new port (must end with ':').</param>
         * <param name="portType">The type of the new port (ex. "Redirected Port").</param>
         */
        public static void AddMonitorPrinterPort(string portName,string portType)
        {
            IntPtr printerHandle;
            PrinterDefaults defaults = new PrinterDefaults { DesiredAccess = PrinterAccess.ServerAdmin };
            if (! OpenPrinter(",XcvMonitor "+portType,out printerHandle,ref defaults))
                throw new Exception("Could not open printer for the monitor port "+portType+"!");
            try
            {
                PortData portData = new PortData
                {
                    dwVersion = 1,
                    dwProtocol = 1, // 1 = RAW, 2 = LPR
                    dwPortNumber = 9100, // 9100 = default port for RAW, 515 for LPR
                    dwReserved = 0,
                    sztPortName = portName,
                    sztIPAddress = "172.30.164.15",
                    sztSNMPCommunity = "public",
                    dwSNMPEnabled = 1,
                    dwSNMPDevIndex = 1
                };
                uint size = (uint) Marshal.SizeOf(portData);
                portData.cbSize = size;
                IntPtr pointer = Marshal.AllocHGlobal((int) size);
                Marshal.StructureToPtr(portData,pointer,true);
                IntPtr outputData;
                UInt32 outputNeeded,status;
                try
                {
                    if (! XcvDataW(printerHandle,"AddPort",pointer,size,out outputData,0,out outputNeeded,out status))
                        throw new PrinterPortException("Could not add port (error code "+status+")!");
                }
                finally
                {
                    Marshal.FreeHGlobal(pointer);
                }
            }
            finally
            {
                ClosePrinter(printerHandle);
            }
        }



这篇关于创建打印机端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 23:30