[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

is the computer connected to the internet?

Posted on 2003-10-22
9
Medium Priority
?
290 Views
Last Modified: 2010-04-05
Hi, i would just like to know the complete code to check if the computer is connected to the internet through dial up or by all means? thanks..
0
Comment
Question by:orafans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Accepted Solution

by:
DaFox earned 172 total points
ID: 9603528
Hi orafans,

there's no reliable way to get the internet connection status if you don't ping a server (think of routers, proxies or other internet connections over lan, for example).
The InternetCheckConnection() API is able to ping a specified server to determinve online status. You should take a look at it.
If you're not satisfied with the function, try this unit:

unit BaclOnlineDetection;

interface

function IsIpPresent : boolean;
function IsOnlineRas : boolean;

// Description:
//      Call IsIeUsingOnlineMode to determin the "Work offline/online" status
//      of Internet Explorer. This function is not a reliable way of determining
//      if a user is connected to the Internet. Use IsUserOnline to check if a
//      user is connected or not.
//      Note: IsIeUsingOnlineMode always returns FALSE if installed version of
//      Internet Explorer is less than 4.0.
// Return Value:
//      TRUE if Internet Explorer is working in "online" mode, FALSE
otherwise.
// Summary:
//      IsIeUsingOnlineMode determins if Internet Explorer is in online mode or
//      not.
function IsIeUsingOnlineMode : boolean;

function IsUserOnline : boolean;

implementation

uses
  Windows,
  WinSock;

//------------------------------------------------------------------------------

function IsIpPresent : boolean;
type
  TApInAddr = array [0..10] of PInAddr;
  PApInAddr = ^TApInAddr;
var
  Phe           : PHostEnt;
  Pptr          : PApInAddr;
  Buf           : array [0..63] of char;
  i             : Integer;
  GInitData     : TWsaData;
  Ip            : string;
begin
  Result := FALSE;
  WsaStartup ($101, GInitData);
  GetHostName (Buf, SizeOf(Buf));
  Phe := GetHostByName (Buf);
  if Phe = nil then
    Exit;
  Pptr := PApInAddr(Phe^.h_addr_list);
  i := 0;
  while Pptr^[i] <> nil do
  begin
    Ip := inet_ntoa(Pptr^[i]^);
    Inc(i);
  end;
  WsaCleanup;
  Result := (Ip <> '') and (Ip <> '127.0.0.1');
end;

//------------------------------------------------------------------------------

function IsOnlineRas : boolean;
const
  REG_RAS_BASE = 'System\CurrentControlSet\Services\RemoteAccess';
  REG_RAS_RC   = 'Remote Connection';
var
  OpenKey       : HKEY;
  DataType,
  DataSize,
  Buf           : DWORD;
begin
  Result := FALSE;
  if RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_RAS_BASE, REG_OPTION_NON_VOLATILE, KEY_READ, OpenKey) = ERROR_SUCCESS then
  begin
    DataType := REG_BINARY;
    DataSize := SizeOf(Buf);
    if RegQueryValueEx (OpenKey, REG_RAS_RC, nil, @DataType, @Buf, @DataSize) = ERROR_SUCCESS then       Result := (Buf = 1);
    RegCloseKey (OpenKey);
  end;
end;

//------------------------------------------------------------------------------

function IsIeUsingOnlineMode : boolean;
var
  InetIsOffline         : function (dwFlags : DWORD) : bool; stdcall;
  HLib                  : THandle;

  function GetBasicOsVer : integer;
  var
    VerInfo     : TOsVersionInfo;
  begin
    VerInfo.dwOSVersionInfoSize := SizeOf(TOsVersionInfo);
    if GetVersionEx (VerInfo) then
    begin
      case VerInfo.dwPlatformId of
        VER_PLATFORM_WIN32_WINDOWS : Result := 1;
        VER_PLATFORM_WIN32_NT : Result := 2;
        else
          Result := 0;
      end;
    end
    else
      Result := 0;
  end;

  //
  // Win9x stores the InetIsOffline API in SHELL32.DLL while NT stores it in
  // URL.DLL
  //
  function GetProperLibHandle : THandle;
  begin
    case GetBasicOsVer of
      1 : Result := LoadLibrary ('SHELL32.DLL');
      2 : Result := LoadLibrary ('URL.DLL');
      else
        Result := INVALID_HANDLE_VALUE;
    end;
  end;

begin
  Result := FALSE;
  HLib := GetProperLibHandle;
  if HLib <> INVALID_HANDLE_VALUE then
    try
      @InetIsOffline := GetProcAddress (HLib, 'InetIsOffline');
      if @InetIsOffline <> nil then
        Result := not (InetIsOffline (0));
    finally
      FreeLibrary (HLib);
    end;
end;

//------------------------------------------------------------------------------

function IsUserOnline : boolean;
begin
  Result := IsIpPresent or IsOnlineRas or IsIeUsingOnlineMode;
end;

//------------------------------------------------------------------------------


end.

Regards,
Markus
0
 

Assisted Solution

by:DTHLIEM
DTHLIEM earned 164 total points
ID: 9604691
Hi orafans,
  Try this code:
********************************
interface  

uses  
  Windows, SysUtils, Registry, WinSock, WinInet;  

type  
  TConnectionType = (ctNone, ctProxy, ctDialup);  

function ConnectedToInternet: TConnectionType;  
function RasConnectionCount: Integer;  


implementation  

//For RasConnectionCount =======================  
const  
  cERROR_BUFFER_TOO_SMALL = 603;  
  cRAS_MaxEntryName       = 256;  
  cRAS_MaxDeviceName      = 128;  
  cRAS_MaxDeviceType      = 16;  
type  
  ERasError = class(Exception);  

  HRASConn = DWORD;  
  PRASConn = ^TRASConn;  
  TRASConn = record  
    dwSize: DWORD;  
    rasConn: HRASConn;  
    szEntryName: array[0..cRAS_MaxEntryName] of Char;  
    szDeviceType: array[0..cRAS_MaxDeviceType] of Char;  
    szDeviceName: array [0..cRAS_MaxDeviceName] of Char;  
  end;  

  TRasEnumConnections =  
    function(RASConn: PrasConn; { buffer to receive Connections data }  
    var BufSize: DWORD;    { size in bytes of buffer }  
    var Connections: DWORD { number of Connections written to buffer }  
    ): Longint;  
  stdcall;  
  //End RasConnectionCount =======================  


function ConnectedToInternet: TConnectionType;  
var  
  Reg:       TRegistry;  
  bUseProxy: Boolean;  
  UseProxy:  LongWord;  
begin  
  Result := ctNone;  
  Reg    := TRegistry.Create;  
  with REG do  
    try  
      try  
        RootKey := HKEY_CURRENT_USER;  
        if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet settings', False) then  
        begin  
          //I just try to read it, and trap an exception  
          if GetDataType('ProxyEnable') = rdBinary then  
            ReadBinaryData('ProxyEnable', UseProxy, SizeOf(Longword))  
          else  
          begin  
            bUseProxy := ReadBool('ProxyEnable');  
            if bUseProxy then  
              UseProxy := 1  
            else  
              UseProxy := 0;  
          end;  
          if (UseProxy <> 0) and (ReadString('ProxyServer') <> '') then  
            Result := ctProxy;  
        end;  
      except  
        //Obviously not connected through a proxy  
      end;  
    finally  
      Free;  
    end;  

  //We can check RasConnectionCount even if dialup networking is not installed  
  //simply because it will return 0 if the DLL is not found.  
  if Result = ctNone then  
  begin  
    if RasConnectionCount > 0 then Result := ctDialup;  
  end;  
end;  

function RasConnectionCount: Integer;  
var  
  RasDLL:    HInst;  
  Conns:     array[1..4] of TRasConn;  
  RasEnums:  TRasEnumConnections;  
  BufSize:   DWORD;  
  NumConns:  DWORD;  
  RasResult: Longint;  
begin  
  Result := 0;  

  //Load the RAS DLL  
  RasDLL := LoadLibrary('rasapi32.dll');  
  if RasDLL = 0 then Exit;  

  try  
    RasEnums := GetProcAddress(RasDLL, 'RasEnumConnectionsA');  
    if @RasEnums = nil then  
      raise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll');  

    Conns[1].dwSize := SizeOf(Conns[1]);  
    BufSize         := SizeOf(Conns);  

    RasResult := RasEnums(@Conns, BufSize, NumConns);  

    if (RasResult = 0) or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns;  
  finally  
    FreeLibrary(RasDLL);  
  end;  
end;  

*****************************

:)
0
 
LVL 3

Assisted Solution

by:ThoseBug
ThoseBug earned 164 total points
ID: 9605565
to know the type of conecction

procedure TForm1.Button1Click(Sender: TObject);
 
  function GetConexion:boolean;
  var
    flags: dword;
  begin
     Result := InternetGetConnectedState(@flags, 0);
    if Result then begin
     showmessage('Conexion On');
     if (flags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then
        showmessage('Conexion by modem');
     if (flags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
        showmessage('Conexion by lan');
     if (flags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
        showmessage('Conexion by proxy');
     if (flags and INTERNET_CONNECTION_MODEM_BUSY) =INTERNET_CONNECTION_MODEM_BUSY then
        showmessage('modem busy');
    end;
  end;
begin
  GetConexion;
end;
 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:ThoseBug
ID: 9605578
and this ti know if is conected

procedure TForm1.Button1Click(Sender: TObject);
 
  function FuncAvail(_dllname, _funcname: string; var _p: pointer): boolean;
 
  var _lib: tHandle;
  begin
    Result := false;
    if LoadLibrary(PChar(_dllname)) = 0 then exit;
    _lib := GetModuleHandle(PChar(_dllname));
    if _lib <> 0 then
      begin
        _p := GetProcAddress(_lib, PChar(_funcname));
        if _p <> nil then Result := true;
      end;
  end;
 
var
  InetIsOffline : function(dwFlags: DWORD): BOOL; stdcall;
 
begin
  if FuncAvail('URL.DLL', 'InetIsOffline', @InetIsOffline) = true then
    if InetIsOffLine(0) = true
      then ShowMessage('Offline')
        else ShowMessage('Online');
end;
 
0
 
LVL 3

Expert Comment

by:ThoseBug
ID: 9605603
you must be add in the uses WinInet
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9605791
@ThoseBug: See my first posting above. I already mentioned that function (including an example). The problem with this function is that it only returns a boolean if Internet Explorer (!!!) is in online mode. So this function won't return a reliable internet connection status in most cases!

Markus
0
 

Author Comment

by:orafans
ID: 9611364
Okey,
         i will rephrase my question.  actually i have a delphi program mapi
         but the problem with this one is that everytime i run the program and
         the computer is already connected to the internet it will disconnect    
         and prompt for a dialup connection.. all i want is that if it is
         connected to the internet it will just automatically connect..  
 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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