Board index » delphi » List Computers on the Network

List Computers on the Network

Hi,
  Does anyone know a way to get a list of IP numbers of the computers /
routers etc on a network?
  I am not sure if the network is mixed (if it matters).

I am new to network programming.  Sorry for basic questions but I am at a
loss.

Thanks,
Chris

 

Re:List Computers on the Network


"Chris" <dovest...@erols.com> schrieb im Newsbeitrag
news:3b6d928d_2@dnews...

Quote
> Hi,
>   Does anyone know a way to get a list of IP numbers of the computers /
> routers etc on a network?
>   I am not sure if the network is mixed (if it matters).

Hi Chris,

try this code:

uses
   .. winsock...

type
  TForm1 = class(TForm)
...

var
    WkSt       : Array[1..100] of String;    // collect the computer names
    IpAddrs    : Array[1..100] of String;   // collect the ip adresses
    WkStCount  : Integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

   procedure FindAllIPAddr(Workgroup : String);

      function HostNameToIpAddr(const sHost: string): string;
      var
         P: PHostEnt;
         Ia: TInAddr;
         sData: TWSAData;
      begin
         WSAStartup($101, sData);
         Ia.S_addr := 0;
         P := gethostbyname(PChar(sHost));
         if P <> NIL then
            with Ia.S_un_b, P^ do begin
               s_b1 := h_addr_list^[0];
               s_b2 := h_addr_list^[1];
               s_b3 := h_addr_list^[2];
               s_b4 := h_addr_list^[3];
            end;
         Result := inet_ntoa(ia);
         WSACleanUp;
      end;

   var
      EnumHandle         : THandle;
      WorkgroupRS      : TNetResource;
      Buf                       : Array[1..500] of TNetResource;
      BufSize                 : DWORD;
      Entries                   : DWORD;
      Result                    : Integer;
  begin

      WkstCount := 0;
      Workgroup := Workgroup + #0;
      FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);

      With WorkgroupRS do begin
         dwScope := 2;
         dwType := 3;
         dwDisplayType := 1;
         dwUsage := 2;
         lpRemoteName :=@Workgroup[1];
      end;

      WNetOpenEnum( RESOURCE_GLOBALNET,
                    RESOURCETYPE_ANY,
                    0,
                    @WorkgroupRS,
                    EnumHandle );

      Repeat
         Entries := 1;
         BufSize := SizeOf(Buf);

         Result := WNetEnumResource(EnumHandle, Entries, @Buf, BufSize);

         If (Result = NO_ERROR) and (Entries = 1) then begin
            Inc(WkstCount);

            // host name...
            Wkst[ WkstCount ] := StrPas(Buf[1].lpRemoteName);
            if pos('\\', Wkst[ WkstCount ])=1 then
              Wkst[ wkstCount ]:=copy(Wkst[ wkstCount ], 3, length(Wkst[
WkstCount ])-2);

            // ip-addr...
            IpAddrs[wkstCount]:=HostNameToIpAddr(Wkst[ WkstCount ]);

         end;

         Until (Entries <> 1) or (Result <> NO_ERROR);
         WNetCloseEnum( EnumHandle );
   end;

   function GetDomainName : AnsiString;
   type

      //Win9X ANSI prototypes from RADMIN32.DLL and RLOCAL32.DLL
      // look at http://www.mvps.org/win32/network/nt95.html for download...

      TWin95_NetWkstaUserGetInfo=function(Reserved:PChar; Level:Integer; var
BufPtr:Pointer):Integer; stdcall;
      TWin95_NetApiBufferFree=function(BufPtr:Pointer):Integer; stdcall;
      TWin95_NetGetDCName=function(ServerName:PChar; domain:PChar; var
BufPtr:Pointer):Integer; stdcall;
      TWin95_NetUserEnum=function(ServerName:PChar; Level,Filter:DWord; var
BufPtr:Pointer; PrefMax:DWord; var Read,Total,Resume:DWord):Integer;
stdcall;
      TWin95_NetLocalGroupEnum=function(ServerName:PChar; Level:DWord; var
BufPtr:Pointer; PrefMax:DWord; var Read,Total,Resume:DWord):Integer;
stdcall;
      TWin95_NetRemoteTOD=function(ServerName:PChar; var
pTOD:Pointer):Integer; stdcall;
      TWin95_NetUserGetInfo=function(ServerName,UserName:PChar; Level:DWord;
var BfrPtr:Pointer):Integer; stdcall;

      //WinNT UNICODE equivalents from NETAPI32.DLL

      TWinNT_NetWkstaGetInfo=function(ServerName:PWideChar; level:Integer;
var BufPtr:Pointer):Integer; stdcall;
      TWinNT_NetApiBufferFree=function(BufPtr:Pointer):Integer; stdcall;
      TWinNT_NetGetAnyDCName=function(ServerName:PWideChar;
domain:PWideChar; var BufPtr:PWideChar):Integer; stdcall;
      TWinNT_NetGetDCName=function(ServerName:PWideChar; domain:PWideChar;
var BufPtr:PWideChar):Integer; stdcall;
      TWinNT_NetUserEnum=function(ServerName:PWideChar; Level,Filter:DWord;
var BufPtr:Pointer; PrefMax:DWord; var Read,Total,Resume:DWord):Integer;
stdcall;
      TWinNT_NetLocalGroupEnum=function(ServerName:PWideChar; Level:DWord;
var BufPtr:Pointer; PrefMax:DWord; var Read,Total,Resume:DWord):Integer;
stdcall;
      TWinNT_NetRemoteTOD=function(ServerName:PWideChar; var
pTOD:Pointer):Integer; stdcall;
      TWinNT_NetUserGetInfo=function(ServerName,UserName:PWideChar;
Level:DWord; var BfrPtr:Pointer):Integer; stdcall;

   type

      WKSTA_INFO_100=record
         wki100_platform_id:Integer;
         wki100_computername:PWideChar;
         wki100_langroup:PWideChar;
         wki100_ver_major:Integer;
         wki100_ver_minor:Integer;
      end;

      WKSTA_USER_INFO_1=record
         wkui1_username:PChar;
         wkui1_logon_domain:PChar;
         wkui1_logon_server:PChar;
         wkui1_oth_domains:PChar;
      end;

      USER_INFO_1000 = record
         usri10_name : PWideChar;
         usri10_comment : PWideChar;
         usri10_usr_comment : PWideChar;
         usri10_full_name : PWideChar
      end;

      USER_INFO_10 = record
         usri10_name : PChar;
         usri10_comment : PChar;
         usri10_usr_comment : PChar;
         usri10_full_name : PChar
      end;

      function IsWinNT : Boolean;
      var
         VersionInfo: TOSVersionInfo;
      begin
         VersionInfo.dwOSVersionInfoSize := Sizeof(TOSVersionInfo);
         Result:=GetVersionEx(VersionInfo);
         if Result then
            Result:=VersionInfo.dwPlatformID=VER_PLATFORM_WIN32_NT;
      end;

   var

     Win95_NetApiBufferFree:    TWin95_NetApiBufferFree;
     Win95_NetWkstaUserGetInfo: TWin95_NetWkstaUserGetInfo;
     Win95_NetGetDCName:        TWin95_NetGetDCName;
     Win95_NetUserEnum:         TWin95_NetUserEnum;
     Win95_NetLocalGroupEnum:   TWin95_NetLocalGroupEnum;
     Win95_NetRemoteTOD:        TWin95_NetRemoteTOD;
     Win95_NetUserGetInfo:      TWin95_NetUserGetInfo;

     WinNT_NetApiBufferFree:    TWinNT_NetApiBufferFree;
     WinNT_NetWkstaGetInfo:     TWinNT_NetWkstaGetInfo;
     WinNT_NetGetDCName:        TWinNT_NetGetDCName;
     WinNT_NetGetAnyDCName:     TWinNT_NetGetAnyDCName;
     WinNT_NetUserEnum:         TWinNT_NetUserEnum;
     WinNT_NetLocalGroupEnum:   TWinNT_NetLocalGroupEnum;
     WinNT_NetRemoteTOD:        TWinNT_NetRemoteTOD;
     WinNT_NetUserGetInfo:      TWinNT_NetUserGetInfo;

     WSNT  : ^WKSTA_INFO_100;
     WSNTu : ^USER_INFO_1000;
     WS95  : ^WKSTA_USER_INFO_1;

     EC      : DWord;
     hNETAPI :THandle;

   begin

      try

         Result:='';

         if IsWinNT then begin
            hNETAPI:=LoadLibrary('NETAPI32.DLL');
            if hNETAPI<>0 then begin

@WinNT_NetWkstaGetInfo:=GetProcAddress(hNETAPI,'NetWkstaGetInfo');
                @WinNT_NetGetDCName:=GetProcAddress(hNETAPI,'NetGetDCName');

@WinNT_NetGetAnyDCName:=GetProcAddress(hNETAPI,'NetGetAnyDCName');

@WinNT_NetApiBufferFree:=GetProcAddress(hNETAPI,'NetApiBufferFree');
                @WinNT_NetUserEnum:=GetProcAddress(hNETAPI,'NetUserEnum');

@WinNT_NetLocalGroupEnum:=GetProcAddress(hNETAPI,'NetLocalGroupEnum');
                @WinNT_NetRemoteTOD:=GetProcAddress(hNETAPI,'NetRemoteTOD');

@WinNT_NetUserGetInfo:=GetProcAddress(hNETAPI,'NetUserGetInfo');

                EC:=WinNT_NetWkstaGetInfo(nil, 100,Pointer(WSNT));
                if EC=0 then begin
                   Result:=WideCharToString(WSNT^.wki100_langroup);
                   WinNT_NetApiBufferFree(Pointer(WSNT));
                end;
            end;
         end
         else begin
            hNETAPI:=LoadLibrary('RADMIN32.DLL');
            if hNETAPI<>0 then begin

               @Win95_NetGetDCName:=GetProcAddress(hNETAPI,'NetGetDCNameA');

@Win95_NetApiBufferFree:=GetProcAddress(hNETAPI,'NetApiBufferFree');

@Win95_NetWkstaUserGetInfo:=GetProcAddress(hNETAPI,'NetWkstaUserGetInfoA');
               @Win95_NetUserEnum:=GetProcAddress(hNETAPI,'NetUserEnumA');

@Win95_NetLocalGroupEnum:=GetProcAddress(hNETAPI,'NetGroupEnumA');
               @Win95_NetRemoteTOD:=GetProcAddress(hNETAPI,'NetRemoteTODA');

@Win95_NetUserGetInfo:=GetProcAddress(hNETAPI,'NetUserGetInfoA');

               EC:=Win95_NetWkstaUserGetInfo(nil, 1,Pointer(WS95));
               if EC=0 then begin
                  Result:=WS95^.wkui1_logon_domain;
                  Win95_NetApiBufferFree(Pointer(WS95));
               end;

            end;
         end;

      finally
          if hNETAPI<>0 then
             FreeLibrary(hNETAPI);
     end;
   end;

var
   sDomain : AnsiString;

begin
   sDomain:=GetDomainName;   // search for active domain..
   FindAllIPAddr(sDomain);
end;

I hope this helps.

Regards
Juri.

Other Threads