rzaidi
January 15, 2009, 7:03pm
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
jaydee
January 15, 2009, 10:29pm
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]
jules
January 16, 2009, 11:19am
3
Interesting…
FWIW my version is actually derived from official Apple code, so I’m surprised that people are having problems with it: