Ple*_*rds 0 delphi windows-7 delphi-xe3
我很久以前的这段代码应该返回一个字符串列表,每台计算机连接到我的Windows,workgroup但是当我测试时,我只得到自己的计算机,但我有三台连接相同workgroup名称的计算机并出现在我的Windows中Explorer网络列表.
可能有什么不对?还有另一种方法吗?
function FindAllComputers(Workgroup: string; WithIP: Boolean): TStringList;
var
EnumHandle : THandle;
WorkgroupRS : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : cardinal;
Entries : cardinal;
Res : Integer;
Computers : Tstringlist;
Limit, I: Integer;
begin
Limit := 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);
Computers := TStringList.Create;
repeat
Entries := 1;
BufSize := SizeOf(Buf);
Res := WNetEnumResource(EnumHandle, Entries, @Buf, BufSize);
if (Res = NO_ERROR) and (Entries = 1) then
begin
Computers.Add(StrPas(Buf[1].lpRemoteName));
end;
Inc(Limit);
until (Entries > 0) or (Res <> NO_ERROR) or (Limit > 100);
WNetCloseEnum( EnumHandle );
if WithIP then
begin
for I := 0 to Computers.Count-1 do
Computers[I] := Computers[I] + '=' + GetIP(Computers[I]);
end;
Result := Computers;
end;
Run Code Online (Sandbox Code Playgroud)
请参阅文档中的WNetEnumResource"lpcCount"(代码中的"条目"参数),返回时会收到枚举的项目数.如果枚举大于0,则终止枚举,但这是预期的.您正在请求枚举一个项目,该函数执行此操作并将其设置为1.只需从循环终止中删除该条件:
..
until (Res <> NO_ERROR) or (Limit > 100);
..
Run Code Online (Sandbox Code Playgroud)
您可能还想查看您不需要的代码StrPas.
| 归档时间: |
|
| 查看次数: |
2463 次 |
| 最近记录: |