打印本文 打印本文 关闭窗口 关闭窗口
远程控制篇:通过机器名得到IP地址 通过IP地址得到机器名
作者:武汉SEO闵涛  文章来源:敏韬网  点击数990  更新时间:2009/4/23 18:42:16  文章录入:mintao  责任编辑:mintao

远程控制篇:

通过机器名得到IP地址

function procedure TForm1.nametoip(name:string):string;
var
  WSAData: TWSAData;
  HostEnt: PHostEnt;
begin
  result:='''''''';
  WSAStartup(2, WSAData);
  HostEnt := gethostbyname(PChar(name));
  if HostEnt <> nil then
  begin
    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])]);
    end;
    WSACleanup;
end;

通过IP地址得到机器名

function iptoname(ip:string):string;
var
  WSAData:TWSAData;
  p:PHostEnt;
  InetAddr:dword;
begin
  WSAStartup(2, WSAData);
  InetAddr:= inet_addr(PChar(IP));
try
   p:=GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
   result:=p^.h_name;
  except
   result:='''''''';
  end;
end;

---------------------------------------------------------

湖北襄樊  官本和(gbh12345@china.com)  2001.4
详见主页源程序:
http://delphi21cn.yeah.net
http://personal.hb.cninfo.net/~gbh

打印本文 打印本文 关闭窗口 关闭窗口