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
#import <IOKit/IOKitLib.h> | |
#import <Foundation/Foundation.h> | |
// Returns a CFData object, containing the computer's GUID. | |
CFDataRef copy_mac_address(void) | |
{ | |
kern_return_t kernResult; | |
mach_port_t master_port; | |
CFMutableDictionaryRef matchingDict; | |
io_iterator_t iterator; | |
io_object_t service; | |
CFDataRef macAddress = nil; | |
kernResult = IOMasterPort(MACH_PORT_NULL, &master_port); | |
if (kernResult != KERN_SUCCESS) { | |
printf("IOMasterPort returned %d\n", kernResult); | |
return nil; | |
} | |
matchingDict = IOBSDNameMatching(master_port, 0, "en0"); | |
if (!matchingDict) { | |
printf("IOBSDNameMatching returned empty dictionary\n"); | |
return nil; | |
} | |
kernResult = IOServiceGetMatchingServices(master_port, matchingDict, &iterator); | |
if (kernResult != KERN_SUCCESS) { | |
printf("IOServiceGetMatchingServices returned %d\n", kernResult); | |
return nil; | |
} | |
while((service = IOIteratorNext(iterator)) != 0) { | |
io_object_t parentService; | |
kernResult = IORegistryEntryGetParentEntry(service, kIOServicePlane, | |
&parentService); | |
if (kernResult == KERN_SUCCESS) { | |
if (macAddress) CFRelease(macAddress); | |
macAddress = (CFDataRef) IORegistryEntryCreateCFProperty(parentService, | |
CFSTR("IOMACAddress"), kCFAllocatorDefault, 0); | |
IOObjectRelease(parentService); | |
} else { | |
printf("IORegistryEntryGetParentEntry returned %d\n", kernResult); | |
} | |
IOObjectRelease(service); | |
} | |
IOObjectRelease(iterator); | |
return macAddress; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment