Skip to content

Instantly share code, notes, and snippets.

@scottmac
Created September 16, 2011 07:17
Show Gist options
  • Save scottmac/1221426 to your computer and use it in GitHub Desktop.
Save scottmac/1221426 to your computer and use it in GitHub Desktop.
Find serial number of USB device on OS X.
#include <stdio.h>
#include <stdlib.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/hid/IOHIDKeys.h>
CFStringRef find_serial(int idVendor, int idProduct) {
CFMutableDictionaryRef matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName);
CFNumberRef numberRef;
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor);
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID), numberRef);
CFRelease(numberRef);
numberRef = 0;
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct);
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID), numberRef);
CFRelease(numberRef);
numberRef = 0;
io_iterator_t iter = NULL;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iter) == KERN_SUCCESS) {
io_service_t usbDeviceRef;
if ((usbDeviceRef = IOIteratorNext(iter))) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(usbDeviceRef, &dict, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS) {
CFTypeRef obj = CFDictionaryGetValue(dict, CFSTR(kIOHIDSerialNumberKey));
if (!obj) {
obj = CFDictionaryGetValue(dict, CFSTR(kUSBSerialNumberString));
}
if (obj) {
return CFStringCreateCopy(kCFAllocatorDefault, (CFStringRef)obj);
}
}
}
}
return NULL;
}
int main(void) {
CFStringRef obj;
obj = find_serial(0x1c34, 0x7141); // serial mode
if (!obj) {
obj = find_serial(0x1c34, 0x7241); // keyboard mode
}
if (obj) {
char serial[256];
if (CFStringGetCString(obj, serial, 256, CFStringGetSystemEncoding())) {
printf("got serial: %s\n", serial);
}
} else {
printf("No matching USB devices found\n");
}
return 0;
}
@johnsonisjiang
Copy link

how can i get the income para [ idVendor \ idProduct ] ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment