Voici une petite routine pour donner le HostName par une adresse IP. Fonctionne seulement sur Windows. La procédure StringConvert_memory() sert a la conversion du formatage d'une chaine en mémoire. Pour que le code soit compatible en Ascii et Unicode.
Code : Tout sélectionner
; +======================================================+
; | ROUTINE DE RECHERCHE DU HOSTNAME PAR ADRESSE IP. |
; +------------------------------------------------------+
; | COPYRIGHT(C)2007-2008, ALL RIGHT RESERVED KOAKDESIGN |
; +--------------+---------------------------------------+
; | Program type | PUREBASIC 4.40 |
; +--------------+---------------------------------------+
; | VER & REV | 0.0.1 |
; +--------------+---------------------------------------+
; | Program name | main.pbi |
; +======================================================+
; +======================================================+
; | Original Version: 0.0.1 |
; +--------------+---------------------------------------+
; | Created by | GallyHomeCorp |
; | Graphix by | |
; +--------------+---------------------------------------+
; | Comments: | |
; +--------------+ |
; | |
; | |
; | |
; +======================================================+
; +======================================================+
; | Système d'Exploitation |
; +--------------+---------------------------------------+
; | Window | Oui |
; | Linux | NON |
; | MacOS | NON |
; +======================================================+
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; +--------------------------------------------------------------------------+
; | |
; +--------------------------------------------------------------------------+
EnableExplicit
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; +--------------------------------------------------------------------------+
; | |
; +--------------------------------------------------------------------------+
InitNetwork()
#AF_INET = 2
#SOCKET_ERROR = -1
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; +--------------------------------------------------------------------------+
; | |
; +--------------------------------------------------------------------------+
Procedure.i StringConvert_memory(stext.s, format.l = #PB_Unicode)
; ROUTINE DE CONVERTION MUTI-FORMAT.
Define *mem
Define result.s
*mem = AllocateMemory(StringByteLength(stext, format)+2)
PokeS(*mem, stext, -1, format)
ProcedureReturn *mem
EndProcedure
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; +--------------------------------------------------------------------------+
; | |
; +--------------------------------------------------------------------------+
Procedure.s GetHostNameFromIP(sadress.s)
; ROUTINE DE RECHERCHE DU HOSTNAME.
Protected lhadress.l
Protected high.b = 1
Protected low.b = 1
Protected wsaversion.w
Protected wsa.WSAData
Protected *host.HOSTENT
PokeB (@wsaversion, high)
PokeB (@wsaversion + 1, low)
If WSAStartup_ (wsaversion, wsa) = #NOERROR
lhadress = inet_addr_(StringConvert_memory(sadress, #PB_Ascii))
If lhadress <> #SOCKET_ERROR
If sadress <> "0.0.0.0" And sadress <> ""
*host = gethostbyaddr_(@lhadress, 4, #AF_INET)
If *host <> 0
ProcedureReturn PeekS(*host\h_name, -1, #PB_Ascii)
EndIf
EndIf
EndIf
WSACleanup_ ()
EndIf
ProcedureReturn ""
EndProcedure
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; ****************************************************************************
; +--------------------------------------------------------------------------+
; | |
; +--------------------------------------------------------------------------+
Debug GetHostNameFromIP("127.0.0.1")
Cordialement,
GallyHC