Page 1 of 1

Obtain computer name and user name

Posted: Thu Jun 16, 2011 8:15 pm
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

Re: Obtain computer name and user name

Posted: Wed Mar 21, 2012 6:16 pm
by Golfy
I can't access to the post specified upper... any idea why?

Re: Obtain computer name and user name

Posted: Wed Mar 21, 2012 9:58 pm
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: