如何取得局域网的所有机器的IP地址(IP是动态分配的)?

arich 2002-04-18 02:03:33
如何取得局域网的所有机器的IP地址(IP是动态分配的)?
...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
arich 2002-04-18
  • 打赏
  • 举报
回复


要获取工作组用什么方法?



wylove 2002-04-18
  • 打赏
  • 举报
回复
首先获取工作组中的所有计算机
var
Computer: Array[1..500] of String[25];
ComputerCount: Integer;


procedure FindAllComputers(Workgroup : String);
Var
EnumHandle: THandle;
WorkgroupRS: TNetResource;
Buf: Array[1..500] of TNetResource;
BufSize: Integer;
Entries: Integer;
Result: Integer;

begin
ComputerCount := 0;

Workgroup := Workgroup + #0;

FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);
With WorkgroupRS do begin
dwScope := 2;
dwType := 3;
dwDisplayType := 1;
dwUsage := 2;
lpRemoteName := @Workgroup[1];
end;

WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
@WorkgroupRS,
EnumHandle );

Repeat
Entries := 1;
BufSize := SizeOf(Buf);

Result :=
WNetEnumResource( EnumHandle,
Entries,
@Buf,
BufSize );
If (Result = NO_ERROR) and (Entries = 1) then begin
Inc( ComputerCount );
Computer[ ComputerCount ] := StrPas(Buf[1].lpRemoteName);
end;
Until (Entries <> 1) or (Result <> NO_ERROR);

WNetCloseEnum( EnumHandle );
end;
然后将主机名解析成ip地址:
在use子句声明Winsock
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
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;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧