当前位置: 首页 > 技术文档 > 正文
avatar

uses 添加 winsock 以下代码已测试可用 调用方法: str为字符变量!

uses 添加 winsock

以下代码已测试可用

function HostToIP(Name: ansistring; var Ip: string): Boolean;    //域名转ip
var
  wsdata : TWSAData;
  hostName : array [0..255] of AnsiChar;
  hostEnt : PHostEnt;
  addr :PAnsiChar;
begin
  WSAStartup ($0101, wsdata);
  try
    gethostname (hostName, sizeof (hostName));
    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;
function HosttoIp(HostName: string): string;
type
  tAddr = array[0..100] of PInAddr;
  pAddr = ^tAddr;
var
  I: Integer;
  WSA: TWSAData;
  PHE: PHostEnt;
  P: pAddr;
begin
  Result := '';

  WSAStartUp($101, WSA);
  try
    PHE := GetHostByName(pansichar(ansistring(HostName)));
    if (PHE <> nil) then
    begin
      P := pAddr(PHE^.h_addr_list);
      I := 0;
      while (P^[I] <> nil) do
      begin
        Result := string(inet_nToa(P^[I]^));
        Inc(I);
      end;
    end;
  except
  end;
end;

调用方法:

HostToIP(edit1.text,str);

str为字符变量!

本文固定链接: https://blog.yanhualou.com/jishu/45.html | 烟花楼博客
标签:

delphi xe域名转IP地址函数:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter