Seite 1 von 1
Internet Verbindung testen
Verfasst: 17.10.2004 05:21
von Hoto
Was ist die schnellste Möglichkeit zu testen ob man online ist? Also das er nicht ewig erst versucht irgendwo hin zu connecten, sondern das er ohne lange wartezeit rausfindet ob der rechner gerade am inet hängt.
Ich hab folgenden Code ausprobiert, allerdings funktioniert der irgendwie nicht, selbst wenn ich offline geh, bleibt da weiterhin "online" stehn. Könnte das vielleicht daran liegen das ich einen 2. PC per LAN angeschlossen hab?
Code: Alles auswählen
Repeat
If InternetGetConnectedState_(0, 0)
result$ = "Online"
Else
result$ = "Offline"
EndIf
Debug result$
Delay(1000)
Until q = 1
Die Funktion hab ich aus dem Codearchive, hab auch in der WinAPI etwas gestöbert aber irgendwie nix hingekriegt...
Mein kleines Programm soll, wenn der rechner nicht on ist, ständig in kurzen abständen testen ob eine verbindung ins inet aufgebaut wurde, danach soll er alle 5 mins eine html seite aus dem web auslesen. Das auslesen klappt wunderbar, nur wenn der pc eben nicht online ist, dann blockiert er beim versuch die html datei runterladen zu wollen den pc ewig lange (30 Sekunden oder so) und eben das will ich verhindern, so das er also nur die seite runterlädt, wenn er wirklich online ist.
Verfasst: 17.10.2004 13:01
von sbehrens
API-Hilfe über InternetGetConnectedState hat geschrieben:· lpdwFlags
[out] Pointer to an unsigned long integer variable where the connection description should be returned. This can be a combination of the following values:
INTERNET_CONNECTION_CONFIGURED
Local system has a valid connection to the Internet, but it may or may not be currently connected.
INTERNET_CONNECTION_LAN
Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM
Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY
No longer used.
INTERNET_CONNECTION_OFFLINE
Local system is in offline mode.
INTERNET_CONNECTION_PROXY
Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED
Local system has RAS installed.
Die Prozedure gibt anscheind nicht zurück, ob man connected ist oder nicht, sondern was für connections vorhanden sind. (Du könntest es vll. mit #Internet_Connection_Offline versuchen.)
mfG
Basti
//Edit:
API-Hilfe über InetIsOffline hat geschrieben:Returns TRUE if the local system in not currently connected to the Internet. Returns FALSE if the local system is connected to the Internet or if no attempt has yet been made to connect to the Internet.
also:
Verfasst: 17.10.2004 13:57
von PBZecke
InetIsOffline liefert mir immer 0, egal ob verbunden oder nicht.

Verfasst: 17.10.2004 14:22
von Hoto
Genau das Problem hab ich eben auch, irgendwie scheint es da keine wirklich zuverlässige Methode zu geben, jedenfalls nicht über eine einzelne WinAPI Funktion.
Verfasst: 17.10.2004 14:34
von PBZecke
Es hängt wahrscheinlich wirklich damit zusammen, dass Dein Rechner in einem Netzwerk hängt.
Ich habe aber einen guten Source der funktioniert (außer bei Proxy-Verbindung).
Das Problem: Ist ein Delphi-Source, und bin daran gescheitert den nach PB umzusetzen.
Code: Alles auswählen
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 <> 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;
Verfasst: 18.10.2004 12:01
von RolandIV
also eigentlich sollte man als Parameter Konstanten einsetzen für den Api Befhel InternetGetConnectedSTate(), aber diese KOnstanten kennt mein COmputer nicht , aber mit 0,0 als Argumente geht das eigntlich