直接获取IP地址的函数

来源:互联网 发布:美工设计团队 编辑:程序博客网 时间:2024/05/20 20:46
uses  WinSock, ComObj//获得本地IPfunction GetLocalIP: String;type  TaPInAddr = Array[0..10] of PInAddr;  PaPInAddr = ^TaPInAddr;var  phe: PHostEnt;  pptr: PaPInAddr;  Buffer: Array[0..63] of AnsiChar;  i: Integer;  GInitData: TWSAData;begin  WSAStartup($101, GInitData);  Result := '';  GetHostName(Buffer, SizeOf(Buffer));  phe := GetHostByName(buffer);  if phe = nil then Exit;  pPtr := PaPInAddr(phe^.h_addr_list);  i := 0;  while pPtr^[i] <> nil do  begin    Result := inet_ntoa(pptr^[i]^);    Inc(i);  end;  WSACleanup;end;//获得外网IPfunction GetIP: string;var   xml : OleVariant;   r:string;   p1,p2 : Integer;begin   xml := CreateOleObject('Microsoft.XMLHTTP');   xml.Open('GET','http://www.net.cn/static/customercare/yourIP.asp', False);   xml.Send;   r := xml.responseText;   p1:=Pos('<h2>',r);  // 找到 h2 标签   p2:=Pos('</h2>',r);  // 找到 h2 结束标签   Result := Copy(r, p1+4, p2-p1-4);end;


 

原创粉丝点击