Link to home
Start Free TrialLog in
Avatar of DanielManchester
DanielManchester

asked on

Internet connection

Using Delphi is there a way to determine if a client's machine has an internet connection?
ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ephraim Wangoya
You can also use WinInet functions, here is a simple example
uses WinInet;

function IsConnected(const Url: string): Boolean;
var
  NetHandle: HINTERNET;
  UrlHandle: HINTERNET;
begin
  Result := False;
  NetHandle := InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

  if Assigned(NetHandle) then 
  begin
    UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);

    if Assigned(UrlHandle) then
    begin
       Result := True;      
      InternetCloseHandle(UrlHandle);
    end;
    
    InternetCloseHandle(NetHandle);
  end;
end;

Open in new window


IsConnected('http://www.microsoft.com')
ewangoya has given me a new approach.  I think I might use a mix of the two.  As an example, being connected to your local network would be proven by ewangoya's example.  Being able to reach the end point would be proven by mine.  Both could be useful.
Implement WININET.DLL and call function InternetGetConnectedState(..)
unit Unit1;

interface

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

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

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

var
  Form1: TForm1;

implementation
{$R *.dfm}

uses WinInet;

const
  INET_CONN_MODEM = 1;
  INET_CONN_LAN = 2;
  INET_CONN_PROXY = 4;
  INET_CONN_MODEM_BUSY = 8;


function IsConnectedToInternet: Boolean;
var
  INetConnectionTypes: Integer;
begin
  try
    INetConnectionTypes := INET_CONN_MODEM + INET_CONN_LAN + INET_CONN_PROXY;
    Result:= InternetGetConnectedState(@INetConnectionTypes, 0);
  except
    Result := false;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  If IsConnectedToInternet Then
    ShowMessage('Connected to Internet!!!')
  Else
    ShowMessage('NOT connected to Internet!!!');
end;

end.

Open in new window