Link to home
Start Free TrialLog in
Avatar of friberg
friberg

asked on

List computers in local network

How can I list the computers that are on my local network in my Delphi app?
"FindFirst('\\*.*', faAnyFile, SearchRec);" doesn't find anything.
Avatar of BlackMan
BlackMan

Here's an example that lists all computers in the WORKGROUP workgroup. If you want to use the example directly, you need a form with a TListBox called LB.
The code is for D4, change DWORD to Integer if you are using D2/D3..


Var
  CurrentPlace : String;
  I : Integer;


type
  TNetRecArr = Array[0..9] of TNetResourceA;

procedure tform1.Scan(Name: String);
var
 Rec: TNetResourceA;
 Temp: TNetRecArr;
 NEHandle,
 NEResult,
 MaxRecs,
 BufSize: dword;
begin
  With Rec do
  begin
    dwScope:=RESOURCE_GLOBALNET;
    dwType:=RESOURCETYPE_DISK;
    dwDisplayType:=RESOURCEDISPLAYTYPE_SHARE;
    dwUsage:=0;
    lpLocalName:='';
    lpRemoteName:=PChar(Name);
    lpComment:='';
    lpProvider:='Microsoft Windows Network';
  End;
  CurrentPlace:='';
  NEResult:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_DISK,0,@Rec,NEHandle);
{ Open enumeration, all resources, only disk, and no printers }
  if NEResult=0 then
  begin
    BufSize:=SizeOf(Temp);
    repeat
      MaxRecs:=10;
      NEResult:=WNetEnumResource(NEHandle,MaxRecs,@Temp,BufSize);
      if (NEResult=NO_ERROR) or (NEResult=ERROR_NO_MORE_ITEMS) then
      begin
For I:=0 to MaxRecs-1 do
  LB.Items.Add(Temp[I].lpremoteName);
      end;
    until NEResult<>NO_ERROR;
    if NEResult=ERROR_NO_MORE_ITEMS then
      WNetCloseEnum(NEHandle);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Scan('WORKGROUP');
end;

Avatar of friberg

ASKER

BlackMan, thanks but I couldn't get it to work. NEResult=1204 and the procedure exits, although I have typed my WorkGroup name correctly. When I click on the Network icon on my desktop, I see almost 400 computers. Any suggestions?
1204 means "Bad Network Provider", I guess you are not running a US language OS?
Type to set lpProvider to either the name you can see in the Explorer under Network / Entire Network or just leave it blank ('')
1204 means "Bad Network Provider", I guess you are not running a US language OS?
Try to set lpProvider to either the name you can see in the Explorer under Network / Entire Network or just leave it blank ('')
Avatar of friberg

ASKER

Thanks, I set it to '' and now it works. I run the Swedish version of Win98, so that must have been the reason. Could you please submit it as an answer?

By the way, I can see 37 other networks if I click on Entire Network. Maybe you could help me to list these in Delphi too, so that I will be able to scan each one of them?

ASKER CERTIFIED SOLUTION
Avatar of BlackMan
BlackMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial