Created
May 10, 2014 04:43
-
-
Save satoshimuraki/f811cfe509f9cdd75085 to your computer and use it in GitHub Desktop.
Getting Mac Serial Number and Hardware UUID
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
// | |
// main.m | |
// | |
// Copyright (c) 2014 Satoshi Muraki | |
// | |
// MIT License | |
// | |
#import <Foundation/Foundation.h> | |
#import <IOKit/IOKitLib.h> | |
NSString *getPlatformAttributeForKey(CFStringRef key) | |
{ | |
io_service_t service; | |
service = IOServiceGetMatchingService( | |
kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")); | |
if (!service) | |
{ | |
NSLog(@"error: couldn't get service\n"); | |
return nil; | |
} | |
NSString *result; | |
result = (__bridge_transfer NSString *)IORegistryEntryCreateCFProperty( | |
service, key, kCFAllocatorDefault, 0); | |
IOObjectRelease(service); | |
return result; | |
} | |
NSString *getMacSerialNumber(void) | |
{ | |
return getPlatformAttributeForKey(CFSTR(kIOPlatformSerialNumberKey)); | |
} | |
NSString *getMacHardwareUUID(void) | |
{ | |
return getPlatformAttributeForKey(CFSTR(kIOPlatformUUIDKey)); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool | |
{ | |
NSString *string; | |
string = getMacSerialNumber(); | |
if (string != nil) | |
{ | |
NSLog(@"serial number: %@\n", string); | |
} | |
else | |
{ | |
NSLog(@"error: couldn't get serial number.\n"); | |
} | |
string = getMacHardwareUUID(); | |
if (string != nil) | |
{ | |
NSLog(@"hardware UUID: %@\n", string); | |
} | |
else | |
{ | |
NSLog(@"error: couldn't get hardwared UUID.\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment