Post by Stéphane SudreIs there a way to retrieve the name of the Mac in Mac OS X with Cocoa ?
What I call the name of the Mac is the one shown in the Login Window
which is the the one you set in the Share System Preferences panel.
I tried NSHost, unistd and gethostname but just get "localhost" as a
result (which is quite not surprising).
I've looked in the NSFoundation, NSApplication documentation, in the
headers, in the Omni various kits and found nothing.
It seems Carbon is the only way to go...
Currently there is no public API for this I filed a feature request
with RADAR until Apple release the API to the public you can use this
code that I wrote.
void GetComputerName(char* outString,UInt32 maxLen)
{
#define kSystemConfigurationPreferences
CFSTR("/var/db/SystemConfiguration/preferences.xml")
#define kComputerNamePropertry
CFSTR("ComputerName")
#define kComputerNameEncodingPropertry
CFSTR("ComputerNameEncoding")
outString[0]=0;
SInt32 errorCode=noErr;
CFStringRef theComputerName=NULL;
CFNumberRef theComputerNameEncoding=NULL;
CFURLRef theCFURLRef=NULL;
// need to be released
CFDataRef xmlData=NULL;
// need to be released
CFDictionaryRef thePropertyList=NULL;
// need to be released
CFDictionaryRef SystemDict=NULL;
CFDictionaryRef SystemSystemDict=NULL;
theCFURLRef=CFURLCreateWithFileSystemPath(kCFAllocatorDefault,kSystemConfigurationPreferences,kCFURLPOSIXPathStyle,false);
if (theCFURLRef==NULL) goto FailError;
if
(!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,theCFURLRef,&xmlData,NULL,NULL,&errorCode)
|| errorCode!=noErr) goto FailError;
thePropertyList=(CFDictionaryRef)CFPropertyListCreateFromXMLData(kCFAllocatorDefault,xmlData,kCFPropertyListImmutable,NULL);
if (thePropertyList==NULL) goto FailError;
if
(CFDictionaryGetValueIfPresent(CFDictionaryRef(thePropertyList),CFSTR("System"),(const
void **)&SystemDict))
{
if
(CFDictionaryGetValueIfPresent(SystemDict,CFSTR("System"),(const void
**)&SystemSystemDict))
{
CFStringEncoding theEncoding;
if
(CFDictionaryGetValueIfPresent(SystemSystemDict,kComputerNameEncodingPropertry,(const
void **)&theComputerNameEncoding) &&
CFDictionaryGetValueIfPresent(SystemSystemDict,kComputerNamePropertry,(const
void **)&theComputerName))
{
if
(CFNumberGetValue(theComputerNameEncoding,kCFNumberSInt32Type,&theEncoding))
CFStringGetCString(theComputerName,outString,maxLen,theEncoding);
}
}
}
FailError:
if (theCFURLRef) CFRelease(theCFURLRef);
if (xmlData) CFRelease(xmlData);
if (thePropertyList) CFRelease(thePropertyList);
}
Hope this helps,
Martin
--
_________________________________________________________________________
Martin Bestmann Netopia Development GmbH
Phone: +49-9134-9942-0 Weingasse 26
Fax: +49-9134-997911 91077 Neunkirchen am Brand
e-mail:***@no.netopia.com Germany