Como saber se estou conectado a internet

Delphi

24/02/2003

Como faço para saber ser o computador está conectado a internet, a rotina tem que ser segura, pois, já tentei algumas e se vejo meu ip quando estou desconectado pelo códiogo do dos ipconfig ele me retorna o seguinte ip 0.0.0.0 e se uso outros métodos dentro do delphi ele me apresenta mais ou menos assim 0.0.0.18 o que impede uma comparação.
Grato


Nuk3

Nuk3

Curtidas 0

Respostas

Carnette

Carnette

24/02/2003

Como saber se estou conectado à internet
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 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 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
//Nao conectado com proxy
end;
finally
Free;
end;

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;


GOSTEI 0
Odirlei

Odirlei

24/02/2003

Olá pessoal,

Carnette, será que vc (ou alguem) poderia exemplificar o uso da rotina acima?

É que sou iniciante e naum entendi muito bem.

Valew!


GOSTEI 0
Adriano Santos

Adriano Santos

24/02/2003

if RasConnectionCount then
label1.caption := ´conectado´
else
label1.caption := ´desconectado´


GOSTEI 0
POSTAR