Internet - saber se esta conectada

Top  Previous  Next

///////////////////////////////// EXEMPLO 1/3

 

function FuncAvail(VLibraryname, VFunctionname: string; var VPointer: pointer): boolean; 

// 

// this function check if VFunctionname exists in VLibraryname 

// 

var 

Vlib: tHandle; 

begin 

Result := false; 

if LoadLibrary(PChar(VLibraryname)) = 0 then 

    exit

Vlib := GetModuleHandle(PChar(VLibraryname)); 

if Vlib <> 0 then 

begin 

   VPointer := GetProcAddress(Vlib, PChar(VFunctionname)); 

   if VPointer <> NIL then 

      Result := true; 

end

end

 

 

Code Button1 on a Form1: 

 

procedure TForm1.Button1Click(Sender: TObject); 

// 

// Call shell32.dll for highter Win98 

//       else call url.dll 

// 

var 

InetIsOffline : function(dwFlags: DWORD):BOOL; stdcall; 

begin 

if FuncAvail('url.dll''InetIsOffline', @InetIsOffline) then 

   if InetIsOffLine(0) then 

      ShowMessage('Not connected'

   else 

      ShowMessage('Connected!'); 

end

 

/////////////////////////////////////// EXEMPLO 2/3

 

...check if I am connected to the internet ?

 

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 <> 0and ( 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..4of 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 = 0or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns;  

  finally  

    FreeLibrary(RasDLL);  

  end;  

end

 

/////////////////////////////////////////// EXEMPLO 3/3

 

// Detalhe, este não roda em WindowsNT.

uses WinInet;

 

function NETConectada: Boolean;

const INTERNET_CONNECTION_MODEM=1;  // Conectado via modem

      INTERNET_CONNECTION_LAN=2// Conectado via rede

      INTERNET_CONNECTION_PROXY=4// Conectado via proxie

      INTERNET_CONNECTION_MODEM_BUSY=8// Modem ocupado com um trabalho não

internet

var dwConnectionTypes : DWORD;

begin

  dwConnectionTypes:=INTERNET_CONNECTION_MODEM+

                     INTERNET_CONNECTION_LAN+

                     INTERNET_CONNECTION_PROXY;

  Result:=InternetGetConnectedState(@dwConnectionTypes,0);

end;

 

 

///////////////////////////////////////// EXEMPLO 4/4

 

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 <> 0and (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..4of 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 = 0or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns; 

  finally 

    FreeLibrary(RasDLL); 

  end

end