Solved

Check for ANY KIND of internet connection. (ANY INTERNET CONNECTION)

Posted on 2006-07-11
17
911 Views
Last Modified: 2010-08-05
Dear Experts,

I am using Delphi 7.
I want to check for any kind of internet connection, whether one exist or not.
For example but not limited:
Dialup
ADLS
Broadband via LAN.
etc.

I need the quickest way to check.

I do have code which check for internet connection but it do not detect a broadband connection via LAN.

Thanks in advance!
0
Comment
Question by:Marius0188
  • 6
  • 5
  • 3
  • +2
17 Comments
 

Author Comment

by:Marius0188
ID: 17083737
Oh and another thing.

If detected that there are no connection whatsoever to the internet I want to display the user's default internet connection dialog.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 17084757
function GetConnectionType: integer;
var
  dwConnectionTypes: DWORD;
begin
  dwConnectionTypes := INTERNET_CONNECTION_MODEM;
  if InternetGetConnectedState(@dwConnectionTypes, 0) then
     result := 1
  else
  begin
     dwConnectionTypes := INTERNET_CONNECTION_LAN + INTERNET_CONNECTION_PROXY;
     if InternetGetConnectedState(@dwConnectionTypes, 0) then
        result := 2
     else
        result := 0;
  end;
end;

procedure TForm1.Button1Click(Sender:TObject);
begin
If GetConnectionType=0 then ShowMessage('No internet connection available')
else
If GetConnectionType=1 then ShowMessage('Dial-up internet connection available')
else
If GetConnectionType=2 then ShowMessage('Broadband internet connection available');
end;
0
 
LVL 11

Expert Comment

by:calinutz
ID: 17084790
And about showing the Internet options (I suppose that is what you want to show in case there is no internet connection available....) check this PAQ:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20934635.html


Regards
0
 

Author Comment

by:Marius0188
ID: 17086468
1. Will the code example work for a permanent (line or broadband) connection?
2. When there is no connection available I would like to show the user's default connection dialog to dial if it's not a permanent line.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 17088747
"1. Will the code example work for a permanent (line or broadband) connection?"
can't you just test it and find out?

"2. When there is no connection available I would like to show the user's default connection dialog to dial if it's not a permanent line."
What exactly do you mean by default connection dialog? doesn't my comment answer your question with that PAQ?

PS: Esti roman cumva?
0
 

Author Comment

by:Marius0188
ID: 17090522
1. It nearly impossible to test for all different line in my home country.
I doubt it will be possible to test for all different infrustructures around the glode except if you can provide me with all the different connections at one given place. :)

2.  Will the code in your PAQ show me the internet connection prompt? For example:
     I have a internet connection set up then you when you want to dial it there is a form / window with
     a button "Dial". I want to display the default internet connection's Dial window (or whatever it's called).
   
0
 
LVL 1

Expert Comment

by:Tueblo
ID: 17094851
The problem is that the information wether there is a permanent connection to the internet does not depend on devices in your computer, like network cards and the like. What if you have a good connection to the next router, but the next hop router has been switched off?

The best method to check if there is an active link to the internet would be sending a ping to a prominent host in my opinion. That means if a ping to e.g. google.com is successful, the computer is most likely connected to the internet. A ping function is implemented in the IPHelper API of Windows (IcmpSendEcho). I could give an example, if you plan to do so.

I think displaying the dial window can be realised by calling the Windows program. I can not just test it on my computer, because I have no dial up connection. But it should be rasphone.exe in the system32 folder. Calling it should be possible by using ShellExecute from the ShellAPI unit.

Regards !
Tueblo
0
 
LVL 16

Assisted Solution

by:CodedK
CodedK earned 50 total points
ID: 17097706
Hi.
Calinutz code works fine.

Here is a smaller version to check if user is online :

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
function IsUserOnline:boolean;
var
connect_status:dword;
begin
connect_status := 2 {user uses a lan}    +
                           1 {user uses a modem.} +
                           4 {user uses a proxy}  ;
result := InternetGetConnectedState(@connect_status,0);
end;
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

To call a number so you can connect since its not a broadband connection use this :
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
function tapiRequestMakeCall(DestAddress, AppName,
 CalledParty, Comment: PChar): Longint; stdcall; external 'TAPI32.DLL';

procedure TForm1.Button1Click(Sender: TObject);
var
 PhoneNumber, AppName: array[0..255] of Char;
begin
 PhoneNumber := '1234567890';
 StrPCopy(AppName, Application.Title);
 tapiRequestMakeCall(PhoneNumber, AppName, '', '');
end;
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

OR

Startup a dial connection (another way) :
http://www.swissdelphicenter.ch/torry/showcode.php?id=1943

Hope this helps
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Accepted Solution

by:
HillGroover earned 75 total points
ID: 17116304
Hi Marius0188,

This should be all that's needed to do what you want.
Don't forget to add "WinInet" to the uses clause

procedure TForm1.Button1Click(Sender: TObject);
var
   connect_status: dword;
begin
   connect_status := 2 + 4 + 1;
   InternetDial(0, nil, INTERNET_CUSTOMDIAL_CONNECT, @connect_status, 0);
end;

This doesn't bother telling you if you're connected or not, if you're connected nothing happens, if you're not connected then you'll see the connection dialog like you want.

Regards
0
 
LVL 16

Expert Comment

by:CodedK
ID: 17116730
HillGroover whtathe difference from my code?
0
 
LVL 4

Expert Comment

by:HillGroover
ID: 17117382
the main problem is this function here...
----------------------------------------------------------------------
function IsUserOnline:boolean;
var
connect_status:dword;
begin
connect_status := 2 {user uses a lan}    +
                           1 {user uses a modem.} +
                           4 {user uses a proxy}  ;
result := InternetGetConnectedState(@connect_status,0);
end;
----------------------------------------------------------------------
It doesn't work if you're connected to just another computer with no internet connection The same code but not in a function, the way i've shown, works for some reason. I used that function for years until last night when i tried it and it told me i was connected yet all i was connected to at the time was my nephews laptop and there was no internet connection so i started to play around with it.

The code you've used for calling numbers uses the phone dialer, it doesn't display the default connection dialog. ADSL connections still need to use a dial up connection, it's cable connections that don't. The code in the link you provided doesn't display the defaul connection dialog either.

you could use my code to get a 14kb exe file like so, if you change dword to integer to avoid having to use the windows unit...
---------------------------
program Project1;

uses
  WinInet;

var
  connect_status: integer;

begin
  connect_status := 2 + 4 + 1;
  InternetDial(0, nil, INTERNET_CUSTOMDIAL_CONNECT, @connect_status, 0);
end.
---------------------------

Regards
HillGroover
0
 

Author Comment

by:Marius0188
ID: 17127398
Hi Tueblo,

Can you please show me example of sending a ping command in Delphi?
I will appreciate it.

Thanks!
0
 
LVL 4

Expert Comment

by:HillGroover
ID: 17127805
0
 

Author Comment

by:Marius0188
ID: 17130442
I have read something about administrative rights are required on XP and NT (2000)  in order to use low level network service for example: ping?

Is this true?

0
 
LVL 4

Expert Comment

by:HillGroover
ID: 17134217
This is not the case, any user can use the ping command.
0
 

Author Comment

by:Marius0188
ID: 17134886
Ok but the following example from about.com does not work:

uses Ping;
...
const ADP_IP = '208.185.127.40'; (* http://delphi.about.com *)
begin
 If  Ping.Ping(ADP_IP) then
   ShowMessage('About Delphi Programming reachable!');
end;


I have add ping to uses clause but "ping.ping()" does not exist?
0
 
LVL 4

Expert Comment

by:HillGroover
ID: 17134983
you'll need to copy the source for the unit ping.pas

give it a different name from ping.pas as the function "ping" will through up and error saying indetifier redeclared.

i've included it below as aping.pas for you, save that in the project directory or on your delphi path somewhere and add "aping" to the uses clause and that function should work fine

delphi.about.com most likely doesn't point to that ip address anymore either (it didn't when i tested it so i would change the const ADP_IP to

const ADP_IP = 'http://about.delphi.com' ;
of
const ADP_IP = 'www.google.com';

//-------------------------------------------------
unit aping;

interface
uses
  Windows, SysUtils, Classes;

type
  TSunB = packed record
    s_b1, s_b2, s_b3, s_b4: byte;
  end;

  TSunW = packed record
    s_w1, s_w2: word;
  end;

  PIPAddr = ^TIPAddr;
  TIPAddr = record
    case integer of
      0: (S_un_b: TSunB);
      1: (S_un_w: TSunW);
      2: (S_addr: longword);
  end;

 IPAddr = TIPAddr;

function IcmpCreateFile : THandle; stdcall; external 'icmp.dll';
function IcmpCloseHandle (icmpHandle : THandle) : boolean;
            stdcall; external 'icmp.dll'
function IcmpSendEcho
   (IcmpHandle : THandle; DestinationAddress : IPAddr;
    RequestData : Pointer; RequestSize : Smallint;
    RequestOptions : pointer;
    ReplyBuffer : Pointer;
    ReplySize : DWORD;
    Timeout : DWORD) : DWORD; stdcall; external 'icmp.dll';


function Ping(InetAddress : string) : boolean;

implementation

uses
  WinSock;

function Fetch(var AInput: string;
                      const ADelim: string = ' ';
                      const ADelete: Boolean = true)
 : string;
var
  iPos: Integer;
begin
  if ADelim = #0 then begin
    // AnsiPos does not work with #0
    iPos := Pos(ADelim, AInput);
  end else begin
    iPos := Pos(ADelim, AInput);
  end;
  if iPos = 0 then begin
    Result := AInput;
    if ADelete then begin
      AInput := '';
    end;
  end else begin
    result := Copy(AInput, 1, iPos - 1);
    if ADelete then begin
      Delete(AInput, 1, iPos + Length(ADelim) - 1);
    end;
  end;
end;

procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
var
  phe: PHostEnt;
  pac: PChar;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  try
    phe := GetHostByName(PChar(AIP));
    if Assigned(phe) then
    begin
      pac := phe^.h_addr_list^;
      if Assigned(pac) then
      begin
        with TIPAddr(AInAddr).S_un_b do begin
          s_b1 := Byte(pac[0]);
          s_b2 := Byte(pac[1]);
          s_b3 := Byte(pac[2]);
          s_b4 := Byte(pac[3]);
        end;
      end
      else
      begin
        raise Exception.Create('Error getting IP from HostName');
      end;
    end
    else
    begin
      raise Exception.Create('Error getting HostName');
    end;
  except
    FillChar(AInAddr, SizeOf(AInAddr), #0);
  end;
  WSACleanup;
end;

function Ping(InetAddress : string) : boolean;
var
 Handle : THandle;
 InAddr : IPAddr;
 DW : DWORD;
 rep : array[1..128] of byte;
begin
  result := false;
  Handle := IcmpCreateFile;
  if Handle = INVALID_HANDLE_VALUE then
   Exit;
  TranslateStringToTInAddr(InetAddress, InAddr);
  DW := IcmpSendEcho(Handle, InAddr, nil, 0, nil, @rep, 128, 0);
  Result := (DW <> 0);
  IcmpCloseHandle(Handle);
end;

end.
//-------------------------------------------------


Regards
HillGroover
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now