Determine network status


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj, ActiveX;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses ActiveDs_TLB;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
wmi : variant;
wmiService : variant;
nicstatus : variant;
card : Olevariant;
cnt : integer;
Enum: IEnumVariant;
P : IUnknown;
F : Cardinal;
connected : integer;
status : string;
begin
WMI:=CreateOleObject('WbemScripting.SWbemLocator');
wmiService := wmi.ConnectServer('.', 'root\wmi');
nicstatus := wmiService.InstancesOf('MSNdis_MediaConnectStatus');
p := nicstatus._NewEnum;
p.QueryInterface(IEnumVariant, Enum);

for cnt := 0 to nicstatus.Count - 1 do
begin
Enum.Next(1, card, F);
connected := card.NdisMediaConnectStatus;
if (connected = 0) then
Status := 'connected'
else
Status := 'network cable unplugged';
ShowMessage(card.InstanceName + ' : ' + Status);
end;
end;


function IsNetworkAlive(flags : LPDWORD) : boolean; stdcall; external 'sensapi.dll';

procedure ShowNetworkStatus;
var
Flags : DWORD;
Res : boolean;
begin
Res := IsNetworkAlive(@Flags);
if Res then
ShowMessage('Network connection is active')
else
ShowMessage('network cable unplugged');
end;

end.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s