Detecting Internet connection...

Does anyone have any source code to detect a connection to the Internet when the program's run under Windows 95/98 or NT?  It needs to be smart enough to know a network connection isn't an internet connection.  
dokkenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rarigoCommented:
Hi dokken,


   This might work without problems.


function TForm1.Connected : Boolean;
begin    
   Result := ( TCP.LocalIP <> '127.0.0.1' ) and ( TCP.LocalIP <> '0.0.0.0' );
end;


Sinceramente,
Reginaldo
0
intheCommented:
hi
yes i can send a component i use that seems to work ok
i dont know where i downloaded them so if you dont mind i email them to you
Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
or thatll work if you have tcp comp..
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

intheCommented:
actually another one of the safest methods is also too ping a known adress like www.borland.com 
0
dokkenAuthor Commented:
Barry,

Is that component a .dcu file or is source code?
0
intheCommented:
Hi
yep source code included with both.
(i say both because the one for detecting (Tonline) uses a tdialup comp (also has source)tdialup contains many of the ras functions ie adding phonebook entrys etc..among many other functions..

this is tonline.pas its not too big:


{
===============================================================================
This component detects if you are online or not.

Properties:

  OnLine -> true if you're online;
  Connections -> TStringList with list of online connections;
  Status -> The last status reported by RAS Api.

Events:

  OnChange -> Fired when a connection is raised or dropped.

===============================================================================
}
unit OnLine;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Dialup;

type
  TOnChange = procedure (Sender: TObject) of Object;

  TOnLineDetect = class(TComponent)
  private
    FOnChange:TOnChange;
    FStatus:TRasConnStatusA;
    FConnections:TStringList;
  protected
    FTimer:TTimer;
    FDialup:TDialup;
    FOldConnections:Byte;
    procedure CheckConnectionsTimer(Sender: TObject);
    procedure DialUpEntryGet(Sender: TObject; EntryName: array of Char);
    procedure DialUpActiveConnection(Sender: TObject; Handle: Integer;
    Status: TRasConnStatusA; StatusString: String; EntryName, DeviceType,
    DeviceName: array of Char);
    function GetInterval:Integer;
    procedure SetInterval(I:Integer);
  public
    function OnLine:bool;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Status:TRasConnStatusA read FStatus write FStatus;
    property Connections:TStringList read FConnections write FConnections;
  published
    property Interval:Integer read GetInterval write SetInterval default 250;
    property OnChange:TOnChange read FOnChange write FOnChange;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Internet', [TOnLineDetect]);
end;

constructor TOnLineDetect.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDialup := TDialup.Create(Self);
  FDialup.OnActiveConnection := DialupActiveConnection;
  FDialup.OnEntryGet := DialupEntryGet;
  FDialup.GetEntries;
  FTimer := TTimer.Create(Self);
  FTimer.Interval := 250;
  FTimer.OnTimer := CheckConnectionsTimer;
  FConnections := TStringList.Create;
  if not (csDesigning in ComponentState) then
  begin
    FTimer.Enabled := True;
  end;
end;

destructor TOnLineDetect.Destroy;
begin
  inherited Destroy;
  FConnections.Free;
end;

procedure TOnLineDetect.CheckConnectionsTimer(Sender: TObject);
begin
  FTimer.enabled := false;
  FOldConnections := FConnections.Count;
  FConnections.Clear;
  FDialup.GetConnections;
  if FConnections.Count <> FOldConnections then
    if Assigned(FOnChange) then FOnChange(Sender);
  FTimer.enabled := true;
end;

procedure TOnLineDetect.DialUpEntryGet(Sender: TObject;
  EntryName: array of Char);
begin
  FDialup.Entries.Add(EntryName);
end;

procedure TOnLineDetect.DialUpActiveConnection(Sender: TObject; Handle: Integer;
  Status: TRasConnStatusA; StatusString: String; EntryName, DeviceType,
  DeviceName: array of Char);
begin
  FConnections.Add(EntryName);
  FStatus := Status;
end;

function TOnLineDetect.OnLine:bool;
Begin
  Result := (FConnections.Count>0);
end;

function TOnLineDetect.GetInterval:Integer;
begin
  Result := FTimer.Interval;
end;

procedure TOnLineDetect.SetInterval(I:Integer);
begin
  FTimer.Interval := I;
end;

end.



tdialup which it needs is too much too post here (+ it comes with examples)
quite handy they are,do you want them ?
Regards Barry
0
dokkenAuthor Commented:
Barry,

You can email the tdialup to greg_68@hotmail.com.  I'll give it a try.  

Do you know if it requires any .dll's?  I ran into that problem with another component which prevented it from working under NT.
0
intheCommented:
hi
i have sent .
as long as ras is installed it should be fine .let me know how it goes on nt.
cheers
0
dokkenAuthor Commented:
Barry,

Sorry for the delay... I don't have NT installed right now so I'm waiting to hear back from someone who's testing the code on their NT machine.

I'll let you know something as soon as I hear something.
0
intheCommented:
ok
0
rarigoCommented:
Just for curiosity. What's wrong with my suggested way to check this?



Sinceramente,
Reginaldo
0
dokkenAuthor Commented:
Reginaldo,

Your suggestion won't work for me since I don't have the TCP component.

Barry,

I haven't heard back from the person that's supposed to be testing it on an NT machine so hopefully it's working properly.  I'll accept your answer.

0
dokkenAuthor Commented:
Barry,

Nope that code doesn't work on NT either, it requires rasapi32.dll.  I need some code that doesn't require anything that might not be installed.
0
rarigoCommented:
Hi dokken,

How about this code?

unit Unit1;

interface

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



const
 INTERNET_CONNECTION_MODEM           = 1;
 INTERNET_CONNECTION_LAN             = 2;
 INTERNET_CONNECTION_PROXY           = 4;
 INTERNET_CONNECTION_MODEM_BUSY      = 8;


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


function InternetGetConnectedState(lpdwFlags: LPDWORD; dwReserved: DWORD): BOOL;
         stdcall; external 'WININET.DLL';

var
  Form1: TForm1;

implementation
{$R *.DFM}

function  _IsConnectedToInternet: Boolean;
var
 dwConnectionTypes: Integer;
begin
 try
   dwConnectionTypes := INTERNET_CONNECTION_MODEM +
                        INTERNET_CONNECTION_LAN +
                        INTERNET_CONNECTION_PROXY;
   if InternetGetConnectedState(@dwConnectionTypes, 0) then
     Result := true
   else
     Result := false;
 except
   Result := false;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if _IsConnectedToInternet then Label1.Caption := 'true'
   else Label1.Caption := 'false';
end;

end.


Sinceramente,
Reginaldo

0
intheCommented:
you can also use the  compoenents here
http://www.rtfm.be/fpiette/indexuk.htm
 like wsocket or ping to check if online..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.