Mega Code Archive

 
Categories / Delphi / Examples
 

Checking for a lan connection

To check for a LAN connection, various things can be done, like checking for your username, or retrieving logon information from the registry, but they can all fail if you consider a dockable notebook. The following function searches for actual existing connections. const MAX_NEIGHBORS = 20; function NetAvailable : boolean; var NetRes : array [0..MAX_NEIGHBORS] of TNetResource; NNError, hEnum, EntryCount, NetResLen : DWORD; loop1 : Integer; begin hEnum := -1; Result := FALSE; try NNError := WNetOpenEnum (RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, Nil, hEnum); if NNError = NO_ERROR then begin while NNError <> ERROR_NO_MORE_ITEMS do begin EntryCount := 1; NetResLen := SizeOf (NetRes); NNError := WNetEnumResource (hEnum, EntryCount, @NetRes, NetResLen); if (NNError = NO_ERROR) then begin for loop1 := 1 to EntryCount do begin if Pos ('Microsoft', NetRes[0].lpProvider) = 1 then begin Result := TRUE; break end end end else begin break end end; WNetCloseEnum (hEnum); // close enum end except on exception do if DEBUG then begin ShowMessage ('Network Neighborhood Detection Failed.') end; end end;