Get HostName (like the console command 'hostname')

Share your advanced PureBasic knowledge/code with the community.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Get HostName (like the console command 'hostname')

Post 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)
TronDoc
Enthusiast
Enthusiast
Posts: 310
Joined: Wed Apr 30, 2003 3:50 am
Location: 3DoorsDown

Post by TronDoc »

Error Starting Program
[!] The PUREBASIC8491641.EXE file is
linked to missing export NETAPI32.DLL:NetWkstaGetInfo.
Maybe because I use Windows98 original?
Joe
peace
[pI 166Mhz 32Mb w95]
[pII 350Mhz 256Mb atir3RagePro WinDoze '98 FE & 2k]
[Athlon 1.3Ghz 160Mb XPHome & RedHat9]
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Yep, Win NT/2k/XP only, sorry.

Timo
quidquid Latine dictum sit altum videtur
TronDoc
Enthusiast
Enthusiast
Posts: 310
Joined: Wed Apr 30, 2003 3:50 am
Location: 3DoorsDown

Post by TronDoc »

thanks for the confirmation.
--jb
peace
[pI 166Mhz 32Mb w95]
[pII 350Mhz 256Mb atir3RagePro WinDoze '98 FE & 2k]
[Athlon 1.3Ghz 160Mb XPHome & RedHat9]
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Should work, the commands used all work starting from Win95 8)

Timo
quidquid Latine dictum sit altum videtur
Post Reply