Skip to content

Instantly share code, notes, and snippets.

@hewigovens
Last active January 2, 2016 09:09
Show Gist options
  • Save hewigovens/8281597 to your computer and use it in GitHub Desktop.
Save hewigovens/8281597 to your computer and use it in GitHub Desktop.
Get internal disk serial number
NSString* getInternalDiskSN()
{
NSString* result = @"";
CFMutableDictionaryRef matching;
matching = IOServiceMatching("IOAHCIBlockStorageDevice");
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
if (service) {
CFMutableDictionaryRef properties = NULL;
IORegistryEntryCreateCFProperties(service, &properties, NULL, kNilOptions);
CFDictionaryRef device_properties = CFDictionaryGetValue(properties, CFSTR("Device Characteristics"));
CFStringRef serial_number = CFDictionaryGetValue(device_properties, CFSTR("Serial Number"));
result = (__bridge NSString*)serial_number;
IOObjectRelease(service);
}
return [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString* serial_number = getInternalDiskSN();
NSLog(@"disk sn:%@", serial_number);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment