Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Über VBA IP-Adresse auslesen?

641 views
Skip to first unread message

Udo Heydkamp

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
Hallo,

könnte man über ein Excel-Makro (Excel 2000) die IP-Adresse des Rechners
auslesen, evtl.über eine (mehrere) API-Funktion(en) ?

Gruß
Udo

Michael Schwimmer

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Udo Heydkamp <che...@cityweb.de> schrieb in im Newsbeitrag: Jt2N3.5975$4q5.7...@news2.cityweb.de...

> könnte man über ein Excel-Makro (Excel 2000) die IP-Adresse des Rechners
> auslesen, evtl.über eine (mehrere) API-Funktion(en) ?

Hallo Udo
Schau dir mal das Posting von James Brown, gepostet in
microsoft.public.vb.winapi
am 6.10.99 unter Subject: Re: Get the computer's ip-adress
Message-ID: <37FAD394...@Brownfamily.com>
an.
Das ist genau das, was du suchst.
Hab's gerade mal unter Excel angetestet. Klappt!

MfG
Michael


Frank Arendt-Theilen

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Hallo Michael,
kannst du mit das Beispiel einmal zuschicken?
Danke im Voraus.

MfG Frank
__________________________________________________________
Frank Arendt-Theilen, MVP für Microsoft Excel
E-Mail: Thei...@t-online.de


Am Thu, 14 Oct 1999 00:48:46 +0200, schrieb "Michael Schwimmer"
<Schw...@T-Online.de> in microsoft.public.de.excel zu "Re: Über VBA
IP-Adresse auslesen?":

Udo Heydkamp

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Hallo Michael,
ich kann den News-Beitrag auch nicht (mehr) finden, vielleicht ist es Dir möglich, ihn
mir zur Verfügung zu stellen.
Vielen Dank schonmal

Übrigens hab ich seit kurzem das API-Buch v.Dan Appleman, wie Du schon angedeutet hattest, 'try and
error' ist es bei mir zur Zeit noch :).

Gruß
Udo


Michael Schwimmer schrieb:

Michael Schwimmer

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
> Schau dir mal das Posting von James Brown, gepostet in
> microsoft.public.vb.winapi
> am 6.10.99 unter Subject: Re: Get the computer's ip-adress
> Message-ID: <37FAD394...@Brownfamily.com>
> an.

Hallo,
Hier ist der Quelltext von James. Habe nur die Kommentare etwas gekürzt.
Hat auf Anhieb funktioniert.

Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public Const ERROR_SUCCESS& = 0
Public Const WS_VERSION_REQD& = &H101
Public Const WS_VERSION_MAJOR& = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD As Long = 1
Public Const SOCKET_ERROR As Long = -1

Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type

Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Public Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal szHost As String, ByVal dwHostLen As Long) As Long
Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal szHost As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Public Function GetIPAddress() As String
Dim sHostName As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim sIPAddr As String
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
" has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
sHostName = Trim$(sHostName)
lpHost = gethostbyname(sHostName)
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. " & _
"Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
'to extract the returned IP address, we have to copy
'the HOST structure and its members
CopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
'create an array to hold the result
ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
'and with the array, build the actual address,
'appending a period between members
For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
'the routine adds a period to the end of the
'string, so remove it here
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
SocketsCleanup
End Function

Public Function GetIPHostName() As String
Dim sHostName As String * 256
If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
" has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
SocketsCleanup
End Function

Public Function HiByte(ByVal wParam As Integer)
HiByte = wParam \ &H100 And &HFF&
End Function

Public Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function

Public Sub SocketsCleanup()
If WSACleanup() <> ERROR_SUCCESS Then
MsgBox "Socket error occurred in Cleanup."
End If
End Sub

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
Dim sLoByte As String
Dim sHiByte As String
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
MsgBox "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End If
If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "This application requires a minimum of " & _
CStr(MIN_SOCKETS_REQD) & " supported sockets."
SocketsInitialize = False
Exit Function
End If
If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
(LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
sHiByte = CStr(HiByte(WSAD.wVersion))
sLoByte = CStr(LoByte(WSAD.wVersion))
MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
" is not supported by 32-bit Windows Sockets."
SocketsInitialize = False
Exit Function
End If
'must be OK, so lets do it
SocketsInitialize = True
End Function

Sub FunktionTesten()
Dim Text1, Text2
Text1 = GetIPHostName()
If Text1 <> "" Then Text1 = MsgBox(Text1, vbInformation, "IP Adresse")
Text2 = GetIPAddress()
If Text2 <> "" Then Text2 = MsgBox(Text2, vbInformation, "Host Name")
End Sub

MfG
Michael


Bernd Busko

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
Reference: <7u5maa$n9q$1...@news06.btx.dtag.de>
From: Michael Schwimmer <Schw...@T-Online.de>

> If Text1 <> "" Then Text1 = MsgBox(Text1, vbInformation, "IP Adresse")
> Text2 = GetIPAddress()
> If Text2 <> "" Then Text2 = MsgBox(Text2, vbInformation, "Host Name")
>

Michael, Frank,

die Funktion ist wirklich sehr interessant. Danke für diesen Beitrag!
Als Hinweis für Anwender: In den obigen Zeilen sind IP und Hostname
vertauscht, MsgBox muss heändert werden.

Gruß,

Bernd
--
mailto:be...@busko.de
http://www.busko.de

0 new messages