Solved

Am I connected to the Internet?

Posted on 2002-03-26
3
139 Views
Last Modified: 2010-04-04
Using Delphi 3...

How can my D3 application detect whether the user is currently connected to the Internet or not?
0
Comment
Question by:Kapusta
  • 2
3 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6897994
What type of D3 are you using?  Developer / Profession / Client Server?  Do you have any internet controls?

Stu
0
 

Author Comment

by:Kapusta
ID: 6898013
I am using Delphi Professional.  As far as components, I have the Internet Mail Suite 1.x by Artchil Gogava.
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 6898103
Hi Kapusta,

Here is some code for you.

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

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

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
     ShowMessage('Yes')
   else
     ShowMessage('No');
end;

This works fine, however it doesn't detect an instant disconnect/connect.  I've found I have to retry it a couple of times before it realises that you're no longer connected to the net (this is the case with Lan anyway, not sure about modem).  This is a Windows thing, not a Delphi/Code thing.

Let me know if you need anymore help,

Cheers,

Stu
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now