If you are doing something that depends on an internet connection, you would probably want to check if internet is available. That's what we are going to detect today.
Today we will learn to check if the computer that our program is running is connected to the internet or not.
This is just like a code snippet. In fact I found this one accidentally when searching for something. I found delphidabbler.com which is made for delphi, but most of the things also apply to Lazarus/Free Pascal. Delphidabbler.com is a great website if you know what you are doing.
So the code I found is:
1
2
3
4
5
6
7
8
9
10
| uses ..., windows, wininet; function IsInternetConnected: Boolean ; var Flags: Windows . DWORD; // flags to pass to API function begin Flags := 0 ; Result := WinInet . InternetGetConnectedState(@Flags, 0 ); end ; |
The function will return True if the machine is connected to internet and False if its not.
(By the way, it uses windows unit. So you can guess that it will not work with any platforms other than Windows.)
We can use this code to build an example project.
Tutorial
Start Lazarus.Create a new Application Project (Project -> New Project -> Application -> OK).
Place a TLabel and a TTimer (from System tab). You can resize your form and customize it the way you like. My form looks like this:
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; // flags to pass to API function 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:
1
2
| uses ..., ..., windows, wininet;
|
http://lazplanet.blogspot.co.id/2014/10/find-out-if-computer-is-connected-to.html