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!

Connected to Network 1

Status
Not open for further replies.

ahhchu

Programmer
Sep 19, 2001
38
US
Can anybody tell me how I can detect if I am connected to a network. Let me clarify, not if the machine should be connected to the network, ie the lan adpator is configured. More like is the lan adpator live....

I guess I could ping a specified connection. But there must be another way other than pinging... How do the tray icons and windows env know if they are connected?

Thanks
 
hi,

check this code

Steph

{
From the MS-DOS prompt, you can enumerate the network
connections (drives) by using the following command:

net use

Programmatically, you would call WNetOpenEnum() to start
the enumeration of connected resources and
WNetEnumResources() to continue the enumeration.
The following sample code enumerates the network connections:

}


procedure TForm1.Button1Click(Sender: TObject);
var
i, dwResult: DWORD;
hEnum: THandle;
lpnrDrv: PNETRESOURCE;
s: string;
const
cbBuffer: DWORD = 16384;
cEntries: DWORD = $FFFFFFFF;
begin
dwResult := WNetOpenEnum(RESOURCE_CONNECTED,
RESOURCETYPE_ANY,
0,
nil,
hEnum);

if (dwResult <> NO_ERROR) then
begin
ShowMessage('Cannot enumerate network drives.');
Exit;
end;
s := '';
repeat
lpnrDrv := PNETRESOURCE(GlobalAlloc(GPTR, cbBuffer));
dwResult := WNetEnumResource(hEnum, cEntries, lpnrDrv, cbBuffer);
if (dwResult = NO_ERROR) then
begin
s := 'Network drives:'#13#10;
for i := 0 to cEntries - 1 do
begin
if lpnrDrv^.lpLocalName <> nil then
s := s + lpnrDrv^.lpLocalName + #9 + lpnrDrv^.lpRemoteName;
Inc(lpnrDrv);
end;
end
else if dwResult <> ERROR_NO_MORE_ITEMS then
begin
s := s + 'Cannot complete network drive enumeration';
GlobalFree(HGLOBAL(lpnrDrv));
break;
end;
GlobalFree(HGLOBAL(lpnrDrv));
until (dwResult = ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);
if s = '' then s := 'No network connections.';
ShowMessage(s);
end;


{***********************************************************************
FindComp Unit from
Fatih Olcer
fatiholcer@altavista.com
***********************************************************************}
unit FindComp;

interface

uses
Windows, Classes;

function FindComputers: DWORD;

var
Computers: TStringList;

implementation

uses
SysUtils;

const
MaxEntries = 250;

function FindComputers: DWORD;
var
EnumWorkGroupHandle, EnumComputerHandle: THandle;
EnumError: DWORD;
Network: TNetResource;
WorkGroupEntries, ComputerEntries: DWORD;
EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
EnumBufferLength: DWORD;
I, J: DWORD;
begin
Computers.Clear;

FillChar(Network, SizeOf(Network), 0);
with Network do
begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_ANY;
dwUsage := RESOURCEUSAGE_CONTAINER;
end;

EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
@Network, EnumWorkGroupHandle);

if EnumError = NO_ERROR then
begin
WorkGroupEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumWorkGroupBuffer);
EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries,
@EnumWorkGroupBuffer, EnumBufferLength);

if EnumError = NO_ERROR then
begin
for I := 1 to WorkGroupEntries do
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
@EnumWorkGroupBuffer, EnumComputerHandle);
if EnumError = NO_ERROR then
begin
ComputerEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumComputerBuffer);
EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries,
@EnumComputerBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
for J := 1 to ComputerEntries do
Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName,
3, Length(EnumComputerBuffer[J].lpRemoteName) - 2));
WNetCloseEnum(EnumComputerHandle);
end;
end;
end;
WNetCloseEnum(EnumWorkGroupHandle);
end;

if EnumError = ERROR_NO_MORE_ITEMS then
EnumError := NO_ERROR;
Result := EnumError;
end;

initialization

Computers := TStringList.Create;

finalization

Computers.Free;
end.
 
Hi,

Simply use the following Code

if GetSystemMetrics(SM_NETWORK) AND $01 = $01 then
ShowMessage('Machine is attached to network') else
ShowMessage('Machine is not attached to network');


Bye :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top