0

In short: how do I get device path associated with USB POS printer? For instance \\?\usb#vid_5986&pid_0535#200901010001#{a5dcbf10-6530-11d2-901f-00c04fb951ed} if I know that printer's port is USB001 and I know it's name?

In full: I have this noname POS printer I need to print on. It comes with an SDK, a trivial DLL with functions like OpenPort("USB001"), SendData("hello world") and ClosePort(), which is OK for simple printing. It isn't OK if you need to reprogram the printer - SendData's null terminated argument prevents you from sending zeros to the printer, which is absolutely necessary for certain commands.

I spent some time researching if I could find some other way, didn't find anything substantial. Finally I found this article about Setupapi.lib and gave it a try: I wrote trivial program that enumerates all device paths, ran it with the printer on, ran it with the printer off, device path which disappeared I used as an argument to CreateFile, wrote data into opened handle and - hooray! - it worked. Printer did print the data I sent there. :)

And now I need more simple and elegant way to get this device path. I am still digging into Setupapi.lib reference, but maybe someone could give me a hint to speed up the process or suggest alternative route?

Thanks in advance.

Vasyl Demianov
  • 305
  • 1
  • 9
  • You already have working code, and now you are asking for a simpler solution? This doesn't appear to be specific enough a question to be on-topic for stackoverflow. – IInspectable Jan 22 '16 at 13:22
  • Code which requires human assistance in turning on/off a device and which also blindly believes that nothing else have changed... is it really working?.. – Vasyl Demianov Jan 22 '16 at 16:05
  • Eventually I found and followed this solution: http://stackoverflow.com/a/13533429/1525403 – Vasyl Demianov Jan 25 '16 at 16:11
  • 1
    Possible duplicate of [Figuring which printer name corresponds to which device ID](http://stackoverflow.com/questions/10651949/figuring-which-printer-name-corresponds-to-which-device-id) – Χpẘ Jan 26 '16 at 23:44
  • See the following article which contains sample code to obtaining the USB printer device path name. [Getting a handle on usbprint.sys](https://blog.peter.skarpetis.com/archives/2005/04/07/getting-a-handle-on-usbprintsys/). See also https://stackoverflow.com/questions/10651949/figuring-which-printer-name-corresponds-to-which-device-id as well as https://stackoverflow.com/questions/40775369/usb-serial-device-with-virtual-com-port-readfile-reads-zero-bytes-if-use-cre – Richard Chambers Oct 26 '21 at 04:14

1 Answers1

1

I googled for "setupapi get device path" and the first hit I got was the SetupDiGetDeviceInterfaceDetail api. Looking at that page you see it returns a SP_DEVICE_INTERFACE_DETAIL_DATA, which contains a device path. If you google the API you'll find a code example as well as several SO questions/answers on its use.

Χpẘ
  • 3,403
  • 1
  • 13
  • 22