Board index » delphi » List computers on the network

List computers on the network

Hi,

I'm stuck with a problem of getting a list of computers in my workgroup in a
listbox (for example) or a tstringlist.

What I want is that computers on the network are listed in a tlistbox or a
tstringlist, so I can do something with the computernames.

I hope someone can help me with this one, thanks

Berend

 

Re:List computers on the network


There was a sample application for MFC or VC
at http://www.codeguru.com I downloaded a while
back which let's you do remote shutdown.

In it there was a set of API calls to enumerate all
the connected computers on the network.

Also try searching the MSDN for the API function
"WNetGetNetworkInformation(...)" to put you in
the right area. Sorry I haven't got a Delphi example
for you but you might have better luck in newsgroup
borland.public.delphi.winapi for this one.

HTH

Re:List computers on the network


Quote
Berend wrote:

> Hi,

> I'm stuck with a problem of getting a list of computers in my workgroup in a
> listbox (for example) or a tstringlist.

> What I want is that computers on the network are listed in a tlistbox or a
> tstringlist, so I can do something with the computernames.

> I hope someone can help me with this one, thanks

> Berend

Try the following code. I wrote it with D4. It works good in out NT4 LAN.

Heri Bender

unit Network;

interface

uses
  // VCL:
  classes,sysutils,windows;

procedure GetNetworkList(sl : TStrings);
procedure GetDomainList(const sNetwork : string; sl : TStrings);
procedure GetServerList(const sDomain,sNetwork : string; sl : TStrings);
procedure GetShareList(const sServer : string; sl : TStrings);

type
  eNetEnum   = class(exception)
  private
    FWinError       : integer;
    FNetError       : integer;
  public
    constructor Create(const aMsg : string; aWinError : integer; aNetError : integer);
    property iWinError : integer
      read FWinError write FWinError;
    property iNetError : integer
      read FNetError write FNetError;
  end;

implementation

type
  TenEnumKind   = (ekNetwork,ekDomain,ekServer,ekShare);
const
  kasEnumKind   : array[TenEnumKind] of string[20] =
                           (
                            'Network',
                            'Domain',
                            'Server',
                            'Share'
                            );

function GetExtendedError(iWinErr : integer) : eNetEnum;
const
  kiDesc      = 255;
  kiProvider  = 255;
var
  iNetErr       : integer;
  zDesc         : array[0..pred(kiDesc)] of char;
  zProvider     : array[0..pred(kiProvider)] of char;
  sDesc         : String;
  sProvider     : string;
begin
  setLength(sDesc,kiDesc);
  SetLength(sProvider,kiProvider);
  WNetGetLastError(iNetErr,zDesc,kiDesc,zProvider,kiProvider);
  sDesc := StrPas(zDesc);
  sProvider := StrPas(zProvider);
  result := eNetEnum.Create(format('Net error %d occured. Message: "%s"; Provider:
"%s"',[iNetErr,sDesc,sProvider]),iWinErr,iNetErr);
end;

procedure EnumNetRes(aScope,aType,aUsage : integer; prNetRes : PNetResource; sl : TStrings; ek :
TenEnumKind);
const
  kiNetResCount = 1024;
type
  TarNetRes   = array[0..pred(kiNetResCount)] of TNetResource;

  procedure FillEntries(arNetRes : TarNetRes; iCount : integer);
  var
    i       : integer;
  begin
    for i := 0 to pred(iCount) do begin
      sl.Add(arNetRes[i].lpRemoteName);
    end;  // for  i
  end;

  procedure DoEnum(hEnum : THandle);
  var
    iWinErr           : integer;
    arNetRes          : TarNetRes;
    iCount            : integer;
    dwBufSize         : DWord;
  begin
    iCount := -1;
    fillchar(arNetRes,sizeof(arNetRes),0);
    dwBufSize := SizeOf(arNetRes);
    iWinErr := WNetEnumResource( hEnum,
                                 iCount,
                                 @arNetRes[0],
                                 dwBufSize);
    case iWinErr of
      No_Error                  : FillEntries(arNetRes,iCount);
      ERROR_NO_MORE_ITEMS,
      ERROR_INVALID_PARAMETER   : ;
      ERROR_ACCESS_DENIED       : sl.Add('Access denied');
      ERROR_EXTENDED_ERROR      : raise GetExtendedError(iWinErr);
    else
      raise eNetEnum.Create(format('Error in Get%sList. WinError: %d, NetError:
%d',[kasEnumKind[ek],iWinErr,0]),iWinErr,0);
    end; // case
  end;

var
  iWinErr           : Integer;
  hEnum             : THandle;
begin
  iWinErr := WNetOpenEnum(aScope,aType,aUsage,prNetRes,hEnum);
  try
    case iWinErr of
      NO_ERROR                  : DoEnum(hEnum);
      ERROR_NO_MORE_ITEMS,
      ERROR_INVALID_PARAMETER   : ;
      ERROR_ACCESS_DENIED       : sl.Add('Access denied');
      ERROR_EXTENDED_ERROR           : raise GetExtendedError(iWinErr);
    else
      raise eNetEnum.Create(format('Error in Get%sList. WinError: %d, NetError:
%d',[kasEnumKind[ek],iWinErr,0]),iWinErr,0);
    end;  // case
  finally
    WNetCloseEnum(hEnum);
  end;
end;

procedure GetNetworkList(sl : TStrings);
begin
  if sl = nil then exit;
  sl.BeginUpdate;
  try
    sl.Clear;
    EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nil,sl,ekNetwork);
  finally
    sl.EndUpdate;
  end;
end;

procedure GetDomainList(const sNetwork : string; sl : TStrings);
var
  rNetRes           : TNetResource;
begin
  if sl = nil then exit;
  sl.BeginUpdate;
  try
    sl.Clear;
    { Start here }
    with rNetRes do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType := RESOURCETYPE_ANY        ;
      dwDisplayType := RESOURCEDISPLAYTYPE_NETWORK;
      dwUsage := -2147483646;
      lpLocalName := nil;
      lpRemoteName := pChar(sNetwork);
      lpComment := nil;
      lpProvider := pChar(sNetwork);
    end;
    EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_Disk,3,@rNetRes,sl,ekDomain);
  finally
    sl.EndUpdate;
  end;
end;

procedure GetServerList(const sDomain,sNetwork : string; sl : TStrings);
var
  rNetRes        : TNetResource;
begin
  if sl = nil then exit;
  sl.BeginUpdate;
  try
    sl.Clear;
    with rNetRes do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType := RESOURCETYPE_ANY;
      dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
      dwUsage := RESOURCEUSAGE_CONTAINER;
      lpLocalName := nil;
      lpRemoteName := pChar(sDomain);
      lpComment := nil;
      lpProvider := pChar(sNetwork);
    end;

    EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@rNetRes,sl,ekServer);

  finally
    sl.EndUpdate;
  end;
end;

procedure GetShareList(const sServer : string; sl : TStrings);
var
  rNetRes       : TNetResource;
begin
  if sl = nil then exit;
  sl.BeginUpdate;
  try
    sl.Clear;
    with rNetRes do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType := RESOURCETYPE_DISK;
      dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
      dwUsage := RESOURCEUSAGE_CONTAINER                ;
      lpLocalName := nil;
      lpRemoteName := pChar(sServer);
      lpComment := nil;
      lpProvider := 'Microsoft Windows Network';   // ???
    end;

    EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_DISK,RESOURCEUSAGE_CONNECTABLE,@rNetRes,sl,ekShare);

  finally
    sl.EndUpdate;
  end;
end;

(******************************************************************************)
(******************************* eNetEnum *************************************)
(******************************************************************************)

constructor eNetEnum.create(const aMsg : string; aWinError : integer; aNetError : integer);
begin
  inherited create(aMsg);
  FWinError := aWinError;
  FNetError := aNetError;
end;

end.

Other Threads