Check Internet Connection on Delphi

 Today we will learn to check if the computer that our program is running is connected to the internet or not. I'm use delphi 7 for this.

The function will return True if the machine is connected to internet and False if its not.
uses
  ..., windows, wininet;
function IsInternetConnected: Boolean;
var
  Flags: Windows.DWORD;
begin
  Flags := 0;
  Result := WinInet.InternetGetConnectedState(@Flags, 0);
end;

You can use timer or at form onshow, for example let's using timer, Double click the TTimer (or Timer1) and enter the following code:

?
1
2
3
4
5
6
7
8
9
10
11
// credits: delphidabbler.com
procedure TForm1.Timer1Timer(Sender: TObject);
var
  Flags: Windows.DWORD;
begin
  Flags := 0;
  if (WinInet.InternetGetConnectedState(@Flags, 0)) then
    Label1.Caption:='Connected'
  else
    Label1.Caption:='Disconnected';
end;

Now scroll to the top of the code and add the two units in the uses clause:


add delphi library wininet at last:

?
1
2
uses
  ..., ..., windows, wininet;
Now Run the project (F9 or Run -> Run).


Now if the computer is connected to the internet, it will show "Connected". And if its not, the label will show "Disconnected".

Video on uploading

0 Response to "Check Internet Connection on Delphi"

Post a Comment