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?
Delphi

Avatar of undefined
Last Comment
Thommy

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
developmentguru

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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')
developmentguru

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.
Thommy

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy