unit Ughcx;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetDynIP(HostName: string = ''): string;
type
TaPInAddr = Array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
IP: string;
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
IP := '0.0.0.0';
if HostName = '' then
GetHostName(Buffer, SizeOf(Buffer))
else
StrPCopy(Buffer, HostName);
phe := GetHostByName(buffer);
if phe = nil then
begin
Result := IP;
Exit;
end;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^ <> nil do
begin
IP := inet_ntoa(pptr^^);
Inc(I);
end;
WSACleanup;
Result := IP; //如果上网则为上网ip否则是网卡ip
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text:=GetDynIP('');
end;
end.