[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Detecting Internet connection...

Posted on 1999-12-20
15
Medium Priority
?
310 Views
Last Modified: 2010-04-06
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.  
0
Comment
Question by:dokken
  • 7
  • 5
  • 3
15 Comments
 

Expert Comment

by:rarigo
ID: 2296385
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
 
LVL 17

Accepted Solution

by:
inthe earned 100 total points
ID: 2296387
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
 
LVL 17

Expert Comment

by:inthe
ID: 2296388
or thatll work if you have tcp comp..
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 17

Expert Comment

by:inthe
ID: 2296398
actually another one of the safest methods is also too ping a known adress like www.borland.com 
0
 

Author Comment

by:dokken
ID: 2297930
Barry,

Is that component a .dcu file or is source code?
0
 
LVL 17

Expert Comment

by:inthe
ID: 2298516
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
 

Author Comment

by:dokken
ID: 2298580
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
 
LVL 17

Expert Comment

by:inthe
ID: 2298712
hi
i have sent .
as long as ras is installed it should be fine .let me know how it goes on nt.
cheers
0
 

Author Comment

by:dokken
ID: 2303634
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
 
LVL 17

Expert Comment

by:inthe
ID: 2303891
ok
0
 

Expert Comment

by:rarigo
ID: 2304358
Just for curiosity. What's wrong with my suggested way to check this?



Sinceramente,
Reginaldo
0
 

Author Comment

by:dokken
ID: 2310458
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
 

Author Comment

by:dokken
ID: 2312279
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
 

Expert Comment

by:rarigo
ID: 2312315
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
 
LVL 17

Expert Comment

by:inthe
ID: 2313018
you can also use the  compoenents here
http://www.rtfm.be/fpiette/indexuk.htm
 like wsocket or ping to check if online..
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question