0
votes

I have application needs to print on 3 printers
i save the printers names in a txt file the problem is when the user disconnect and reconnect the printer ( usb printer ) it takes anew name or anther name and the printing get mixed up.

how i select the printer if the name changed ?

1
Can't you let the user tell you which printer to use - David Heffernan
no even the user don`t know the names of the printer - Amr El Morsy
The PRINTER shouldn't get a new name. The USB PORT may be re-allocated to a new port, but if you print using the printer's NAME then that shouldn't concern you. How does your actual code look for selection of printer to print to, and the actual printing you do? - HeartWare
Enumerate the printers and ask the user to pick one. Or show the printer setup dialog. Just as every other program does. - David Heffernan
@SirRufo: Which is why I ask for the actual code. It may be that the saving/restoring of printer name need to take into consideration that the printer name for this particular printer/driver re-installs itself when the USB port changes, and assigns a name like "MyCompany Printer on USB1". If so, the program needs to eliminate the " on USB#" from the saving and later scanning for the printer among the printer names stored in Printers string list. Like I said - I haven't seen this happen, but that doesn't mean that it CAN'T happen. But in order to help, we need to see the actual code... - HeartWare

1 Answers

1
votes

I have had the same issue with USB cameras. They also tend to just create another instance/entry for the device under windows with an appended number (example: Epson Blah Blah #1) as soon as the device is moved to another USB port.

The answer most probably lies in the Device Descriptors. More specifically the HardwareID of the device.

For a quick way to see a device's HardwareID go to Control Panel > Device Manager (if Win7 or above) then right-click on the device whether it be a USB Flashdrive or USB Camera. Click on properties and goto the "Details" tab. Change the "Property" value to "Hardware Ids". You will see the HardwareIDs

You can find more information on that here: https://msdn.microsoft.com/en-us/library/windows/hardware/ff539283%28v=vs.85%29.aspx

and:

https://msdn.microsoft.com/en-us/library/windows/hardware/jj649944%28v=vs.85%29.aspx

The latter talks about the registry keys where you can also find the information of a device. The key would be:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceClasses\ HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\\\Device Parameters HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

So if you can link HardwareIDs to a printer then you would be able to enumerate installed printers to compare with then select the one you want. So you will install a printer initially and let your application select that printer and save its HardwareID. Then everytime devices change on the PC you can just get the one that correlates to the initially installed printer.

I have not tested this as I am currently without two exactly same devices to make sure both don't have the same Hardware Ids.