USB printing

A couple of articles i've been meaning to highlight for a while, were a few posts i came across whilst looking to print via USB.

Obviously i came across this post on stack overflow initially - figuring which printer name corresponds to which device id. Which is what i set out to do as i wanted to use CreateFile, WriteFile, to send to data to a USB printer.

The article gives a few hints about what to do (searching through the registry, and some C# examples of the SetupAPI).

Further searching gave me this example of the SetupAPI usage in C++. However it again only gives us some hints on achieving our goal.

It is only with this article i found on codeproject that i discovered how to enumerate installed devices and get what i actually needed.

Ive recently looked at a different approach to this, as there may no longer be a printer associated with the USB port. The previous linked articles gave us code to get a list of usb device paths, so from this we can go and look in the registry for info we are after. Here is some pseudo code to get details of the usb port from the device path

// we would normally have something like this as our device path
std::tstring tsUSBDevPath = _T("\\\\?\\usb#vid_0412&pid_051c&mi_00#7&1a4c9a3f&0&0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}");

//to build registry path
std::tstring tsRegDevPath = tsUSBDevPath;
//replace \ with #
Replace(tsRegDevPath, _T(""), _T("#"));
//now have ##?#USB#vid_0412&PID_051C&MI_00#7&1a4c9a3f&1&0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}

int iPos = tsRegDevPath.rfind(_T("{"));
if(iPos == -1) {return false;}

std::tstring tsGUID = tsRegDevPath.substr(iPos);

std::tstring tsRegPath = _T("SYSTEM\\CurrentControlSet\\Control\\DeviceClasses");
tsRegPath += tsGUID + _T("") + tsRegDevPath + _T("\\#\\Device Parameters") ;

//Now if we open this reg key, we can look at the following values
// Port Number - can see what is assigned
// Port Description - printer name associated
// recyclable -  if printer deleted, and then cable pulled, an recycle key of type REG_NONE appears

Connect

Company

Printer Utils is Niris Computer Services's website for all interesting printer software development. © 2020 Niris Computer Services
Go to top