Page 1 of 1

Get HostName (like the console command 'hostname')

Posted: Wed Jun 04, 2003 2:20 pm
by gnozal
Updated for version 5.20

Code: Select all

; 
; Get HostName (like the console command 'hostname')
;
; ------------------------
; STRUCTURE WKSTA_INFO_100
;
; The WKSTA_INFO_100 Structure contains information about a workstation environment,
; including platform-specific information, the names of the domain And the local 
; computer, And information concerning the operating system.
;
; wki100_platform_id
; Specifies the information level To use To retrieve platform-specific information.
; This member can be one of the following values, defined in the lmcons.h file:
; PLATFORM_ID_DOS, PLATFORM_ID_OS2, PLATFORM_ID_NT, PLATFORM_ID_OSF, Or PLATFORM_ID_VMS.
; wki100_computername
; Pointer To a Unicode string specifying the name of the local computer.
; wki100_langroup
; Pointer To a Unicode string specifying the name of the domain To which the computer
; belongs.
; wki100_ver_major
; Specifies the major version number of the operating system running on the computer.
; wki100_ver_minor
; Specifies the minor version number of the operating system running on the computer.
;
Structure WKSTA_INFO_100
    wki100_platform_id.l
    wki100_computername.l
    wki100_langroup.l
    wki100_ver_major.l
    wki100_ver_minor.l
EndStructure
; ------------------------
Procedure.s GetHostName()
 WorkstationEnv.WKSTA_INFO_100
 WorkstationConfigElements.l
 WKSTASize = SizeOf(WKSTA_INFO_100)
 ; The NetWkstaGetInfo function returns information about the configuration elements for a workstation.
 Result = NetWkstaGetInfo_(0, 100, @WorkstationConfigElements)
 If Result = 0
  CMResult = CopyMemory(WorkstationConfigElements, @WorkstationEnv, WKSTASize)
  Buffer.s = Space(512)
  ; The WideCharToMultiByte function maps a wide-character string to a new character string. 
  ; CP_ACP = ANSI code page
  Result = WideCharToMultiByte_(#CP_ACP ,0,WorkstationEnv\wki100_computername,-1,@Buffer.s,512,0,0)
  ProcedureReturn Trim(Buffer)
 Else
  ProcedureReturn "Unknown"
 EndIf 
EndProcedure
; ------------------------
HostName.s = GetHostName()
MessageRequester("HostName",HostName,0)

Posted: Wed Jun 04, 2003 10:16 pm
by TronDoc
Error Starting Program
[!] The PUREBASIC8491641.EXE file is
linked to missing export NETAPI32.DLL:NetWkstaGetInfo.
Maybe because I use Windows98 original?
Joe

Posted: Wed Jun 04, 2003 10:28 pm
by freak
Yep, Win NT/2k/XP only, sorry.

Timo

Posted: Wed Jun 04, 2003 10:30 pm
by TronDoc
thanks for the confirmation.
--jb

Posted: Thu Jun 05, 2003 7:59 am
by gnozal
Here is another example (Fred's code I think) ; it could work with Win98 ?

Code: Select all

Structure HOSTENT
  h_name.l
  h_aliases.l
  h_addrtype.w
  h_length.w
  h_addr_list.l
EndStructure


If InitNetwork()


  Hostname$ = Space(255)
  gethostname_(Hostname$,255)

  Debug "Hostname: "+Hostname$

  pHostinfo = gethostbyname_(Hostname$)

  If pHostinfo = 0 

    Debug "Unable to resolve domain name."

  Else

    CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT))


    If hostinfo\h_addrtype <> #AF_INET

      Debug "A non-IP address was returned."

    Else

      While PeekL(hostinfo\h_addr_list+AdressNumber*4)

        ipAddress = PeekL(hostinfo\h_addr_list+AdressNumber*4)
        Debug StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0)
        AdressNumber+1

      Wend

          

    EndIf

  EndIf

Else

  Debug "Network can't be initialized"

EndIf

Posted: Fri Jun 06, 2003 12:06 am
by freak
Should work, the commands used all work starting from Win95 8)

Timo