Seite 1 von 1

GetIPbyName

Verfasst: 19.10.2007 16:08
von jpd
Hi,

habe heute rumgespielt ...

und denke das es für diesen bereich passt.

Ciao
jpd

Code: Alles auswählen

;http://www.purebasic.fr/german/archive/viewtopic.php?t=834#5330


Procedure.s Uni2Ansi2(*Unicode.l) 
  Size.l = WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, #Null, #Null, #Null, #Null) 
  ansi.s=Space(Size) 
  WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, @ansi, Size, #Null, #Null) 
  ProcedureReturn ansi 
EndProcedure 

Structure host_info
  computername.s
  ip_adress.s[8]  ;number of ip adress on the computer
  error.s
EndStructure
Global NewList host_info.host_info()

Procedure.s GetIPbyName (NameIP.s) 
  
  Protected AdressNumber.l
  AddElement( host_info())
  host_info()\computername=NameIP
  
 
    CompilerIf #PB_Compiler_Unicode 
      pHostinfo = gethostbyname_(Uni2Ansi(@NameIP))
    CompilerElse
      pHostinfo = gethostbyname_(NameIP)
    CompilerEndIf
    If pHostinfo = 0 
      
      host_info()\error=  "Unable To resolve domain name" 
    Else 
      CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT)) 
      If hostinfo\h_addrtype <> #AF_INET 
        
        host_info()\error=  "A non-IP address was returned"
      Else 
        
        While PeekL(hostinfo\h_addr_list+AdressNumber*4) 
          ipAddress = PeekL(hostinfo\h_addr_list+AdressNumber*4) 
         
         host_info()\ip_adress[AdressNumber]=StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0) 
          AdressNumber+1 
        Wend 
      EndIf 
    EndIf 
  ;If InitNetwork() 
 
EndProcedure 
If InitNetwork() 
  GetIPbyName ("jpd-pc")
Else 
  MessageRequester("Info",  "Network can't be initialized")
  End
EndIf 
  
ResetList ( host_info())

While NextElement(host_info())
  Debug  host_info()\computername
  If host_info()\error=""
    For x=0 To 7
      If host_info()\ip_adress[x] <>""
        Debug host_info()\ip_adress[x]
      EndIf   
    Next
  Else
    Debug "Error: "+  host_info()\error
  EndIf   
Wend 

ClearList( host_info())

Verfasst: 20.10.2007 00:20
von HeX0R

Verfasst: 20.10.2007 00:40
von jpd
Hi HeXor,

wenn dass nicht jemand anderes stört..

ist kein problem quellangabe eingetragen!