Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Am I connected to the Internet?

Posted on 2002-03-26
3
Medium Priority
?
156 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

971 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