1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| ' Déclaration des types
Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Public Type WSADATA
wversion As Integer
wHighVersion As Integer
szDescription(0 To 256) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUpDg As Integer
lpszVendorInfo As Long
End Type
' Déclarations des fonctions API
Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wversion&, lpWSAData As WSADATA) As Long
Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Public Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal hostname As String, ByVal HostLen As Integer) As Long
Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal hostname As String) As Long
Public Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
(Dest As Any, ByVal source As Long, ByVal cbCopy As Long)
Private Const SOCKET_ERROR = -1
' Type utilisateur
Public Type IPtype
Nom As String * 256
AdresseIP As String * 64
End Type
' Origine : Janick Tremblay
' E-mail : <a href="mailto:jafi@videotron.ca">jafi@videotron.ca</a>
Public Function ObtenirAdresseIP() As IPtype
' Cette fonction récupère le nom et
' l'adresse IP de la machine locale
Dim WSAD As WSADATA
Dim host As HOSTENT
Dim lgRetVal As Long
Dim stNom As String * 256
Dim lgAdresse As Long
Dim stIPadr As String
Dim Temp() As Byte
Dim lgFor As Long
' Initialisation
ObtenirAdresseIP.Nom = vbNullString
ObtenirAdresseIP.AdresseIP = vbNullString
' Vérifie l'accès à la DLL
If (WSAStartup(&H101, WSAD) <> 0) Then
MsgBox "WINSOCK.DLL ne répond pas.", vbExclamation, "Echec"
Exit Function
End If
' Récupération du nom
If (gethostname(stNom, Len(stNom)) = SOCKET_ERROR) Then
MsgBox "Erreur Winsock", vbExclamation, "Echec"
Exit Function
End If
' Récupération de l'adresse IP
lgAdresse = gethostbyname(stNom)
If (lgAdresse = 0) Then
MsgBox "WINSOCK.DLL ne répond pas.", vbExclamation, "Echec"
Exit Function
End If
CopyMemory host, lgAdresse, Len(host)
CopyMemory lgAdresse, host.hAddrList, 4
ReDim Temp(1 To host.hLength)
CopyMemory Temp(1), lgAdresse, host.hLength
' Récomposition de l'adresse
For lgFor = 1 To host.hLength
stIPadr = stIPadr & Temp(lgFor) & "."
Next lgFor
stIPadr = Left$(stIPadr, Len(stIPadr) - 1)
lgRetVal = WSACleanup()
' Retourne les valeurs
ObtenirAdresseIP.Nom = stNom
ObtenirAdresseIP.AdresseIP = stIPadr
End Function
Public Sub Main()
Dim Adr As IPtype
Dim stNom As String, stAdr As String
Dim lgTmp As Long
' Récupération des informations
Adr = ObtenirAdresseIP
lgTmp = InStr(Adr.Nom, Chr$(0))
If (lgTmp <> 0) Then
stNom = "Nom = " & Left$(Adr.Nom, lgTmp - 1)
stAdr = Trim$(Adr.AdresseIP)
End If
' Affiche le résultat
MsgBox stNom & vbCrLf & "Adresse IP = " & stAdr, vbOKOnly, "IP"
End Sub |
Partager