Problems getting MAC address on Mac


#1

Does anybody use JUCE to obtain an Apple’s MAC address?

We seem to get a lot of blank Mac addresses…and, also, don’t seem to get all the mac addresses on a computer (eg. Ethernet, airport, bluetooth)…just a single one…

anybody use this JUCE code? any ideas?

regards, Raza


#2

I’ve had this old code kicking around which I used to use! I’m sure it could be improved on, but it seemed to work reliably (although looking at it now I dunno why there’s a loop over “allKeys”…

[code]

  • (NSString*) getAddressOfPrimaryInterface
    {
    NSString *hostName=@"";

    int i;
    SCDynamicStoreContext context = { 0, (void *)self, NULL, NULL, NULL };
    SCDynamicStoreRef dynStore = SCDynamicStoreCreate(
    NULL,
    (CFStringRef) [[NSBundle mainBundle] bundleIdentifier],
    nil,
    &context);
    NSArray * allKeys;

    NSString * primaryInterface;
    allKeys = [(NSArray *)SCDynamicStoreCopyKeyList(dynStore, CFSTR(“State:/Network/Global/IPv4”)) autorelease];
    for(i=0; i<[allKeys count]; i++)
    {
    NSDictionary * dict = [(NSDictionary *)
    SCDynamicStoreCopyValue(dynStore, (CFStringRef)[allKeys objectAtIndex:i]) autorelease];
    primaryInterface = (NSString *) [dict objectForKey:@“PrimaryInterface”];
    }
    allKeys = [(NSArray *)SCDynamicStoreCopyKeyList(dynStore,
    CFStringCreateWithFormat(kCFAllocatorDefault,
    NULL,
    CFSTR(“State:/Network/Interface/%@/IPv4”),
    primaryInterface)) autorelease];
    for(i=0; i<[allKeys count]; i++)
    {
    NSDictionary * dict = [(NSDictionary *)
    SCDynamicStoreCopyValue(dynStore, (CFStringRef)[allKeys objectAtIndex:i]) autorelease];
    hostName=[[dict objectForKey:@“Addresses”] objectAtIndex:0];
    }

    CFRelease(dynStore);
    return hostName;
    }[/code]


#3

Interesting…

FWIW my version is actually derived from official Apple code, so I’m surprised that people are having problems with it:
developer.apple.com/samplecode/GetPrimaryMacAddress/listing1.html