PDA

Vollständige Version anzeigen : Wie erhalte ich die lokale IP-Adresse des installierten Netzwerk-Adapters?


Stefan Kulpa
27.01.2003, 19:12
Um die IP-Adresse der Netzwerkkarte zu ermitteln, kann man sich des Win32-API bedienen:


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

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

_anton_
14.08.2008, 15:49
Variante mit WMI:
<span style="font-family: Courier New,FixedSys;"><br><span style="color: #0000CC;">Sub</span> IP_lokal() <br>&nbsp; <span style="color: #0000CC;">On Error Resume Next</span> &nbsp; &nbsp;<br>&nbsp; <span style="color: #0000CC;">Set</span> objWMIService = GetObject(&quot;winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2&quot;) &nbsp; <br>&nbsp; <span style="color: #0000CC;">Set</span> colPings = objWMIService.ExecQuery(&quot;Select * From Win32_PingStatus where Address = ''&quot;) &nbsp;<br>&nbsp; <span style="color: #0000CC;">If</span> Err = 0 <span style="color: #0000CC;">Then</span> &nbsp;<br>&nbsp; &nbsp; <span style="color: #0000CC;">For Each</span> objPing <span style="color: #0000CC;">In</span> colPings &nbsp; <br>&nbsp; &nbsp; &nbsp; <span style="color: #0000CC;">If</span> Err = 0 <span style="color: #0000CC;">Then</span> &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000CC;">If</span> objPing.StatusCode = 0 <span style="color: #0000CC;">Then</span> &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox &quot;Lokale IPAddresse: &quot; & objPing.ProtocolAddress<br>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000CC;">End If</span> &nbsp;<br>&nbsp; &nbsp; &nbsp; <span style="color: #0000CC;">End If</span> &nbsp;<br>&nbsp; &nbsp; <span style="color: #0000CC;">Next</span> <br>&nbsp; <span style="color: #0000CC;">End If</span> &nbsp;<br><span style="color: #0000CC;">End Sub</span> &nbsp;<br></span>