How to Get the Serial Number of an Installed USB Printer?

AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

I can get a list of installed PMPrinters from PMServer.CreatePrinterList() which provides some information about the printers (not the serial number) as documented here,

https://developer.apple.com/documentation/applicationservices/1459606-pmprintergetid?language=objc

And I can enumerate USB devices using the IOKit's IOServiceGetMatchingServices (0, IOServiceMatching ("IOUSBDevice"), ref iter) which provides serial numbers.

But I cannot find a single parameter in either a PMPrinter nor an AppleUSBHostResources service that I can use to match the two objects.

Can anybody help me here?

Thanks

Answers

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    I am uncertain, as I am not completely knowledgeable in IOKit, but I would start with this code (obj-c):

    io_iterator_t iterator;
    IOServiceGetMatchingServices (kIOMasterPortDefault, IOServiceMatching("IOUSBDevice"), &iterator);
    
    io_service_t    object;
    while ((object = IOIteratorNext(iterator)))
    {
        CFTypeRef props = IORegistryEntryCreateCFProperty (object, CFSTR(kIORegistryEntryPropertyKeysKey), NULL, 0);
        CFTypeRef r = IORegistryEntryCreateCFProperty (object, CFSTR("idProduct"), NULL, 0);
        if (r != NULL)
        {
            NSLog (@"%@", r);
            CFRelease(r);
        }
    }
    

    The props list lists all of the items you can query off a given item. Hopefully you can find a general one (name?) to match up with what PMPrinter gives you.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Thanks Chris. Here's what's available via the IOKit.

    No name :/ . But what I did notice is that the 'USB Vendor name + "_" + USB Product Name' is the same as that which is returned from this Core Printing API call, CFStringRef PMPrinterGetID(PMPrinter printer).

    I'm going to work with this assumption for now. And I'm pretty sure all my logic will break if a user happens to have two of the exact same USB printers installed on their system. Hopefully, there won't be many of those kinds of users.

Sign In or Register to comment.