Board index » delphi » Get the computer names in a network ?

Get the computer names in a network ?

Hello,

I need to know, how I can get the names (or the ip-addresses) of the
different computers in a network ?
Is there a function in Delphi 3 C/S which return me these information ?

Thanks for any help,

Andreas.
a...@tmt.at
http://www.tmt.at/ante/

 

Re:Get the computer names in a network ?


It gets pretty hairy but you can use the WinAPI function:

DWORD WNetEnumResource(

    HANDLE hEnum,       // handle to enumeration
    LPDWORD lpcCount,   // pointer to entries to list
    LPVOID lpBuffer,    // pointer to buffer for results
    LPDWORD lpBufferSize        // pointer to buffer size variable
   );

Where:

hEnum= handle you get with the function below
lpcCount = The number of results you want, probably set as a fairly
large number.
lpBuffer = Address of array[1..lpcCount] of TNetResource
lpBufferSize = sizeof(lpBuffer)

You have to feed it the handle of an enumeration which you get using
the WinAPI function:

DWORD WNetOpenEnum(
    DWORD dwScope,      // scope of enumeration
    DWORD dwType,       // resource types to list
    DWORD dwUsage,      // resource usage to list
    LPNETRESOURCE lpNetResource,        // pointer to resource
structure
    LPHANDLE lphEnum    // pointer to enumeration handle buffer
   );

Where:

  dwScope:= RESOURCE_GLOBALNET;
  dwType:= RESOURCETYPE_DISK;
  dwUsage:= RESOURCEUSAGE_CONTAINER;
  lpNetResource:= NIL
  lphEnum is the returned THandle you put into hEnum above.

You can then iterate through the lpBuffer to get the network entries.
Since some of the entries might be workgroups, you may need to do this
recursively.

After calling WNetEnumResource, iterate through the lpBuffer entries
from which you can read the TNetResource structure entry details:

    DWORD  dwScope;
    DWORD  dwType;
    DWORD  dwDisplayType;
    DWORD  dwUsage;
    LPTSTR lpLocalName;
    LPTSTR lpRemoteName;
    LPTSTR lpComment;
    LPTSTR lpProvider;

You can then toss this structure back into the WNetOpenEnum function
above (as lpNetResource) to get a handle to toss into the
WNetEnumResource (as hEnum) and do the whole thing over to iterate
through the next level of network resource entries.

I'm new to these functions as well so if I've spewed out bogus info
somewhere, feel free to post an addendum.

          - Alex Olshove

In article <89g1s1$p4...@apans1.apa.at>,
  "Andreas Grabher" <a...@tmt.at> wrote:

Quote
> Hello,

> I need to know, how I can get the names (or the ip-addresses) of the
> different computers in a network ?
> Is there a function in Delphi 3 C/S which return me these
information ?

> Thanks for any help,

> Andreas.
> a...@tmt.at
> http://www.tmt.at/ante/

--
----------------------------------
Visit The Hovercraft Homepage
http://members.xoom.com/HoverHome

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:Get the computer names in a network ?


Hello, I got the following function from a german newsgroup.
It's very easy to use :-)

Cheers,

Andreas.

---%<---%<---%<---%<---%<---

function TMainWin.EnumClients(tar:TStrings):DWord;
var ContinueEnumeration:boolean;
   Function WNetEnumeration(PNetContainer: PNetResource):DWord;
   var BufferSize,NetEntries:DWORD;
       i:Integer;
       NetResources:TNetResources;
       NetHandle:THandle;
       Text:String;
   begin
      Result:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
        RESOURCEUSAGE_CONTAINER,PNetContainer,NetHandle);
      if Result<>NO_ERROR then
         exit;
      ContinueEnumeration:=True;
      while ContinueEnumeration do
      begin
         NetEntries:=MaxNetResources;
         BufferSize:=SizeOf(NetResources);

Result:=WNetEnumResource(NetHandle,NetEntries,@NetResources[0],BufferSize);

         if Result=NO_ERROR then
         begin
            for i:=0 to Pred(NetEntries)do
            begin
               if NetResources[i].dwDisplayType=RESOURCEDISPLAYTYPE_SERVER
then
               begin
                  text:=NetResources[i].lpRemoteName;
                  while Pos('\',text)=1 do
                     Delete(text,1,1);
                  tar.Add(Text+' '+GetIP(Text));
               end;
               if NetResources[i].dwUsage<>RESOURCEUSAGE_CONNECTABLE then
                  WNetEnumeration(@NetResources[i]);
            end;
         end
         else
         begin
            Result:=GetLastError;
            ContinueEnumeration:=Result=ERROR_MORE_DATA;
         end;
      end;
      if NetHandle<>0 then
         WNetCloseEnum(NetHandle);
   end;
begin
   tar.Clear;
   Result:=WNetEnumeration(nil);
end;

---%<---%<---%<---%<---%<---

Other Threads