Re: UInt8 > NSString
Re: UInt8 > NSString
- Subject: Re: UInt8 > NSString
- From: arri <email@hidden>
- Date: Tue, 6 Mar 2007 16:01:21 +0100
On Mar 6, 2007, at 05:18 27, Bob Smith wrote:
..Hmmm, maybe, I would have to see your code to be sure.
Briefly, here is the thread-safe approach:
- (NSString *)makeTheString
{
char theBytes[10];
(do something to fill in theBytes)
NSString *theString = [NSString stringWithCString:theBytes];
return(theString);
}
Safe, because theBytes[] is dynamically allocated each time the
method is called; which is why you were having your original
problem, but now it's a good thing because it means each thread
will have it's own memory.
ok, then this should indeed be threadsafe, since 'MACAddress' is
being allocated in the -mainMacAddress method.
thanks everyone for your input!
gr
arri
static kern_return_t FindEthernetInterfaces(io_iterator_t
*matchingServices)
{
kern_return_t kernResult;
CFMutableDictionaryRef matchingDict;
CFMutableDictionaryRef propertyMatchDict;
matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);
if (NULL == matchingDict) {
printf("IOServiceMatching returned a NULL dictionary.\n");
} else {
propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (NULL == propertyMatchDict) {
printf("CFDictionaryCreateMutable returned a NULL dictionary.\n");
} else {
CFDictionarySetValue(propertyMatchDict, CFSTR
(kIOPrimaryInterface), kCFBooleanTrue);
CFDictionarySetValue(matchingDict, CFSTR(kIOPropertyMatchKey),
propertyMatchDict);
CFRelease(propertyMatchDict);
}
}
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
matchingDict, matchingServices);
if (KERN_SUCCESS != kernResult) {
printf("IOServiceGetMatchingServices returned 0xx\n", kernResult);
}
return kernResult;
}
static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8
*MACAddress, UInt8 bufferSize)
{
io_object_t intfService;
io_object_t controllerService;
kern_return_t kernResult = KERN_FAILURE;
if (bufferSize < kIOEthernetAddressSize) {
return kernResult;
}
bzero(MACAddress, bufferSize);
while (intfService = IOIteratorNext(intfIterator)) {
CFTypeRef MACAddressAsCFData;
kernResult = IORegistryEntryGetParentEntry(intfService,
kIOServicePlane,
&controllerService);
if (KERN_SUCCESS != kernResult) {
printf("IORegistryEntryGetParentEntry returned 0xx\n",
kernResult);
}
else {
MACAddressAsCFData = IORegistryEntryCreateCFProperty
(controllerService,
CFSTR(kIOMACAddress),
kCFAllocatorDefault,
0);
if (MACAddressAsCFData) {CFDataGetBytes(MACAddressAsCFData,
CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
CFRelease(MACAddressAsCFData);
}
(void) IOObjectRelease(controllerService);
}
(void) IOObjectRelease(intfService);
}
return kernResult;
}
@implementation TheClass
.
.
.
- (NSString*)mainMacAddress {
kern_return_t kernResult = KERN_SUCCESS;
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
NSString *addressString = nil;
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult) {
addressString =[NSString stringWithString:@"000000000000"];
} else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof
(MACAddress));
if (KERN_SUCCESS != kernResult) {
addressString =[NSString stringWithString:@"000000000000"];
} else {
addressString =[NSString stringWithFormat:@"xxxxx%
02x",
MACAddress[0], MACAddress[1], MACAddress[2],MACAddress[3],
MACAddress[4], MACAddress[5]];
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
return addressString;
}
.
.
.
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden