PDA

Vollständige Version anzeigen : Wie ermittle ich einen lokalen Hostnamen?


Stefan Kulpa
28.01.2003, 17:09
Um den lokalen Hostnamen einer lokalen IP-Adresse ermitteln zu können, muss man auf das Win32-API zurückgreifen.
Hinweis: diese Funktion ist nicht dafür geeignet, IP-Adressen im Internet zu benutzen, um deren Hostnamen zu ermitteln!

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Const</span> WSADESCRIPTION_LEN <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 256
<span class="TOKEN">Const</span> WSASYS_STATUS_LEN <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 128
<span class="TOKEN">Const</span> WS_VERSION_REQD <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = &amp;H101
<span class="TOKEN">Const</span> IP_SUCCESS <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 0
<span class="TOKEN">Const</span> SOCKET_ERROR <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = -1
<span class="TOKEN">Const</span> AF_INET <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 2
&nbsp;
<span class="TOKEN">Type</span> WSADATA
wVersion <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wHighVersion <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
szDescription(0 <span class="TOKEN">To</span> WSADESCRIPTION_LEN) <span class="TOKEN">As</span> <span class="TOKEN">Byte</span>
szSystemStatus(0 <span class="TOKEN">To</span> WSASYS_STATUS_LEN) <span class="TOKEN">As</span> <span class="TOKEN">Byte</span>
imaxsockets <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
imaxudp <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
lpszvenderinfo <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> WSAStartup <span class="TOKEN">Lib</span> &quot;wsock32.dll&quot; _
(<span class="TOKEN">ByVal</span> VersionReq <span class="TOKEN">As</span> Long, _
WSADataReturn <span class="TOKEN">As</span> WSADATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> WSACleanup <span class="TOKEN">Lib</span> &quot;wsock32.dll&quot; () <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> inet_addr <span class="TOKEN">Lib</span> &quot;wsock32.dll&quot; _
(<span class="TOKEN">ByVal</span> s <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> gethostbyaddr <span class="TOKEN">Lib</span> &quot;wsock32.dll&quot; _
(haddr <span class="TOKEN">As</span> Long, _
<span class="TOKEN">ByVal</span> hnlen <span class="TOKEN">As</span> Long, _
<span class="TOKEN">ByVal</span> addrtype <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; _
(xDest <span class="TOKEN">As</span> Any, _
xSource <span class="TOKEN">As</span> Any, _
<span class="TOKEN">ByVal</span> nbytes <span class="TOKEN">As</span> <span class="TOKEN">Long</span>)
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> lstrlen <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;lstrlenA&quot; _
(lpString <span class="TOKEN">As</span> Any) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Function</span> SocketsInitialize() <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
&nbsp;
<span class="TOKEN">Dim</span> WSAD <span class="TOKEN">As</span> WSADATA
SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> GetHostNameFromIP(<span class="TOKEN">ByVal</span> sAddress <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
<span class="TOKEN">Dim</span> lPtrHostent <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> lAddress <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> lBytes <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">If</span> SocketsInitialize() <span class="TOKEN">Then</span>
<span class="REM"> '// String-Adresse in Long konvertieren</span>
lAddress = inet_addr(sAddress)
<span class="TOKEN">If</span> lAddress &lt;&gt; SOCKET_ERROR <span class="TOKEN">Then</span>
<span class="REM"> '// Den Pointer zur HOSTENT-Struktur ermitteln,</span>
<span class="REM"> '// die den Namen und die Adresse enth&auml;lt.</span>
lPtrHostent = gethostbyaddr(lAddress, 4, AF_INET)
<span class="TOKEN">If</span> lPtrHostent &lt;&gt; 0 <span class="TOKEN">Then</span>
<span class="REM"> '// Adresse konvertieren und Hostnamen aufl&ouml;sen</span>
CopyMemory lPtrHostent, <span class="TOKEN">ByVal</span> lPtrHostent, 4
lBytes = lstrlen(<span class="TOKEN">ByVal</span> lPtrHostent)
<span class="TOKEN">If</span> lBytes &gt; 0 <span class="TOKEN">Then</span>
sAddress = Space$(lBytes)
CopyMemory <span class="TOKEN">ByVal</span> sAddress, <span class="TOKEN">ByVal</span> lPtrHostent, lBytes
GetHostNameFromIP = sAddress
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'lBytes &gt; 0</span>
<span class="TOKEN">Else</span>
MsgBox &quot;Aufruf ist fehlgeschlagen!&quot;, vbExclamation
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'lPtrHostent &lt;&gt; 0</span>
<span class="TOKEN">Call</span> WSACleanup
<span class="TOKEN">Else</span>
MsgBox &quot;Ung&uuml;ltige IP-Adresse!&quot;, vbExclamation
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'lAddress &lt;&gt; SOCKET_ERROR</span>
<span class="TOKEN">Else</span>
MsgBox &quot;Sockets konnte nicht initialisiert werden!&quot;, vbExclamation
<span class="TOKEN">End</span> <span class="TOKEN">If</span> <span class="REM">'SocketsInitialize()</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)

Wird nun die Funktion GetHostNameFromIP mit Übergabe einer lokalen IP-Adresse übergeben, erhält man den entsprechenden Hostnamen.

Günther Kramer
01.04.2003, 12:05
Hallo,

hier noch eine weitere Möglichkeit.

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Private</span> <span class="TOKEN">Const</span> MAX_COMPUTERNAME <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 16
&nbsp;
<span class="TOKEN">Private</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> apiGetUserName <span class="TOKEN">Lib</span> &quot;advapi32.dll&quot; _
<span class="TOKEN">Alias</span> &quot;GetUserNameA&quot; (<span class="TOKEN">ByVal</span> lpBuffer <span class="TOKEN">As</span> String, _
nSize <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Private</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> GetComputerName <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
<span class="TOKEN">Alias</span> &quot;GetComputerNameA&quot; (<span class="TOKEN">ByVal</span> lpBuffer <span class="TOKEN">As</span> String, _
nSize <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Private</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> lstrlenW <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(<span class="TOKEN">ByVal</span> lpString <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Function Public Function GetLocalComputerName() As String</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Methode: | Ermittelt den Computername</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Parameter: | keine</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | Computername des aktuellen PCs</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="TOKEN">Public Function</span> GetLocalComputerName() <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">On Error GoTo</span> Err_GetLocalComputerName
&nbsp;
<span class="TOKEN">Dim</span> tmp <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
tmp = Space$(MAX_COMPUTERNAME)
&nbsp;
<span class="TOKEN">If</span> GetComputerName(tmp, Len(tmp)) &lt;&gt; 0 <span class="TOKEN">Then</span>
GetLocalComputerName = TrimNull(tmp)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
Exit_GetLocalComputerName:
<span class="TOKEN">Exit Function</span>
&nbsp;
Err_GetLocalComputerName:
MsgBox Err.Description
<span class="TOKEN">Resume</span> Exit_GetLocalComputerName
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Function Private Function TrimNull(startstr As String) As String</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Methode: | Zusatzfunktion, die f&uuml;r GetLocalComputerName</span>
<span class="REM">'// | ben&ouml;tigt wird</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// Parameter: | &Uuml;bergabestring</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | R&uuml;ckgabestring</span>
<span class="REM">'// -----------------------------------------------------------------------</span>
<span class="TOKEN">Private Function</span> TrimNull(startstr <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;</pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

_anton_
14.08.2008, 14:58
Hallo ,

noch eine Variante:
<span style="font-family: Courier New,FixedSys;"><br><span style="color: #0000CC;">Sub</span> hostname() <br>&nbsp; <span style="color: #0000CC;">Set</span> objShell = CreateObject(&quot;WScript.Shell&quot;) &nbsp;<br>&nbsp; <span style="color: #0000CC;">Set</span> objExec = objShell.Exec(&quot;hostname&quot;) <br>&nbsp; MsgBox objExec.StdOut.ReadAll <br><span style="color: #0000CC;">End Sub</span> &nbsp;<br></span>