1,183
社区成员
发帖
与我相关
我的任务
分享
uses Winsock;
...
function getIP(DNS_hostname: string): string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(DNS_hostname));
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;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
mstr: string;
ss: TStringList;
i: integer;
begin
WinExec('cmd /c ipconfig/all >d:\abc.txt',SW_HIDE);
sleep(1000);
ss := TStringList.Create;
ss.LoadFromFile('d:\abc.txt');
for i:=0 to ss.Count -1 do
begin
mstr := Trim(ss.Strings[i]);
if Pos('Host Name',mstr)>0 then
begin
Delete(mstr,1,Pos(':',mstr)+1);
showmessage('hostname:' + mstr);
end;
if Pos('IP Address',mstr)>0 then
begin
Delete(mstr,1,Pos(':',mstr)+1);
showmessage('ipaddr:' + mstr);
end;
end;
ss.Destroy;
DeleteFile('d:\abc.txt');
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, ExtCtrls;
type
TForm1 = class(TForm)
UdpSocket1: TUdpSocket;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
self.Caption:=self.UdpSocket1.LocalHostName;
self.Caption:=self.Caption+':'+self.UdpSocket1.LocalHostAddr;
end;
end.