Figuring which printer name corresponds to which device ID

前端 未结 5 1241
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 04:59

My goal is to open a printer connected via USB using the CreateFile (and then issue some WriteFiles and ReadFiles).

If the pri

5条回答
  •  既然无缘
    2020-12-10 05:25

    Try this ... let me know if this helps ...

        static void Main(string[] args)
        {
            ManagementObjectSearcher s = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity");
            foreach (ManagementObject device in s.Get())
            {
                // Try Name, Caption and/or Description (they seem to be same most of the time).
                string Name = (string)device.GetPropertyValue("Name");
    
                // >>>>>>>>>>>>>>>>>>>> Query String ...
                if (Name == "O2Micro Integrated MMC/SD controller")
                {
                    /*
                     class Win32_PnPEntity : CIM_LogicalDevice
                    {
                      uint16   Availability;
                      string   Caption;
                      string   ClassGuid;
                      string   CompatibleID[];
                      uint32   ConfigManagerErrorCode;
                      boolean  ConfigManagerUserConfig;
                      string   CreationClassName;
                      string   Description;
                      string   DeviceID;
                      boolean  ErrorCleared;
                      string   ErrorDescription;
                      string   HardwareID[];
                      datetime InstallDate;
                      uint32   LastErrorCode;
                      string   Manufacturer;
                      string   Name;
                      string   PNPDeviceID;
                      uint16   PowerManagementCapabilities[];
                      boolean  PowerManagementSupported;
                      string   Service;
                      string   Status;
                      uint16   StatusInfo;
                      string   SystemCreationClassName;
                      string   SystemName;
                    };
                    */
    
                    try
                    {
                        Console.WriteLine("Name         : {0}", Name);
                        Console.WriteLine("DeviceID     : {0}", device.GetPropertyValue("DeviceID"));
                        Console.WriteLine("PNPDeviceID  : {0}", device.GetPropertyValue("PNPDeviceID"));
                        Console.WriteLine("ClassGuid    : {0}", device.GetPropertyValue("ClassGuid"));
                        Console.WriteLine("HardwareID   :\n{0}", JoinStrings(device.GetPropertyValue("HardwareID") as string[]));
                        Console.WriteLine("CompatibleID :\n{0}", JoinStrings(device.GetPropertyValue("CompatibleID") as string[]));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ERROR: {0}", e.Message);
                    }
                }
            }
        }
    
        static string JoinStrings(string[] sarray)
        {
            StringBuilder b = new StringBuilder();
            if (sarray != null)
            {
                foreach (string s in sarray)
                    b.Append("        '" + s + "'\n");
            }
            return b.ToString();
        }
    

    Don't have a USB printer to test against, but this provides the information you are looking for (including for USB devices)...

    Description  : O2Micro Integrated MMC/SD controller
    DeviceID     : PCI\VEN_1217&DEV_8221&SUBSYS_04931028&REV_05\4&26B31A7F&0&00E5
    PNPDeviceID  : PCI\VEN_1217&DEV_8221&SUBSYS_04931028&REV_05\4&26B31A7F&0&00E5
    ClassGuid    : {4d36e97b-e325-11ce-bfc1-08002be10318}
    HardwareID   :
            'PCI\VEN_1217&DEV_8221&SUBSYS_04931028&REV_05'
            'PCI\VEN_1217&DEV_8221&SUBSYS_04931028'
            'PCI\VEN_1217&DEV_8221&CC_080501'
            'PCI\VEN_1217&DEV_8221&CC_0805'
    
    CompatibleID :         'PCI\VEN_1217&DEV_8221&REV_05'
            'PCI\VEN_1217&DEV_8221'
            'PCI\VEN_1217&CC_080501'
            'PCI\VEN_1217&CC_0805'
            'PCI\VEN_1217'
            'PCI\CC_080501'
            'PCI\CC_0805'
    

    Also, for a URI, change the '\'s to '#'s in the URI you are intending of building.

    so ..

    usb\vid_0a5f&pid_0027\46a072900549\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}
    

    becomes

    usb#vid_0a5f&pid_0027#46a072900549#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}
    

    ====

    As GSerg pointed out that Win32_Printer Class helps with the above code, but doesn't provide the device id.

    But if I use Win32_Printer class and print out the "PortName" property, that, for the printers I have installed gives be a port/filename that I can use with CreateFile() and open the device.

    e.g.:

    Name         : Microsoft XPS Document Writer
    Description  :
    DeviceID     : Microsoft XPS Document Writer
    PNPDeviceID  :
    PortName  : XPSPort:
    
    
    Name         : Fax
    Description  :
    DeviceID     : Fax
    PNPDeviceID  :
    PortName  : SHRFAX:
    

    Here, writing to "XPSPORT:" or "SHRFAX:" sends data to the printer. What does this do for your USB printer?

提交回复
热议问题