Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to list hosts connected to a LAN?

Status
Not open for further replies.

xartas

Programmer
Aug 20, 2001
26
ES
Hi!

can someone provide a code snippet that lists every machine connected to a LAN? I'm trying to retrieve network names for those PC's in my domain and outside my domain but connected to the LAN my machine belongs too. The main trouble I've encountered using mailslots to solve this problem is that mailslots get PC's in my domain but not those outside; seems like someway they are invisible.

Thanks in advanced for your time and help.
 
Hi,

I had picked this up from some site long back.Dont remember which but anyway Hope this helps

function EnumerateFunc( hwnd: HWND; hdc: HDC ; lpnr: PNetResource ): Boolean;
const
cbBuffer : DWORD = 16384; // 16K is a good size
var
hEnum, dwResult, dwResultEnum : DWORD;
lpnrLocal : array
[0..16384 div SizeOf(TNetResource)] of TNetResource; // pointer to enumerated structures
i : integer;
cEntries : Longint;
begin
centries := -1; // enumerate all possible entries

// Call the WNetOpenEnum function to begin the enumeration.
dwResult := WNetOpenEnum(
RESOURCE_CONTEXT, // Enumerate currently connected resources.
RESOURCETYPE_DISK, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
hEnum // handle to the resource
);

if (dwResult <> NO_ERROR) then
begin
// Process errors with an application-defined error handler
Result := False;
Exit;
end;

// Initialize the buffer.
FillChar( lpnrLocal, cbBuffer, 0 );

// Call the WNetEnumResource function to continue
// the enumeration.
dwResultEnum := WNetEnumResource(hEnum, // resource handle
DWORD(cEntries), // defined locally as -1
@lpnrLocal, // LPNETRESOURCE
cbBuffer); // buffer size

// This is just printing
for i := 0 to cEntries - 1 do
begin
// loop through each structure and
// get remote name of resource... lpnrLocal.lpRemoteName)
end;

// Call WNetCloseEnum to end the enumeration.
dwResult := WNetCloseEnum(hEnum);

if(dwResult <> NO_ERROR) then
begin
// Process errors... some user defined function here
Result := False;
end
else
Result := True;
end;
 
Thank you earlrainer. I'll try it as soon as I can. Seems like your code piece is just what I was looking for. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top