Fórum Como detectar internet...dúvida #288716
19/07/2005
0
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
Curtir tópico
+ 0Posts
19/07/2005
Massuda
Gostei + 0
19/07/2005
Adriano Santos
abraço
Gostei + 0
19/07/2005
Adriano Santos
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.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)