I know there's plenty ways of enumerating computers and devices on a network, but how do I show just computers available for Windows file sharing? I need to provide a tree view of network computers and their shared folders. I know I could use existing shell controls for this, but I would rather keep it in my own tree view. It will only list those computers on the network which have shared folders. Past the computers and their shared folders, I can do the individual directory listing part myself. I just need to know how to get the computer list and their shared folder lists.
问题:
回答1:
WNetOpenEnum will give you all the computers on the network
The WNetOpenEnum function starts an enumeration of network resources or existing connections. You can continue the enumeration by calling the WNetEnumResource function.
NetShareEnum will you all shares on a machine.
Retrieves information about each shared resource on a server.
You can use the combination of both to filter out what you don't want
回答2:
Please consider the following resources if you are interested in sample Delphi code using WNetOpenEnum/WNetOpenEnum Windows API:
- Networked Drives by Angus John:
- Use the Windows API to generate a list of available Network Resources published in the EDN by Justin Swett
- Get a list of computers in a network from Delphitricks.com.
Having a good command of NETRESOURCE structure is also must.
I personnaly recommend the following listings from http://www.developpez.net's Forum:
Listing #1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
procedure EnumNetworkProc(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.EnumNetworkProc(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean);
begin
if aNetResource.dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN, RESOURCEDISPLAYTYPE_SERVER] then
ListBox1.Items.Add(StringOfChar(' ', aLevel*4) +aNetResource.lpRemoteName);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
EnumNetwork(EnumNetworkProc, RESOURCE_GLOBALNET, RESOURCETYPE_DISK);
end;
end.
Listing #2:
unit Unit2;
interface
uses Windows;
type
TEnumNetworkProc = procedure(const aNetResource :TNetResource; const aLevel :word; var aContinue :boolean) of object;
procedure EnumNetwork(const aEnumNetworkProc :TEnumNetworkProc; const aScope :dword = RESOURCE_GLOBALNET; const aType :dword = RESOURCETYPE_ANY);
implementation
//Procédure récursive
procedure DoEnumNetwork(const aContainer :Pointer;
const aEnumNetworkProc :TEnumNetworkProc;
const aScope :dword;
const aType :dword;
const aLevel :byte);
type
PNetResourceArray = ^TNetResourceArray;
TNetResourceArray = array [0..0] of TNetResource;
var
NetHandle :THandle;
NetResources :PNetResourceArray;
NetResult :dword;
Size, Count, i :Cardinal;
Continue :boolean;
begin
Continue := TRUE;
NetResult := WNetOpenEnum(aScope, aType, 0, aContainer, NetHandle);
if NetResult = NO_ERROR then
try
//Taille de base
Size := 50 *SizeOf(TNetResource);
GetMem(NetResources, Size);
try
while Continue do
begin
Count := $FFFFFFFF;
NetResult := WNetEnumResource(NetHandle, Count, NetResources, Size);
//Taille insuffisante ?
if NetResult = ERROR_MORE_DATA
then ReallocMem(NetResources, Size)
else Break;
end;
//Enumère
if NetResult = NO_ERROR then
for i := 0 to Count - 1 do
begin
//Callback
if Assigned(aEnumNetworkProc) then
begin
aEnumNetworkProc(NetResources^[i], aLevel, Continue);
if not Continue then Break;
end;
//Appel récursif
if (NetResources^[i].dwUsage and RESOURCEUSAGE_CONTAINER) > 0 then
DoEnumNetwork(@NetResources^[i], aEnumNetworkProc, aScope, aType, aLevel +1);
end;
finally
FreeMem(NetResources, Size);
end;
finally
WNetCloseEnum(NetHandle);
end;
end;
procedure EnumNetwork(const aEnumNetworkProc: TEnumNetworkProc; const aScope, aType: dword);
begin
DoEnumNetwork(nil, aEnumNetworkProc, aScope, aType, 0);
end;
end.
Source: Liste des machines sur un réseau local (tout le réseau de Win) - Original post (in French) by Lucas Panny.