Created
October 18, 2011 09:00
-
-
Save tmiz/1294978 to your computer and use it in GitHub Desktop.
Get System Serial Number for Mac / TN1103
This file contains hidden or 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
// | |
// USAGE: | |
// $ clang++ main.cpp -framework CoreFoundation -framework IOKit -o SystemSerial | |
// $ ./SystemSerial | |
// Serial Number(System): XXXXXXXXXXX | |
// | |
// Reference. | |
// http://developer.apple.com/library/mac/#technotes/tn1103/_index.html | |
// | |
// tmiz [email protected] | |
#include <CoreFoundation/CoreFoundation.h> | |
#include <IOKit/IOKitLib.h> | |
void GetSerialNumber(CFStringRef *serialNumber) { | |
if (serialNumber != NULL) { | |
*serialNumber = NULL; | |
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, | |
IOServiceMatching("IOPlatformExpertDevice")); | |
if (platformExpert) { | |
CFTypeRef serialNumberAsCFString = | |
IORegistryEntryCreateCFProperty(platformExpert, | |
CFSTR(kIOPlatformSerialNumberKey), | |
kCFAllocatorDefault, 0); | |
if (serialNumberAsCFString) { | |
*serialNumber = (CFStringRef)serialNumberAsCFString; | |
} | |
IOObjectRelease(platformExpert); | |
} | |
} | |
} | |
int main(int ac, char *av[]) { | |
CFStringRef strSerial; | |
GetSerialNumber(&strSerial); | |
CFIndex len = CFStringGetLength(strSerial); | |
CFIndex max = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8); | |
char *str = new char[max+1]; | |
memset(str, 0, max+1); | |
if (!CFStringGetCString(strSerial, str, max + 1, kCFStringEncodingUTF8)) { | |
printf("ERR:Cannot convert"); | |
} | |
printf("Serial Number(System): %s\n",str); | |
CFRelease(strSerial); | |
delete [] str; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment