Delphi Source Code
Search engine
HOME Components Tutorials Add Trick Links Contacts
ActiveX Components Database Files Forms Graphic Internet/Lan Math Miscellaneous Multimedia Printing Strings System Information Windows

Delphi source code for Internet/Lan >> Check if I am connected to the internet


Category: Internet/Lan
Title: Check if I am connected to the internet
Author: www.swissdelphicenter.ch
Date added: 15.03.2006
Hits: 13170


interface
uses
  Windows, SysUtils, Registry, WinSock, WinInet;
type
  TConnectionType = (ctNone, ctProxy, ctDialup);
function ConnectedToInternet: TConnectionType;
function RasConnectionCount: Integer;
implementation
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;

Related Delphi Source Code:
Delphi Tricks
Delphi Tricks
For any problems or recommendations about Delphi Tricks site, please

feel free to contact us on that e-mail: support@delphitricks.com.
If you want to advertise on the site use that e-mail: advertise@delphitricks.com.

You can freely use or modify these Delphi source codes for non-commercial use. We are not responsible of any damages that can be caused by the utilisation of that source codes.

Copyright © 2006-2010 AVSoftware Company. All rights reserved.
Hide IP tricks