// Hiermit kann man
in einem Netz die IP-Adressen von einem
// Computernamen ableiten, bzw. den zugehörigen Namen zu einer
// IP-Adresse ermitteln. Voraussetzung ist, dass der Computer auch
// angemeldet ist.
// Getestet mit D4 unter XP
uses WinSock;
var
HE: PHostEnt;
WSAD: TWSAData;
function GetIP(HName: string): string;
type
PAdr = array[0..249] of PInAddr;
PPAdr = ^PAdr;
var
pp: PPAdr;
x: Integer;
begin
Result := 'nicht gefunden';
x := WSAStartup(MAKEWORD(2, 0), WSAD);
if x <> 0 then exit;
if copy(HName, 1, 7) = 'http://' then
HName := Copy(HName, 8, maxint)
else if copy(HName, 1, 2) = '\\' then
HName := Copy(HName, 3, maxint);
HE := GetHostByName(PChar(HName));
if HE = nil then Exit;
Result := '';
pp := PPAdr(HE^.h_addr_list);
for x := 0 to high(PAdr) do
if pp^[x] = nil then break
else Result := Result + inet_ntoa(pp^[x]^) + #13#10;
Result := Trim(Result);
WSACleanup;
end;
function IPToName(IPAddr: string): string;
var
SAI: TSockAddrIn;
s1, s2, s3: string;
p: integer;
begin
Result := 'nicht gefunden';
try
p := pos('.', IPAddr);
s1 := inttostr(strtoint(copy(IPAddr, 1, p - 1)));
IPAddr := copy(IPAddr, p + 1, maxint);
p := pos('.', IPAddr);
s2 := inttostr(strtoint(copy(IPAddr, 1, p - 1)));
IPAddr := copy(IPAddr, p + 1, maxint);
p := pos('.', IPAddr);
s3 := inttostr(strtoint(copy(IPAddr, 1, p - 1)));
IPAddr := s1 + '.' + s2 + '.' + s3 + '.' + copy(IPAddr, p + 1, maxint);
except
exit;
end;
p := WSAStartup(MAKEWORD(2, 0), WSAD);
if p <> 0 then exit;
SAI.sin_addr.s_addr := inet_addr(PChar(IPAddr));
HE := gethostbyaddr(@SAI.sin_addr.S_addr, 4, AF_INET);
if HE <> nil then
Result := string(HE^.h_name);
WSACleanup;
end;
// Beispielaufruf
procedure TForm1.Button5Click(Sender: TObject);
var
s: string;
begin
s := GetIP('PC100');
showmessage(s);
s := IPToName('002.227.007.036');
showmessage(s);
end;
|