Last active
January 2, 2016 09:09
-
-
Save hewigovens/8281597 to your computer and use it in GitHub Desktop.
Get internal disk serial number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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