Obtain computer name and user name

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Obtain computer name and user name

Post by Shardik »

Code: Select all

ImportC ""
  CFRelease(CFTypeRef.L)
  CFStringGetCString(CFStringRef.L, *Buffer, BufferSize, Encoding.L)
  CSCopyMachineName()
  CSCopyUserName(UseShortName.L)
EndImport

Procedure.S AddNameToInfo(NameRef.L, Description.S)
  Shared Info.S
  Protected Name.S = Space(100)

  If NameRef
    If CFStringGetCString(NameRef, @Name, Len(Name), 0)
      Info + #CR$ + Description + " = " + Name
    EndIf
    CFRelease(NameRef)
  EndIf
EndProcedure

Define Info.S

AddNameToInfo(CSCopyMachineName(), "Machine name")
AddNameToInfo(CSCopyUserName(#True), "User's short name")
AddNameToInfo(CSCopyUserName(#False), "User's long name")

MessageRequester("System Info", Info)
A much easier way to obtain the computer's name would be the PureBasic
function Hostname() (and therefore even cross-platform):

Code: Select all

InitNetwork()
Debug Hostname()
But it doesn't display the correct name of my iMac because it attaches the
name of my DSL router:
Debug Hostname() wrote:shardiks-imac.fritz.box
whereas my first API code example displays the correct name
Shardiks-iMac
Update: freak clarified that the host name is not necessarily identical to the computer name:
http://www.purebasic.fr/english/viewtop ... 52&start=1
Golfy
User
User
Posts: 97
Joined: Wed Mar 21, 2012 6:10 pm

Re: Obtain computer name and user name

Post by Golfy »

I can't access to the post specified upper... any idea why?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Obtain computer name and user name

Post by Shardik »

Golfy wrote:I can't access to the post specified upper... any idea why?
The thread linked in my first post to freak's explanation was
presumably moved to an area that cannot be accessed anymore
by normal board users because it was originally posted in the
MacOS bugs subforum. All corrected bug reports or falsely
reported errors (as in this case) are moved after some time to
an inaccessible board area... :wink:
Post Reply