机器名--》IP地址(100分)

  • 主题发起人 主题发起人 xuzh9
  • 开始时间 开始时间
X

xuzh9

Unregistered / Unconfirmed
GUEST, unregistred user!
我已得到与我连接的所有机器的机器名,请问如何得到这些机器的IP地址??
 
function GetIPFromName(Name: string): string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(Name));
with HostEnt^ do
Result := Format('%d.%d.%d.%d', [Byte(h_addr^[0]),
Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
WSACleanup;
end;

Just Search: GetHostByName IP
 
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;

 
to shaofun兄弟:
能否给全面些,如一小个例子??
 
to xuzh9
你可以告诉我你是怎样得到与自己机相联的计算机名的呢?
email zdb123@263.net
 
to zdb123:
procedure TForm1.Button1Click(Sender: TObject);
VAR
command:string ;
comline: pchar ;
begin
command:='COMMAND.COM /C net view>c:/~~~.txt';
comline:=pchar(command);
WinExec(comline, sw_hide);
timer1.Enabled:=true;
end;
每秒试一次
procedure TForm1.Timer1Timer(Sender: TObject);
var f:tstringlist;
ll,ii:integer;
s1,s:string;
b:bool;
begin
f:=tstringlist.Create;
try
f.LoadFromFile('c:/~~~.txt');
except
end;
if f.Count>10 then begin
memo1.Clear;
memo1.lines.add(f.text);
timer1.Enabled:=false;
f.Free;
{去掉多余的行和'//'}
for ll:=memo1.lines.count-1 downto 0 do begin
if strpos(pchar(memo1.lines.strings[ll]),'//')=nil then
memo1.lines.delete(ll)
else
memo1.lines.Strings[ll]:=delxxx(memo1.lines.Strings[ll]);
{自编1个去'//'的函数}
end;
 
后退
顶部