How to get serial number from Mac hard disks?

I’m not sure if “AppleUSBEHCI” is the proper thing to look for but you can retrieve this sort of data using the IOKit framework:

#include <IOKit/IOKitLib.h>
#include <Cocoa/Cocoa.h>

kern_return_t   kr;
io_iterator_t   io_objects;
io_service_t    io_service;

kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
            IOServiceNameMatching("AppleUSBEHCI"), &io_objects);

if(kr != KERN_SUCCESS)
    exit(1);

while((io_service= IOIteratorNext(io_objects)))
{
    kr = IORegistryEntryCreateCFProperties(io_service, &service_properties, kCFAllocatorDefault, kNilOptions);
    if(kr == KERN_SUCCESS)
    {
        NSDictionary * m = (NSDictionary *)service_properties;
        NSLog(@"%@", m);
        CFRelease(service_properties);
    }

    io_iterator_t   iter;
    //handle kr error
    kr = IORegistryEntryGetChildIterator(io_service, kIOServicePlane, &iter);

    io_registry_entry_t     child;
    while( (child = IOIteratorNext( iter )))
    {
        kr = IORegistryEntryCreateCFProperties(child, &child_props,  kCFAllocatorDefault, kNilOptions );
        NSLog(@"Child props: %@", child_props);
        //release child_props
    }

    IOObjectRelease(io_service);
}

IOObjectRelease(io_objects);

Leave a Comment