Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
392to396
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
392to396
392to396
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

IP-Adresse ermitteln

IP-Adresse ermitteln
11.03.2004 12:36:51
Lars
Hallo,
kann ich per VBA die aktuelle IP-Adresse des Rechners ermitteln?
Gruß
Lars

9
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: IP-Adresse ermitteln
11.03.2004 12:41:26
Ulf
Jein, nur mithilfe von API-Funktionen.
Ulf
AW: IP-Adresse ermitteln
11.03.2004 12:50:52
Lars
Hallo Ulf,
durch Dein Jein nehme ich mal, dass es wohl nicht mit zwei Zeilen Code getan ist, oder?
Gruß
Lars
AW: IP-Adresse ermitteln
11.03.2004 12:55:21
Ulf
Richtig, es sind, je nachdem wieviel Leerzeilen dazwischen sind
soum die 100 Zeilen.
Ulf
AW: IP-Adresse ermitteln
11.03.2004 13:14:23
Lars
Hallo Ulf,
ich werde das Problem dann auf eine andere Art lösen.
Danke Dir trotzdem!
Gruß
Lars
AW: IP-Adresse ermitteln
11.03.2004 13:59:05
Tommy
Hallo Lars,
Den nachstehenden Code in ein Standardmodul eingeben, einer Schaltfläche zuweisen und starten.
StandardModule: Modul1
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () _
As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _
wVersionRequired&, lpWSAData As WinSocketDataType) _
As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () _
As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal _
HostName$, ByVal HostLen%) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal HostName$) As Long
Private Declare Function gethostbyaddr Lib "WSOCK32.DLL" _
(ByVal addr$, ByVal laenge%, ByVal typ%) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As _
Any, ByVal hpvSource&, ByVal cbCopy&)
Const WS_VERSION_REQD = &H101
Const SOCKET_ERROR = -1
Const WSADescription_Len = 256
Const WSASYS_Status_Len = 128
Private Type HostDeType
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Type WinSocketDataType
wversion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Byte
szSystemStatus(0 To WSASYS_Status_Len) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpszVendorInfo As Long
End Type
Private Declare Function RasEnumConnections Lib "RasApi32.DLL" _
Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As _
Long, lpcConnections As Long) As Long
Private Declare Function RasGetConnectStatus Lib "RasApi32.DLL" _
Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, _
lpStatus As Any) As Long
Private Declare Function RasEnumEntries Lib "RasApi32.DLL" _
Alias "RasEnumEntriesA" (ByVal reserved$, ByVal _
lpszPhonebook$, lprasentryname As Any, lpcb As Long, _
lpcEntries As Long) As Long
Private Declare Function RasHangUp Lib "RasApi32.DLL" _
Alias "RasHangUpA" (ByVal hRasConn As Long) As Long
Const RAS_MaxEntryName = 256
Const RAS_MaxDeviceType = 16
Const RAS_MaxDeviceName = 32
Private Type RASType
dwSize As Long
hRasCon As Long
szEntryName(RAS_MaxEntryName) As Byte
szDeviceType(RAS_MaxDeviceType) As Byte
szDeviceName(RAS_MaxDeviceName) As Byte
End Type
Private Type RASStatusType
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS_MaxDeviceType) As Byte
szDeviceName(RAS_MaxDeviceName) As Byte
End Type
Private Type RASENTRYNAME95
dwSize As Long
szEntryName(RAS_MaxEntryName) As Byte
End Type
Dim DFÜName$
Sub AufrufDNS()
Dim sIP As String
sIP = InputBox("IP-Adresse:", , "212.227.119.87")
If sIP = "" Then Exit Sub
Call WWWAddress(sIP)
End Sub

Sub AufrufIP()
Dim sDNS As String
sDNS = InputBox("DNS-Adresse:", , "www.herber.de")
Call WWWAddress(sDNS)
End Sub

Sub WWWAddress(strAddress As String)
Dim X%
Dim IP$, DNS$, HOST$
If Not Online Then Exit Sub
Call InitSockets
If IsNumeric(Left(strAddress, 1)) Then
DNS = HostByAddress(strAddress)
IP = strAddress
Else
IP = HostByName(strAddress)
DNS = strAddress
End If
ActiveCell.Value = "DNS: " & DNS & " " & "IP: " & IP
Call CleanSockets
End Sub


Private Sub InitSockets()
Dim Result%
Dim LoBy%, HiBy%
Dim SocketData As WinSocketDataType
Result = WSAStartup(WS_VERSION_REQD, SocketData)
If Result <> 0 Then
MsgBox ("'winsock.dll' antwortet nicht !")
End
End If
End Sub


Private Function HostByName(Name$, Optional X% = 0) As String
Dim MemIp() As Byte
Dim Y%
Dim HostDeAddress&, HostIp&
Dim IPAddress$
Dim HOST As HostDeType
HostDeAddress = gethostbyname(Name)
If HostDeAddress = 0 Then
HostByName = ""
Exit Function
End If
Call RtlMoveMemory(HOST, HostDeAddress, LenB(HOST))
For Y = 0 To X
Call RtlMoveMemory(HostIp, HOST.hAddrList + 4 * Y, 4)
If HostIp = 0 Then
HostByName = ""
Exit Function
End If
Next Y
ReDim MemIp(1 To HOST.hLength)
Call RtlMoveMemory(MemIp(1), HostIp, HOST.hLength)
IPAddress = ""
For Y = 1 To HOST.hLength
IPAddress = IPAddress & MemIp(Y) & "."
Next Y
IPAddress = Left$(IPAddress, Len(IPAddress) - 1)
HostByName = IPAddress
End Function


Private Function DFÜStatus() As Boolean
Dim RAS(255) As RASType, RASStatus As RASStatusType
Dim lg&, lpcon&, Result&
RAS(0).dwSize = 412
lg = 256 * RAS(0).dwSize
Result = RasEnumConnections(RAS(0), lg, lpcon)
If lpcon = 0 Then
DFÜStatus = False
Else
RASStatus.dwSize = 160
Result = RasGetConnectStatus(RAS(0).hRasCon, RASStatus)
If RASStatus.RasConnState = &H2000 Then
DFÜStatus = True
Else
DFÜStatus = False
End If
End If
End Function


Private Function Online() As Boolean
Dim Test As Boolean
Test = DFÜStatus
If Test = False Then MsgBox _
("Keine Online Verbindung vorhanden ! Bitte einwählen !")
Online = Test
End Function


Private Function HostByAddress(ByVal Addresse$) As String
Dim X%
Dim HostDeAddress&
Dim aa$, BB As String * 5
Dim HOST As HostDeType
aa = Chr$(Val(NextChar(Addresse, ".")))
aa = aa + Chr$(Val(NextChar(Addresse, ".")))
aa = aa + Chr$(Val(NextChar(Addresse, ".")))
aa = aa + Chr$(Val(Addresse))
HostDeAddress = gethostbyaddr(aa, Len(aa), 2)
If HostDeAddress = 0 Then
HostByAddress = ""
Exit Function
End If
Call RtlMoveMemory(HOST, HostDeAddress, LenB(HOST))
aa = ""
X = 0
Do
Call RtlMoveMemory(ByVal BB, HOST.hName + X, 1)
If Left$(BB, 1) = Chr$(0) Then Exit Do
aa = aa + Left$(BB, 1)
X = X + 1
Loop
HostByAddress = aa
End Function


Private Function MyHostName() As String
Dim HostName As String * 256
If gethostname(HostName, 256) = SOCKET_ERROR Then
MsgBox "Windows Sockets error " & Str(WSAGetLastError())
Exit Function
Else
MyHostName = NextChar(Trim$(HostName), Chr$(0))
End If
End Function


Private Sub CleanSockets()
Dim Result&
Result = WSACleanup()
If Result <> 0 Then
MsgBox ("Socket Error " & Trim$(Str$(Result)) & _
" in Prozedur 'CleanSockets' aufgetreten !")
End
End If
End Sub


Private Function NextChar(Text$, Char$) As String
Dim POS%
POS = InStr(1, Text, Char)
If POS = 0 Then
NextChar = Text
Text = ""
Else
NextChar = Left$(Text, POS - 1)
Text = Mid$(Text, POS + Len(Char))
End If
End Function


Gruß Tommy
Anzeige
AW: IP-Adresse ermitteln neues Makro
11.03.2004 14:08:26
Tommy
Hallo Lars,
sorry, war vorhin das falsche Makro, hier das richtige für Deinen Zweck:
Folgenden Code in Modul kopieren: und dann Start ausführen


Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As LongByVal Sort As LongAs Long
Const MAX_IP = 5 'To make a buffer... i dont think you have more than 5 ip on your pc.. 
Type IPINFO
dwAddr As Long ' IP address
dwIndex As Long ' interface index
dwMask As Long ' subnet mask
dwBCastAddr As Long ' broadcast address
dwReasmSize As Long ' assembly size
unused1 As Integer ' not currently used
unused2 As Integer '; not currently used
End Type 
Type MIB_IPADDRTABLE
dEntrys As Long 'number of entries in the table
mIPInfo(MAX_IP) As IPINFO 'array of IP address entries
End Type 
Type IP_Array
mBuffer As MIB_IPADDRTABLE
BufferLen As Long
End Type
Public Function ConvertAddressToString(longAddr As LongAs String
Dim myByte(3) As Byte
Dim Cnt As Long
CopyMemory myByte(0), longAddr, 4
For Cnt = 0 To 2
ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
Next Cnt
ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function 
Public Sub Start()
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE
Dim IPtext As String 
On Error GoTo END1
GetIpAddrTable ByVal 0&, Ret, True 
If Ret <= 0 Then Exit Sub
ReDim bBytes(0 To Ret - 1) As Byte
GetIpAddrTable bBytes(0), Ret, False
CopyMemory Listing.dEntrys, bBytes(0), 4
Tel = 0
CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
IPtext = ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr)
IPtext = IPtext & "."
MsgBox IPtext 'hier gegebenenfalls an eine Zelle übergeben
Exit Sub
END1:
MsgBox "ERROR"
End Sub

     Code eingefügt mit Syntaxhighlighter 2.5

Gruß
Tommy
Anzeige
Bei mir geht es nicht
11.03.2004 16:28:09
Andreas Walter
Bei mir bringt es
xxx.yyy.zzz.
wobei xxx yyy und zzz richtig sind, aber der 4. Teil fehlt
2 kleine Änderungen notwendig
11.03.2004 16:34:51
Andreas Walter
Folgendes funktioniert
For Cnt = 0 To 3 ' SCHLEIFE MUSS VON 0 BIS 3 GEHEN ***********************************
' IPtext = IPtext & "." Diese Zeile ist nicht benötigt******************************
AW: 2 kleine Änderungen notwendig
12.03.2004 15:26:59
Lars
Hallo Andreas,
Danke Dir, funktioniert super! Hätte aber wirklich nicht gedacht, dass das so aufwendig ist.
Gruß
Lars

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige