Solved

Get the list of computer names from windows network neighbour hood

Posted on 2002-07-01
2
201 Views
Last Modified: 2010-04-04
Hai experts,

How do we get the list of computer names which are displayed in the network neighbourhood, programatically.
0
Comment
Question by:pradeep_pc
2 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 7121505
Look in the UDDF (http://www.gnomehome.demon.nl/uddf/) Networking Section.
0
 
LVL 2

Accepted Solution

by:
mikepj earned 50 total points
ID: 7123617
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    procedure DisplayStruct(lpnr : PNETRESOURCE);
    function EnumerateFunc(lpnr : PNETRESOURCE) : Boolean;
    procedure NetErrorHandler(
       dwResult  : DWORD;
       const Msg : String);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}


procedure TForm2.DisplayStruct(lpnr : PNETRESOURCE);
var
    p : PChar;
begin
    p := lpnr^.lpRemoteName;
    if (p[0] = '\') and (p[1] = '\') then
        p := p + 2;
    memo1.lines.Add(p);
    Caption := IntToStr(memo1.lines.Count);
end;

// Code by francois.piette@overbyte.be  http://www.overbyte.be
function TForm2.EnumerateFunc(lpnr : PNETRESOURCE) : Boolean;
var
    dwResult     : DWORD;
    dwResultEnum : DWORD;
    hEnum        : THandle;
    cbBuffer     : DWORD;             // 16K is a good size
    cEntries     : DWORD;             // enumerate all possible entries
    lpnrLocal    : PNETRESOURCE;      // pointer to enumerated structures
    PtrResource  : PNetResource;
    i            : DWORD;
begin
    cbBuffer := 16384;
    cEntries := DWORD(-1);

    if Application.Terminated then begin
        Result := FALSE;
        Exit;
    end;

    dwResult := WNetOpenEnum(RESOURCE_GLOBALNET,
                             RESOURCETYPE_ANY,
                             RESOURCEUSAGE_CONTAINER, // enumerate allresources
                             lpnr,                    // NULL first timethis function is called
                             hEnum);                  // handle to resource

    if dwResult <> NO_ERROR then begin
        NetErrorHandler(dwResult, 'WNetOpenEnum');
        Result := FALSE;
        Exit;
    end;

    repeat
        // Allocate memory for NETRESOURCE structures.
        GetMem(lpnrLocal, cbBuffer);

        dwResultEnum := WNetEnumResource(hEnum,       // resource handle
                                         cEntries,    // defined locally as0xFFFFFFFF
                                         lpnrLocal,   // LPNETRESOURCE
                                         cbBuffer);   // buffer size

        if dwResultEnum = NO_ERROR then begin
            for i := 0 to cEntries - 1 do begin
                PtrResource := PNETRESOURCE(PChar(lpnrLocal) + i *SizeOf(lpnrLocal^));
                if PtrResource^.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER then
                    DisplayStruct(PtrResource);
                Application.ProcessMessages;
                // If this NETRESOURCE is a container, call the function
                // recursively.
                if (RESOURCEUSAGE_CONTAINER = (PtrResource^.dwUsage and RESOURCEUSAGE_CONTAINER)) and
                   (PtrResource^.dwDisplayType <>RESOURCEDISPLAYTYPE_SERVER) then begin
                    if (not EnumerateFunc(PtrResource)) then begin
                        // TextOut(HandleDC, 10, 10, 'EnumerateFunc returned FALSE.', 29);
                    end;
                end;
            end;
        end
        else if dwResultEnum <> ERROR_NO_MORE_ITEMS then begin
            NetErrorHandler(dwResultEnum, 'WNetEnumResource');
            break;
        end;
    until dwResultEnum = ERROR_NO_MORE_ITEMS;

    FreeMem(lpnrLocal);

    dwResult := WNetCloseEnum(hEnum);

    if dwResult <> NO_ERROR then begin
        NetErrorHandler(dwResult, 'WNetCloseEnum');
        Result := FALSE;
        Exit;
    end;

    Result := TRUE;
end;

procedure TForm2.NetErrorHandler(
   dwResult  : DWORD;
   const Msg : String);
begin
    Application.MessageBox(PChar(Msg + ' Error #' + IntToStr(dwResult)),'Error', MB_OK);
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  EnumerateFunc(nil);
end;

end.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question