Private Type WSADATA wversion As Integer wHighVersion As Integer szDescription(MAX_WSADescription) As Byte szSystemStatus(MAX_WSASYSStatus) As Byte wMaxSockets As Long wMaxUDPDG As Long dwVendorInfo As Long End Type
Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type
' returns the standard host name for the local machine Private Declare Function apiGetHostName _ Lib "wsock32" Alias "gethostname" _ (ByVal Name As String, _ ByVal nameLen As Long) _ As Long
' retrieves host information corresponding to a host name ' from a host database Private Declare Function apiGetHostByName _ Lib "wsock32" Alias "gethostbyname" _ (ByVal hostname As String) _ As Long
' retrieves the host information corresponding to a network address Private Declare Function apiGetHostByAddress _ Lib "wsock32" Alias "gethostbyaddr" _ (addr As Long, _ ByVal dwlen As Long, _ ByVal dwType As Long) _ As Long
' moves memory either forward or backward, aligned or unaligned, ' in 4-byte blocks, followed by any remaining bytes Private Declare Sub sapiCopyMem _ Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, _ Source As Any, _ ByVal length As Long)
' converts a string containing an (Ipv4) Internet Protocol ' dotted address into a proper address for the IN_ADDR structure Private Declare Function apiInetAddress _ Lib "wsock32" Alias "inet_addr" _ (ByVal cp As String) _ As Long
' function initiates use of Ws2_32.dll by a process Private Declare Function apiWSAStartup _ Lib "wsock32" Alias "WSAStartup" _ (ByVal wVersionRequired As Integer, _ lpWsaData As WSADATA) _ As Long
Private Declare Function apilstrlen _ Lib "kernel32" Alias "lstrlen" _ (ByVal lpString As Long) _ As Long
Private Declare Function apilstrlenW _ Lib "kernel32" Alias "lstrlenW" _ (ByVal lpString As Long) _ As Long
' function terminates use of the Ws2_32.dll Private Declare Function apiWSACleanup _ Lib "wsock32" Alias "WSACleanup" _ () As Long
Function test() Debug.Print fGetHostIPAddresses(Environ("ComputerName")).Item(1) End Function
Function fGetHostIPAddresses(strHostName As String) As Collection ' ' Resolves the English HostName and returns ' a collection with all the IPs bound to the card ' On Error GoTo ErrHandler Dim lngRet As Long Dim lpHostEnt As HOSTENT Dim strOut As String Dim colOut As Collection Dim lngIPAddr As Long Dim abytIPs() As Byte Dim i As Integer
Set colOut = New Collection
If fInitializeSockets() Then strOut = String$(255, vbNullChar) lngRet = apiGetHostByName(strHostName) If lngRet Then
Do While (lngIPAddr) With lpHostEnt ReDim abytIPs(0 To .hLength - 1) strOut = vbNullString Call sapiCopyMem( _ abytIPs(0), _ ByVal lngIPAddr, _ .hLength) For i = 0 To .hLength - 1 strOut = strOut & abytIPs(i) & "." Next strOut = left$(strOut, Len(strOut) - 1) .hAddrList = .hAddrList + Len(.hAddrList) Call sapiCopyMem( _ lngIPAddr, _ ByVal lpHostEnt.hAddrList, _ Len(lngIPAddr)) If Len(Trim$(strOut)) Then colOut.Add strOut End With Loop End If End If Set fGetHostIPAddresses = colOut ExitHere: Call apiWSACleanup Set colOut = Nothing Exit Function ErrHandler: With Err MsgBox "Error: " & .Number & vbCrLf & .Description, _ vbOKOnly Or vbCritical, _ .Source End With Resume ExitHere End Function
Function fGetHostName(strIPAddress As String) As String ' ' Looks up a given IP address and returns the ' machine name it's bound to ' On Error GoTo ErrHandler Dim lngRet As Long Dim lpAddress As Long Dim strOut As String Dim lpHostEnt As HOSTENT
If fInitializeSockets() Then lpAddress = apiInetAddress(strIPAddress) lngRet = apiGetHostByAddress(lpAddress, 4, AF_INET) If lngRet Then Call sapiCopyMem( _ lpHostEnt, _ ByVal lngRet, _ Len(lpHostEnt)) fGetHostName = fStrFromPtr(lpHostEnt.hName, False) End If End If ExitHere: Call apiWSACleanup Exit Function ErrHandler: With Err MsgBox "Error: " & .Number & vbCrLf & .Description, _ vbOKOnly Or vbCritical, _ .Source End With Resume ExitHere End Function
Private Function fInitializeSockets() As Boolean Dim lpWsaData As WSADATA Dim wVersionRequired As Integer
Private Function fMakeWord( _ ByVal low As Integer, _ ByVal hi As Integer) _ As Integer Dim intOut As Integer Call sapiCopyMem( _ ByVal VarPtr(intOut) + 1, _ ByVal VarPtr(hi), _ 1) Call sapiCopyMem( _ ByVal VarPtr(intOut), _ ByVal VarPtr(low), _ 1) fMakeWord = intOut End Function
Private Function fStrFromPtr( _ pBuf As Long, _ Optional blnIsUnicode As Boolean) _ As String Dim lngLen As Long Dim abytBuf() As Byte
If blnIsUnicode Then lngLen = apilstrlenW(pBuf) * 2 Else lngLen = apilstrlen(pBuf) End If ' if it's not a ZLS If lngLen Then ReDim abytBuf(lngLen) ' return the buffer If blnIsUnicode Then 'blnIsUnicode is True not tested Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen) fStrFromPtr = abytBuf Else ReDim Preserve abytBuf(UBound(abytBuf) - 1) Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen) fStrFromPtr = StrConv(abytBuf, vbUnicode) End If End If End Function **********************************************************
-- @+ Jessy Sempere - Access MVP
------------------------------------ Site @ccess : http://access.jessy.free.fr/ Pour l'efficacité de tous : http://users.skynet.be/mpfa/ ------------------------------------ "Sonia LAMOURET" a écrit dans le message news: #
Bonjour à tous.
Comment puis-je récupérer l'IP d'un ardinateur dans une variable dans Access 97 ?
Merci d'avance
Bonjour
Copie le code qui suit dans un module de ta base de donnée
Ensuite il te suffit d'appeler la fonction "test()"
PS : je ne sais plus d'où vient le code mais il n'est pas de moi...
Private Type WSADATA
wversion As Integer
wHighVersion As Integer
szDescription(MAX_WSADescription) As Byte
szSystemStatus(MAX_WSASYSStatus) As Byte
wMaxSockets As Long
wMaxUDPDG As Long
dwVendorInfo As Long
End Type
Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
' returns the standard host name for the local machine
Private Declare Function apiGetHostName _
Lib "wsock32" Alias "gethostname" _
(ByVal Name As String, _
ByVal nameLen As Long) _
As Long
' retrieves host information corresponding to a host name
' from a host database
Private Declare Function apiGetHostByName _
Lib "wsock32" Alias "gethostbyname" _
(ByVal hostname As String) _
As Long
' retrieves the host information corresponding to a network address
Private Declare Function apiGetHostByAddress _
Lib "wsock32" Alias "gethostbyaddr" _
(addr As Long, _
ByVal dwlen As Long, _
ByVal dwType As Long) _
As Long
' moves memory either forward or backward, aligned or unaligned,
' in 4-byte blocks, followed by any remaining bytes
Private Declare Sub sapiCopyMem _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal length As Long)
' converts a string containing an (Ipv4) Internet Protocol
' dotted address into a proper address for the IN_ADDR structure
Private Declare Function apiInetAddress _
Lib "wsock32" Alias "inet_addr" _
(ByVal cp As String) _
As Long
' function initiates use of Ws2_32.dll by a process
Private Declare Function apiWSAStartup _
Lib "wsock32" Alias "WSAStartup" _
(ByVal wVersionRequired As Integer, _
lpWsaData As WSADATA) _
As Long
Private Declare Function apilstrlen _
Lib "kernel32" Alias "lstrlen" _
(ByVal lpString As Long) _
As Long
Private Declare Function apilstrlenW _
Lib "kernel32" Alias "lstrlenW" _
(ByVal lpString As Long) _
As Long
' function terminates use of the Ws2_32.dll
Private Declare Function apiWSACleanup _
Lib "wsock32" Alias "WSACleanup" _
() As Long
Function test()
Debug.Print fGetHostIPAddresses(Environ("ComputerName")).Item(1)
End Function
Function fGetHostIPAddresses(strHostName As String) As Collection
'
' Resolves the English HostName and returns
' a collection with all the IPs bound to the card
'
On Error GoTo ErrHandler
Dim lngRet As Long
Dim lpHostEnt As HOSTENT
Dim strOut As String
Dim colOut As Collection
Dim lngIPAddr As Long
Dim abytIPs() As Byte
Dim i As Integer
Set colOut = New Collection
If fInitializeSockets() Then
strOut = String$(255, vbNullChar)
lngRet = apiGetHostByName(strHostName)
If lngRet Then
Do While (lngIPAddr)
With lpHostEnt
ReDim abytIPs(0 To .hLength - 1)
strOut = vbNullString
Call sapiCopyMem( _
abytIPs(0), _
ByVal lngIPAddr, _
.hLength)
For i = 0 To .hLength - 1
strOut = strOut & abytIPs(i) & "."
Next
strOut = left$(strOut, Len(strOut) - 1)
.hAddrList = .hAddrList + Len(.hAddrList)
Call sapiCopyMem( _
lngIPAddr, _
ByVal lpHostEnt.hAddrList, _
Len(lngIPAddr))
If Len(Trim$(strOut)) Then colOut.Add strOut
End With
Loop
End If
End If
Set fGetHostIPAddresses = colOut
ExitHere:
Call apiWSACleanup
Set colOut = Nothing
Exit Function
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbOKOnly Or vbCritical, _
.Source
End With
Resume ExitHere
End Function
Function fGetHostName(strIPAddress As String) As String
'
' Looks up a given IP address and returns the
' machine name it's bound to
'
On Error GoTo ErrHandler
Dim lngRet As Long
Dim lpAddress As Long
Dim strOut As String
Dim lpHostEnt As HOSTENT
If fInitializeSockets() Then
lpAddress = apiInetAddress(strIPAddress)
lngRet = apiGetHostByAddress(lpAddress, 4, AF_INET)
If lngRet Then
Call sapiCopyMem( _
lpHostEnt, _
ByVal lngRet, _
Len(lpHostEnt))
fGetHostName = fStrFromPtr(lpHostEnt.hName, False)
End If
End If
ExitHere:
Call apiWSACleanup
Exit Function
ErrHandler:
With Err
MsgBox "Error: " & .Number & vbCrLf & .Description, _
vbOKOnly Or vbCritical, _
.Source
End With
Resume ExitHere
End Function
Private Function fInitializeSockets() As Boolean
Dim lpWsaData As WSADATA
Dim wVersionRequired As Integer
Private Function fMakeWord( _
ByVal low As Integer, _
ByVal hi As Integer) _
As Integer
Dim intOut As Integer
Call sapiCopyMem( _
ByVal VarPtr(intOut) + 1, _
ByVal VarPtr(hi), _
1)
Call sapiCopyMem( _
ByVal VarPtr(intOut), _
ByVal VarPtr(low), _
1)
fMakeWord = intOut
End Function
Private Function fStrFromPtr( _
pBuf As Long, _
Optional blnIsUnicode As Boolean) _
As String
Dim lngLen As Long
Dim abytBuf() As Byte
If blnIsUnicode Then
lngLen = apilstrlenW(pBuf) * 2
Else
lngLen = apilstrlen(pBuf)
End If
' if it's not a ZLS
If lngLen Then
ReDim abytBuf(lngLen)
' return the buffer
If blnIsUnicode Then
'blnIsUnicode is True not tested
Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen)
fStrFromPtr = abytBuf
Else
ReDim Preserve abytBuf(UBound(abytBuf) - 1)
Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen)
fStrFromPtr = StrConv(abytBuf, vbUnicode)
End If
End If
End Function
**********************************************************
--
@+
Jessy Sempere - Access MVP
news@access.fr.vu
------------------------------------
Site @ccess : http://access.jessy.free.fr/
Pour l'efficacité de tous :
http://users.skynet.be/mpfa/
------------------------------------
"Sonia LAMOURET" <slamouret@francelot.com> a écrit dans le message news:
#1QPThFsEHA.1272@TK2MSFTNGP09.phx.gbl...
Bonjour à tous.
Comment puis-je récupérer l'IP d'un ardinateur dans une variable dans Access
97 ?
Private Type WSADATA wversion As Integer wHighVersion As Integer szDescription(MAX_WSADescription) As Byte szSystemStatus(MAX_WSASYSStatus) As Byte wMaxSockets As Long wMaxUDPDG As Long dwVendorInfo As Long End Type
Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type
' returns the standard host name for the local machine Private Declare Function apiGetHostName _ Lib "wsock32" Alias "gethostname" _ (ByVal Name As String, _ ByVal nameLen As Long) _ As Long
' retrieves host information corresponding to a host name ' from a host database Private Declare Function apiGetHostByName _ Lib "wsock32" Alias "gethostbyname" _ (ByVal hostname As String) _ As Long
' retrieves the host information corresponding to a network address Private Declare Function apiGetHostByAddress _ Lib "wsock32" Alias "gethostbyaddr" _ (addr As Long, _ ByVal dwlen As Long, _ ByVal dwType As Long) _ As Long
' moves memory either forward or backward, aligned or unaligned, ' in 4-byte blocks, followed by any remaining bytes Private Declare Sub sapiCopyMem _ Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, _ Source As Any, _ ByVal length As Long)
' converts a string containing an (Ipv4) Internet Protocol ' dotted address into a proper address for the IN_ADDR structure Private Declare Function apiInetAddress _ Lib "wsock32" Alias "inet_addr" _ (ByVal cp As String) _ As Long
' function initiates use of Ws2_32.dll by a process Private Declare Function apiWSAStartup _ Lib "wsock32" Alias "WSAStartup" _ (ByVal wVersionRequired As Integer, _ lpWsaData As WSADATA) _ As Long
Private Declare Function apilstrlen _ Lib "kernel32" Alias "lstrlen" _ (ByVal lpString As Long) _ As Long
Private Declare Function apilstrlenW _ Lib "kernel32" Alias "lstrlenW" _ (ByVal lpString As Long) _ As Long
' function terminates use of the Ws2_32.dll Private Declare Function apiWSACleanup _ Lib "wsock32" Alias "WSACleanup" _ () As Long
Function test() Debug.Print fGetHostIPAddresses(Environ("ComputerName")).Item(1) End Function
Function fGetHostIPAddresses(strHostName As String) As Collection ' ' Resolves the English HostName and returns ' a collection with all the IPs bound to the card ' On Error GoTo ErrHandler Dim lngRet As Long Dim lpHostEnt As HOSTENT Dim strOut As String Dim colOut As Collection Dim lngIPAddr As Long Dim abytIPs() As Byte Dim i As Integer
Set colOut = New Collection
If fInitializeSockets() Then strOut = String$(255, vbNullChar) lngRet = apiGetHostByName(strHostName) If lngRet Then
Do While (lngIPAddr) With lpHostEnt ReDim abytIPs(0 To .hLength - 1) strOut = vbNullString Call sapiCopyMem( _ abytIPs(0), _ ByVal lngIPAddr, _ .hLength) For i = 0 To .hLength - 1 strOut = strOut & abytIPs(i) & "." Next strOut = left$(strOut, Len(strOut) - 1) .hAddrList = .hAddrList + Len(.hAddrList) Call sapiCopyMem( _ lngIPAddr, _ ByVal lpHostEnt.hAddrList, _ Len(lngIPAddr)) If Len(Trim$(strOut)) Then colOut.Add strOut End With Loop End If End If Set fGetHostIPAddresses = colOut ExitHere: Call apiWSACleanup Set colOut = Nothing Exit Function ErrHandler: With Err MsgBox "Error: " & .Number & vbCrLf & .Description, _ vbOKOnly Or vbCritical, _ .Source End With Resume ExitHere End Function
Function fGetHostName(strIPAddress As String) As String ' ' Looks up a given IP address and returns the ' machine name it's bound to ' On Error GoTo ErrHandler Dim lngRet As Long Dim lpAddress As Long Dim strOut As String Dim lpHostEnt As HOSTENT
If fInitializeSockets() Then lpAddress = apiInetAddress(strIPAddress) lngRet = apiGetHostByAddress(lpAddress, 4, AF_INET) If lngRet Then Call sapiCopyMem( _ lpHostEnt, _ ByVal lngRet, _ Len(lpHostEnt)) fGetHostName = fStrFromPtr(lpHostEnt.hName, False) End If End If ExitHere: Call apiWSACleanup Exit Function ErrHandler: With Err MsgBox "Error: " & .Number & vbCrLf & .Description, _ vbOKOnly Or vbCritical, _ .Source End With Resume ExitHere End Function
Private Function fInitializeSockets() As Boolean Dim lpWsaData As WSADATA Dim wVersionRequired As Integer
Private Function fMakeWord( _ ByVal low As Integer, _ ByVal hi As Integer) _ As Integer Dim intOut As Integer Call sapiCopyMem( _ ByVal VarPtr(intOut) + 1, _ ByVal VarPtr(hi), _ 1) Call sapiCopyMem( _ ByVal VarPtr(intOut), _ ByVal VarPtr(low), _ 1) fMakeWord = intOut End Function
Private Function fStrFromPtr( _ pBuf As Long, _ Optional blnIsUnicode As Boolean) _ As String Dim lngLen As Long Dim abytBuf() As Byte
If blnIsUnicode Then lngLen = apilstrlenW(pBuf) * 2 Else lngLen = apilstrlen(pBuf) End If ' if it's not a ZLS If lngLen Then ReDim abytBuf(lngLen) ' return the buffer If blnIsUnicode Then 'blnIsUnicode is True not tested Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen) fStrFromPtr = abytBuf Else ReDim Preserve abytBuf(UBound(abytBuf) - 1) Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen) fStrFromPtr = StrConv(abytBuf, vbUnicode) End If End If End Function **********************************************************
-- @+ Jessy Sempere - Access MVP
------------------------------------ Site @ccess : http://access.jessy.free.fr/ Pour l'efficacité de tous : http://users.skynet.be/mpfa/ ------------------------------------ "Sonia LAMOURET" a écrit dans le message news: #
Bonjour à tous.
Comment puis-je récupérer l'IP d'un ardinateur dans une variable dans Access 97 ?