Como detectar internet...dúvida

Delphi

19/07/2005

Galera é o seguinte: A um tempo, atrás funçando na net, encontrei uma dica bem legal de como detectar conexão com internet. Beleza, a função funciona perfeitamente se for linha discada. Mas se for conexão banda larga ou via lan, ou seja, em uma rede corporativa onde, normalmente a internet é via proxy ou entra por servidor, a dica não funciona....

Já mexi pra caramba na dica e não consegui resolver o problema. Alguém já viu isso ou sabe como resolver para detectar internet em todas as ´instâncias´?

Código da Dica abaixo (completo).


...

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 para receber dados da conexao}
    var BufSize: DWord; { tamanho em bytes do buffer }
    var Connections: DWord { numero de conexoes escritas no buffer }
    ): LongInt; stdcall;


........


function TForm1.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
        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
    end;
  finally
    Free;
  end;
  if Result = ctNone then
  begin
    if RasConnectionCount > 0 then
      Result := ctDialup;
  end;

end;

function TForm1.RasConnectionCount: Integer;
var
  RasDLL: HInst;
  Conns: array[1..4] of TRasConn;
  RasEnums: TRasEnumConnections;
  BufSize: DWord;
  NumConns: DWord;
  RasResult: Longint;
begin
  Result := 0;
  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;

procedure TForm1.tm1Timer(Sender: TObject);
begin

  if RasConnectionCount > 0 then
  begin
    imgConectado.BringToFront;
    lblConectado.Caption := ´Conectado´;
  end
  else
  begin
    imgDesconectado.BringToFront;
    lblConectado.Caption := ´Desconectado´;
  end;
  //btnEnvio.Enabled := RasConnectionCount > 0;
end;



Adriano Santos

Adriano Santos

Curtidas 0

Respostas

Massuda

Massuda

19/07/2005

[url=http://forum.clubedelphi.net/viewtopic.php?t=62762]Este tópico[/url] mostra outra forma de detectar a conexão com a internet.


GOSTEI 0
Adriano Santos

Adriano Santos

19/07/2005

Valeu Massuda, vou testar aqui.

abraço


GOSTEI 0
Adriano Santos

Adriano Santos

19/07/2005

Cara, funcionou beleza.
Só não entendi para chamar a tela de conexão. A função só chama se estiver desconectado? Não pude testar essa parte sem internet, entende?

De qualquer forma, valeu.

... InternetAutodial(INTERNET_AUTODIAL_FORCE_ONLINE or INTERNET_AUTODIAL_FORCE_UNATTENDED, 0) ...



GOSTEI 0
POSTAR