Skip to content

Instantly share code, notes, and snippets.

Created October 21, 2019 16:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#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,
if (kernResult == KERN_SUCCESS) {
if (macAddress) CFRelease(macAddress);
macAddress = (CFDataRef) IORegistryEntryCreateCFProperty(parentService,
CFSTR("IOMACAddress"), kCFAllocatorDefault, 0);
} else {
printf("IORegistryEntryGetParentEntry returned %d\n", kernResult);
return macAddress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment