Solved

Get the list of computer names from windows network neighbour hood

Posted on 2002-07-01
2
195 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
Comment Utility
Look in the UDDF (http://www.gnomehome.demon.nl/uddf/) Networking Section.
0
 
LVL 2

Accepted Solution

by:
mikepj earned 50 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now