Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:58:18+00:00 2026-06-02T01:58:18+00:00

I would like to perform a thorough LAN devices discovery, so that I can

  • 0

I would like to perform a thorough LAN devices discovery, so that I can create a diagram similar to the one attached, but with additional information like IP and MAC addresses.

I’ve tried the code from Torry:

type
  PNetResourceArray = ^TNetResourceArray;
  TNetResourceArray = array[0..100] of TNetResource;

function CreateNetResourceList(ResourceType: DWord;
                              NetResource: PNetResource;
                              out Entries: DWord;
                              out List: PNetResourceArray): Boolean;
var
  EnumHandle: THandle;
  BufSize: DWord;
  Res: DWord;
begin
  Result := False;
  List := Nil;
  Entries := 0;
  if WNetOpenEnum(RESOURCE_GLOBALNET,
                  ResourceType,
                  0,
                  NetResource,
                  EnumHandle) = NO_ERROR then begin
    try
      BufSize := $4000;  // 16 kByte
      GetMem(List, BufSize);
      try
        repeat
          Entries := DWord(-1);
          FillChar(List^, BufSize, 0);
          Res := WNetEnumResource(EnumHandle, Entries, List, BufSize);
          if Res = ERROR_MORE_DATA then
          begin
            ReAllocMem(List, BufSize);
          end;
        until Res <> ERROR_MORE_DATA;

        Result := Res = NO_ERROR;
        if not Result then
        begin
          FreeMem(List);
          List := Nil;
          Entries := 0;
        end;
      except
        FreeMem(List);
        raise;
      end;
    finally
      WNetCloseEnum(EnumHandle);
    end;
  end;
end;

procedure ScanNetworkResources(ResourceType, DisplayType: DWord; List: TStrings);

procedure ScanLevel(NetResource: PNetResource);
var
  Entries: DWord;
  NetResourceList: PNetResourceArray;
  i: Integer;
begin
  if CreateNetResourceList(ResourceType, NetResource, Entries, NetResourceList) then try
    for i := 0 to Integer(Entries) - 1 do
    begin
      if (DisplayType = RESOURCEDISPLAYTYPE_GENERIC) or
        (NetResourceList[i].dwDisplayType = DisplayType) then begin
        List.AddObject(NetResourceList[i].lpRemoteName,
                      Pointer(NetResourceList[i].dwDisplayType));
      end;
      if (NetResourceList[i].dwUsage and RESOURCEUSAGE_CONTAINER) <> 0 then
        ScanLevel(@NetResourceList[i]);
    end;
  finally
    FreeMem(NetResourceList);
  end;
end;

begin
  ScanLevel(Nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ScanNetworkResources(RESOURCETYPE_DISK, RESOURCEDISPLAYTYPE_SERVER, ListBox1.Items);
end;

But it only returns the names of the computers in the network, without the router and their IP address. So that’s not really a solution.

Could you please tell me what is a good way to enumerate all devices (routers, computers, printers) in a local network, along with their IP and MAC addresses?

Thank you.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:58:19+00:00Added an answer on June 2, 2026 at 1:58 am

    I modified you code adding the function GetHostName and inet_ntoa to get the ip address and the SendARP function to get the MAC address of a network resource.

    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      StrUtils,
      Windows,
      WinSock,
      SysUtils;
    
    type
      PNetResourceArray = ^TNetResourceArray;
      TNetResourceArray = array[0..1023] of TNetResource;
    
    function SendArp(DestIP,SrcIP:ULONG;pMacAddr:pointer;PhyAddrLen:pointer) : DWord; StdCall; external 'iphlpapi.dll' name 'SendARP';
    
    function GetIPAddress(const HostName: AnsiString): AnsiString;
    var
      HostEnt: PHostEnt;
      Host: AnsiString;
      SockAddr: TSockAddrIn;
    begin
      Result := '';
      Host := HostName;
      if Host = '' then
      begin
        SetLength(Host, MAX_PATH);
        GetHostName(PAnsiChar(Host), MAX_PATH);
      end;
      HostEnt := GetHostByName(PAnsiChar(Host));
      if HostEnt <> nil then
      begin
        SockAddr.sin_addr.S_addr := Longint(PLongint(HostEnt^.h_addr_list^)^);
        Result := inet_ntoa(SockAddr.sin_addr);
      end;
    end;
    
    
    function GetMacAddr(const IPAddress: AnsiString; var ErrCode : DWORD): AnsiString;
    var
     MacAddr    : Array[0..5] of Byte;
     DestIP     : ULONG;
     PhyAddrLen : ULONG;
    begin
      Result    :='';
      ZeroMemory(@MacAddr,SizeOf(MacAddr));
      DestIP    :=inet_addr(PAnsiChar(IPAddress));
      PhyAddrLen:=SizeOf(MacAddr);
      ErrCode   :=SendArp(DestIP,0,@MacAddr,@PhyAddrLen);
      if ErrCode = S_OK then
       Result:=AnsiString(Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',[MacAddr[0], MacAddr[1],MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]]));
    end;
    
    
    function ParseRemoteName(Const lpRemoteName : string) : string;
    begin
      Result:=lpRemoteName;
      if StartsStr('\\', lpRemoteName) and (Length(lpRemoteName)>2) and (LastDelimiter('\', lpRemoteName)>2) then
       Result:=Copy(lpRemoteName, 3, PosEx('\', lpRemoteName,3)-3)
      else
      if StartsStr('\\', lpRemoteName) and (Length(lpRemoteName)>2) and (LastDelimiter('\', lpRemoteName)=2) then
       Result:=Copy(lpRemoteName, 3, length(lpRemoteName));
    end;
    
    
    function CreateNetResourceList(ResourceType: DWord;
                                  NetResource: PNetResource;
                                  out Entries: DWord;
                                  out List: PNetResourceArray): Boolean;
    var
      EnumHandle: THandle;
      BufSize: DWord;
      Res: DWord;
    begin
      Result := False;
      List := Nil;
      Entries := 0;
      if WNetOpenEnum(RESOURCE_GLOBALNET, ResourceType, 0, NetResource, EnumHandle) = NO_ERROR then
      begin
        try
          BufSize := $4000;  // 16 kByte
          GetMem(List, BufSize);
          try
            repeat
              Entries := DWord(-1);
              FillChar(List^, BufSize, 0);
              Res := WNetEnumResource(EnumHandle, Entries, List, BufSize);
              if Res = ERROR_MORE_DATA then
              begin
                ReAllocMem(List, BufSize);
              end;
            until Res <> ERROR_MORE_DATA;
    
            Result := Res = NO_ERROR;
            if not Result then
            begin
              FreeMem(List);
              List := Nil;
              Entries := 0;
            end;
          except
            FreeMem(List);
            raise;
          end;
        finally
          WNetCloseEnum(EnumHandle);
        end;
      end;
    end;
    
    procedure ScanNetworkResources(ResourceType, DisplayType: DWord);
    
    procedure ScanLevel(NetResource: PNetResource);
    var
      Entries: DWord;
      NetResourceList: PNetResourceArray;
      i: Integer;
      IPAddress, MacAddress : AnsiString;
      ErrCode : DWORD;
    begin
      if CreateNetResourceList(ResourceType, NetResource, Entries, NetResourceList) then try
        for i := 0 to Integer(Entries) - 1 do
        begin
          if (DisplayType = RESOURCEDISPLAYTYPE_GENERIC) or
            (NetResourceList[i].dwDisplayType = DisplayType) then
            begin
              IPAddress   :=GetIPAddress(ParseRemoteName(AnsiString(NetResourceList[i].lpRemoteName)));
              MacAddress  :=GetMacAddr(IPAddress, ErrCode);
              Writeln(Format('Remote Name %s Ip %s MAC %s',[NetResourceList[i].lpRemoteName, IPAddress, MacAddress]));
            end;
          if (NetResourceList[i].dwUsage and RESOURCEUSAGE_CONTAINER) <> 0 then
            ScanLevel(@NetResourceList[i]);
        end;
      finally
        FreeMem(NetResourceList);
      end;
    end;
    
    begin
      ScanLevel(Nil);
    end;
    
    var
      WSAData: TWSAData;
    begin
      try
       if WSAStartup($0101, WSAData)=0 then
       try
         ScanNetworkResources(RESOURCETYPE_ANY, RESOURCEDISPLAYTYPE_SERVER);
         Writeln('Done');
       finally
         WSACleanup;
       end;
      except
        on E:Exception do
          Writeln(E.Classname, ': ', E.Message);
      end;
      Readln;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to perform something similar to this (ie get the sum of
I would like to perform 5 consecutive reads to a slave devices and check
In my plugin code I would like to perform a WP_Query (or similar) which
Can someone explain the following htacess lines, I understand parts, but would like a
I would like to perform a key event detection in textbox. The keys should
I would like to perform a bitwise exclusive or of two strings in python,
I would like to perform a measurement and plot a graph while the measurement
I would like to perform a certain selection which (as I tested in sqldeveloper)
I'm working on a block module and would like to perform ajax operations on
I am new to Java, the function I would like to perform is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.