Solved

Am I connected to the Internet?

Posted on 2002-03-26
3
142 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 122
Virtuailstring tree compare node issue 14 117
Delphi: how to implement a User Shortcut mapper? 1 110
enhance the following code 3 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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