"获得主机名和IP地址" 和 "监测局域网内计算机" Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiBase/html/delphi_20061209192223212.html
哪位大虾可以帮帮忙啊,帮我把这两段程序分析一下啊,万分感激!!!!!!
获得主机名和IP地址:
hostName;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs,
StdCtrls, Winsock;
type
TForm1 = class(TForm)
Edit1: TEdit;Label1: TLabel;Label2: TLabel;Edit2: TEdit;Edit3: TEdit;
Label3: TLabel;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
Ip:string;
Ipstr:string;
Buffer:Array[1..32] of char;
i:integer;
WSData:TWSAdata;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then
begin
ShowMessage('WS2_32.DLL初始化失败!'); halt; end;
try
if GetHostName(@Buffer[1],32)<>0 then
begin
ShowMessage('没有得到主机名!'); halt; end;
except
ShowMessage('没有成功返回主机名'); halt; end;
Host := GetHostByName(@Buffer[1]);
if Host = nil then
begin
ShowMessage('IP地址为空'); halt; end
else
begin
Edit2.Text := Host.h_name;
Edit3.Text := Chr(Host.h_addrtype+64);
for i:=1 to 4 do
begin
Ip:=IntToStr(Ord(Host.h_addr^[i-1]));
ShowMessage('分段IP地址为:'+Ip);
if i<4 then
Ipstr := Ipstr + Ip + '.'
else
Edit1.Text:=Ipstr+Ip; end; end;
WSACleanup; end; end.
监测局域网内计算机:
unit local;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Winsock;
type
TForm1 = class(TForm)
Button1: TButton; Edit1: TEdit; Memo1: TMemo; Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function FindComputer(ComputerName: string):Boolean;
implementation
{$R *.DFM}
function FindComputer(ComputerName: string):Boolean;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
Result :=True;
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(ComputerName));
if HostEnt = nil then Result := False;
WSACleanup; end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if FindComputer(edit1.text)=True then
Memo1.lines.Add(edit1.text+'在局域网上!')
else
Memo1.lines.Add(edit1.text+'不在局域网上!'); end; end.