Discussion:
How can I get the name of the computer ?
Stéphane Sudre
2001-04-20 11:38:49 UTC
Permalink
Is 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...
Martin Bestmann
2001-04-21 07:40:39 UTC
Permalink
Post by Stéphane Sudre
Is 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
Marcel Bresink
2001-04-23 06:34:47 UTC
Permalink
Post by Stéphane Sudre
Is there a way to retrieve the name of the Mac in Mac OS X with Cocoa ?
The easiest way would be

[[NSProcessInfo processInfo] hostName]

Works for me. If you still get "localhost", you have a name service
configuration error at the Darwin level.

Marcel

--
Dr. Marcel Bresink, Ringstr. 21, 56630 Kretz, Germany
Fon: +49-2632-953150 Fax: -953151 http://www.bresink.de/
Stéphane Sudre
2001-04-23 07:29:55 UTC
Permalink
Post by Marcel Bresink
Post by Stéphane Sudre
Is there a way to retrieve the name of the Mac in Mac OS X with
Cocoa ?
The easiest way would be
[[NSProcessInfo processInfo] hostName]
Works for me. If you still get "localhost", you have a name service
configuration error at the Darwin level.
Hum, that was looking promising, but I got "localhost". Since I didn't
change anything to the Darwin stuff and I am just using it as the basic
users, I believe that I will try the CFstuff.

--
Stephane
Kurt Revis
2001-04-23 07:40:57 UTC
Permalink
Post by Martin Bestmann
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.
[a whole ton of CoreFoundation-based code]
Or, you could save yourself a lot of trouble and just do it the
Foundation way:

[[NSDictionary
dictionaryWithContentsOfFile:@"/var/db/SystemConfiguration/preferences.xml"
] valueForKeyPath:@"System.System.ComputerName"];
Rosyna
2001-04-23 07:57:40 UTC
Permalink
Wouldn't this assume the user has changed the computer name in the Sharing CP?
(or pref panel, whatever they call it in OS X.)

I doubt most users will change it, as most will not go to the Sharing
CP let alone read the bottom of it. Users don't read the bottom of
things.
Post by Kurt Revis
Or, you could save yourself a lot of trouble and just do it the
[[NSDictionary
--
Sincerely,
Rosyna Keller
Technical Support/Holy Knight/Always needs a hug

Unsanity: Unsane Tools for Insane People
Martin Bestmann
2001-05-03 09:06:07 UTC
Permalink
Post by Kurt Revis
[[NSDictionary
Is there also a non ObjC way (e.g. C/C++) of doing the above line in
one single statement?

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
Stéphane Sudre
2001-04-23 08:47:52 UTC
Permalink
Post by Rosyna
Wouldn't this assume the user has changed the computer name in the
Sharing CP?
(or pref panel, whatever they call it in OS X.)
I doubt most users will change it, as most will not go to the Sharing
CP let alone read the bottom of it. Users don't read the bottom of
things.
When the User enter the main user name within the SetUp Assistant, the
name of the Machine is set to John Doe's Computer. So there is always
one and if it's empty it doesn't annoy me.
Marcel Bresink
2001-04-23 09:06:44 UTC
Permalink
Post by Stéphane Sudre
Post by Marcel Bresink
[[NSProcessInfo processInfo] hostName]
Works for me. If you still get "localhost", you have a name service
configuration error at the Darwin level.
Hum, that was looking promising, but I got "localhost". Since I didn't
change anything to the Darwin stuff and I am just using it as the basic
users, I believe that I will try the CFstuff.
Maybe you get a different result then, but this is not a problem
of Cocoa vs. Carbon but a bug in the "Network Location" feature
of OSX. The typical problem is to have the correct host name
displayed in the login panel, but to have "localhost" displayed
in the "Basic" screen saver. This should work if you set the correct
host name in /etc/hostconfig.

Marcel

--
Dr. Marcel Bresink, Ringstr. 21, 56630 Kretz, Germany
Fon: +49-2632-953150 Fax: -953151 http://www.bresink.de/
Uli Zappe
2001-04-24 03:25:40 UTC
Permalink
Post by Marcel Bresink
Maybe you get a different result then, but this is not a problem
of Cocoa vs. Carbon but a bug in the "Network Location" feature
of OSX.
According to Eric Peyton, this is not a bug, but a design decision. :-(

This whole naming stuff really annoys me. Why do I have to set the
names two times, one time via GUI, and one time by editing a text
file, to get consistency?

What's more, do you know where to store the appropriate domain
entry? So far, I couldn't find a way except entering the FQDN as
hostname in /etc/hostconfig - but then I have a new version of the
"screen saver inconsistency" (the screen saver will display the
FQDN, and not just the hostname). OTOH, if I only enter the
hostname in /etc/hostconfig, sendmail complains about not being
able to find the domain... Sigh.

Bye
Uli
________________________________________________________

Uli Zappe ***@ritual.org
Lorscher Straße 5 http://www.ritual.org
D-60489 Frankfurt Fon: +49-700-ULIZAPPE
Germany Fax: +49-700-ZAPPEFAX
________________________________________________________
Marcel Bresink
2001-04-24 06:56:10 UTC
Permalink
What's more, do you know where to store the appropriate domain entry?
So far, I couldn't find a way except entering the FQDN as hostname in
/etc/hostconfig - but then I have a new version of the "screen saver
inconsistency" (the screen saver will display the FQDN, and not just
the hostname). OTOH, if I only enter the hostname in /etc/hostconfig,
sendmail complains about not being able to find the domain... Sigh.
I think that's just a problem of name resolution. If you
have a DNS or NetInfo server resolving names, an entry
in the "Domain Names" field of the Network pane in System
Preferences should be sufficient.
I don't know what will happen on a standalone computer
though....

Marcel

--
Dr. Marcel Bresink, Ringstr. 21, 56630 Kretz, Germany
Fon: +49-2632-953150 Fax: -953151 http://www.bresink.de/
Uli Zappe
2001-04-24 15:12:58 UTC
Permalink
Post by Marcel Bresink
Post by Uli Zappe
What's more, do you know where to store the appropriate domain
entry? So far, I couldn't find a way except entering the FQDN as
hostname in /etc/hostconfig - but then I have a new version of
the "screen saver inconsistency" (the screen saver will display
the FQDN, and not just the hostname). OTOH, if I only enter the
hostname in /etc/hostconfig, sendmail complains about not being
able to find the domain... Sigh.
I think that's just a problem of name resolution. If you
have a DNS or NetInfo server resolving names, an entry
in the "Domain Names" field of the Network pane in System
Preferences should be sufficient.
No, it's not. This entry sets just the "search" entry in
/etc/resolv.conf, not the "domain" entry (don't ask me why...), and
since resolv.conf is dynamically created at boot time, there's no
way to add a "domain" entry to it. :-( NetInfo's
/locations/resolver directory, OTOH, is gone...

Bye
Uli
________________________________________________________

Uli Zappe ***@ritual.org
Lorscher Straße 5 http://www.ritual.org
D-60489 Frankfurt Fon: +49-700-ULIZAPPE
Germany Fax: +49-700-ZAPPEFAX
________________________________________________________
James Roberts
2001-04-24 15:25:24 UTC
Permalink
Acutally, what's in the Network Location thing I believe is the Appletalk
name. /etc/hostconfig is the unix style hostname. The hostname is limited
in the same way that DNS names are limited (no spaces, commas, apostrophes,
etc...). Appletalk doesn't have this limitation.

James Roberts

-----Original Message-----
From: macosx-dev-***@omnigroup.com
[mailto:macosx-dev-***@omnigroup.com]On Behalf Of Marcel Bresink
Sent: Monday, April 23, 2001 3:16 AM
To: Stephane Sudre
Cc: macosx-***@omnigroup.com
Subject: Re: How can I get the name of the computer ?
Post by Stéphane Sudre
Post by Marcel Bresink
[[NSProcessInfo processInfo] hostName]
Works for me. If you still get "localhost", you have a name service
configuration error at the Darwin level.
Hum, that was looking promising, but I got "localhost". Since I didn't
change anything to the Darwin stuff and I am just using it as the basic
users, I believe that I will try the CFstuff.
Maybe you get a different result then, but this is not a problem
of Cocoa vs. Carbon but a bug in the "Network Location" feature
of OSX. The typical problem is to have the correct host name
displayed in the login panel, but to have "localhost" displayed
in the "Basic" screen saver. This should work if you set the correct
host name in /etc/hostconfig.

Marcel

--
Dr. Marcel Bresink, Ringstr. 21, 56630 Kretz, Germany
Fon: +49-2632-953150 Fax: -953151 http://www.bresink.de/
Jens Baumeister
2001-04-26 10:25:58 UTC
Permalink
Post by James Roberts
-----Original Message-----
Acutally, what's in the Network Location thing I believe is
the Appletalk
name. /etc/hostconfig is the unix style hostname. The
hostname is limited
in the same way that DNS names are limited (no spaces,
commas, apostrophes,
etc...). Appletalk doesn't have this limitation.
Is there any obvious way for the end-user to change the unix style hostname
via GUI?

If not, there should be one!

_____________________________________
Jens Baumeister
Bullex GmbH, Cologne, Germany

She sells c-shells by the sea shore.
Uli Zappe
2001-04-26 20:10:56 UTC
Permalink
Post by Jens Baumeister
Is there any obvious way for the end-user to change the unix style
hostname
via GUI?
No. :-(

The whole network related GUI is Apple-Talk centric so far, and
neglects Unix networking almost totally. I was surprised to see
Apple not only not hiding but actually advertising OS X's Unix
layer, but they should follow thru in the GUI...

Bye
Uli
________________________________________________________

Uli Zappe ***@ritual.org
Lorscher Straße 5 http://www.ritual.org
D-60489 Frankfurt Fon: +49-700-ULIZAPPE
Germany Fax: +49-700-ZAPPEFAX
________________________________________________________
Eric Peyton
2001-05-05 12:12:54 UTC
Permalink
Yes, look at the documentation on CFDictionary.

Eric
Post by Martin Bestmann
Post by Kurt Revis
[[NSDictionary
Is there also a non ObjC way (e.g. C/C++) of doing the above line
in one single statement?
Martin
--
_________________________________________________________________________
Martin Bestmann Netopia Development GmbH
Phone: +49-9134-9942-0 Weingasse 26
Fax: +49-9134-997911 91077 Neunkirchen
am Brand
_______________________________________________
MacOSX-dev mailing list
http://www.omnigroup.com/mailman/listinfo/macosx-dev
Chris Kane
2001-05-07 15:46:56 UTC
Permalink
... Though you may not be able to do it in a one-liner or it might be
more trouble than it's worth.

As a software engineer, I distrust the use of this particular one-liner,
because it is and looks very convenient, but is expensive. It is
tempting to copy/paste this line elsewhere (or several elsewheres) and
every time you're going to be loading and parsing the dictionary,
getting one value, and then throwing the whole thing away (via
autorelease). I've seen this sort of thing happen before (and run
across such code in the system software). Using multiple lines won't
make it look so cheap, and will make you realize later when you want
another key out of the dictionary where the best place to put that is.


Chris Kane
Cocoa Frameworks, Apple, Inc.
Post by Eric Peyton
Yes, look at the documentation on CFDictionary.
Eric
Post by Martin Bestmann
Post by Kurt Revis
[[NSDictionary
xml"
Is there also a non ObjC way (e.g. C/C++) of doing the above line in
one single statement?
Loading...