Wie erhalte ich die lokale IP-Adresse des installierten Netzwerk-Adapters? - MS-Office-Forum
MS-Office-Forum

Zurück   MS-Office-Forum > Microsoft Access & Datenbanken > Microsoft Access - MOF-FAQ > MOF-FAQ - Module/VBA/VBE
Registrieren Forum Hilfe Alle Foren als gelesen markieren

Banner und Co.

Antworten
Themen-Optionen Ansicht
Alt 27.01.2003, 19:12   #1
Stefan Kulpa
MOF Meister
MOF Meister
Standard Wie erhalte ich die lokale IP-Adresse des installierten Netzwerk-Adapters?

Um die IP-Adresse der Netzwerkkarte zu ermitteln, kann man sich des Win32-API bedienen:


Code:

Const MAX_ADAPTER_NAME_LENGTH           As Long = 256
Const MAX_ADAPTER_DESCRIPTION_LENGTH    As Long = 128
Const MAX_ADAPTER_ADDRESS_LENGTH        As Long = 8
Const ERROR_SUCCESS                     As Long = 0
 
Type IP_ADDRESS_STRING
     IpAddr(0 To 15)                    As Byte
End Type
 
Type IP_MASK_STRING
     IpMask(0 To 15)                    As Byte
End Type
 
Type IP_ADDR_STRING
     dwNext                             As Long
     IpAddress                          As IP_ADDRESS_STRING
     IpMask                             As IP_MASK_STRING
     dwContext                          As Long
End Type
 
Type IP_ADAPTER_INFO
  dwNext                                As Long
  ComboIndex                            As Long
  sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3))          As Byte
  sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3))   As Byte
  dwAddressLength                                           As Long
  sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1))         As Byte
  dwIndex                                                   As Long
  uType                                                     As Long
  uDhcpEnabled                                              As Long
  CurrentIpAddress                                          As Long
  IpAddressList                                             As IP_ADDR_STRING
  GatewayList                                               As IP_ADDR_STRING
  DhcpServer                                                As IP_ADDR_STRING
  bHaveWins                                                 As Long
  PrimaryWinsServer                                         As IP_ADDR_STRING
  SecondaryWinsServer                                       As IP_ADDR_STRING
  LeaseObtained                                             As Long
  LeaseExpires                                              As Long
End Type
 
Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
                (pTcpTable As Any, _
                 pdwSize As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias _
                "RtlMoveMemory" _
                (dst As Any, _
                 src As Any, _
                 ByVal bcount As Long)
 
Function LocalIPAddress() As String
 
   Dim lRequired  As Long
   Dim btBuffer()      As Byte
   Dim uAdapter     As IP_ADAPTER_INFO
 '  Dim AdapterStr  As IP_ADDR_STRING
 
   Dim lPtr        As Long
   Dim sIPAddr     As String
   Dim bFound       As Boolean
 
    Call GetAdaptersInfo(ByVal 0&, lRequired)
    If lRequired > 0 Then
        ReDim btBuffer(0 To lRequired - 1) As Byte
        If GetAdaptersInfo(btBuffer(0), lRequired) = ERROR_SUCCESS Then
    '// Pointer zu den Daten im btBuffer() Array ermitteln
        lPtr = VarPtr(btBuffer(0))
        Do While (lPtr <> 0) 'And (bFound = False)
        '// Speicherdaten kopieren
            CopyMemory uAdapter, ByVal lPtr, LenB(uAdapter)
            With uAdapter
                sIPAddr = _
                TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))
                If Len(sIPAddr) > 0 Then
                    bFound = True
                    Exit Do
               End If
               lPtr = .dwNext
            End With  'With uAdapter
         Loop  'Do While (lPtr <> 0)
      End If   'If GetAdaptersInfo
   End If      'If lRequired > 0
   LocalIPAddress = sIPAddr
 
End Function
 
Private Function TrimNull(sValue As String)
 
    Dim iPos As Integer
    iPos = InStr(sValue, Chr$(0))
    If iPos Then
          TrimNull = Left$(sValue, iPos - 1)
    Else: TrimNull = sValue
    End If
 
End Function
Code eingefügt mit dem MOF Code Converter

Durch den Aufruf der Funktion LocalIPAddress() erhält man nun die IP-Adresse.

__________________

Stefan
Stefan Kulpa ist offline  
verlinken auf Del.icio.us Diese Seite zu Mister Wong hinzufügen
Antworten Auf Beitrag antworten
Alt 14.08.2008, 15:49   #2
_anton_
MOF User
MOF User
Standard

Variante mit WMI:

Sub IP_lokal()
  On Error Resume Next    
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")  
  Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = ''")  
  If Err = 0 Then  
    For Each objPing In colPings  
      If Err = 0 Then  
        If objPing.StatusCode = 0 Then  
          MsgBox "Lokale IPAddresse: " & objPing.ProtocolAddress
        End If  
      End If  
    Next
  End If  
End Sub  
_anton_ ist offline  
verlinken auf Del.icio.us Diese Seite zu Mister Wong hinzufügen
Antworten Auf Beitrag antworten
Antworten


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Besucher: 1)
 
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge anzufügen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

vB Code ist An.
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist An.
Gehe zu


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:00 Uhr.



Powered by: vBulletin Version 3.6.2 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.

Copyright ©2000-2024 MS-Office-Forum. Alle Rechte vorbehalten.
Copyright ©Design: Manuela Kulpa ©Rechte: Günter Kramer
Eine Verwendung der Inhalte in anderen Publikationen, auch auszugsweise,
ist ohne ausdrückliche Zustimmung der Autoren nicht gestattet.