2010. június 17., csütörtök

Check if the PC is connected to the Internet


Problem/Question/Abstract:

I just want to know a better way for checking an internet connection. One possible way is just to try to connect to a server with a TClientSocket or something like that. I can make this in a time interval. Is there a registry value or something like that which is changed if the computer goes online?

Answer:

Solve 1:

uses
  wininet

function Connected: boolean;
var
  flags: DWORD;
begin
  Flags := INTERNET_CONNECTION_MODEM or INTERNET_CONNECTION_LAN or
    INTERNET_CONNECTION_PROXY or INTERNET_CONNECTION_MODEM_BUSY;
  result := InternetGetConnectedState(@Flags, 0);
end;


Solve 2:

function TestIsOnline(var IPAddress: string): Boolean;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array[0..63] of Char;
  i: Integer;
  GInitData: TWSAData;
  IP: string;
begin
{$IFDEF OFFLINETEST}
  IPAddress := '127.0.0.1';
  Result := True;
{$ELSE}
  WSAStartup($101, GInitData);
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(Buffer);
  if phe = nil then
    Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  i := 0;
  while pPtr^[i] <> nil do
  begin
    IP := inet_ntoa(pptr^[i]^);
    Inc(i)
  end;
  WSACleanup;
  Result := (IP <> '') and (IP <> '127.0.0.1');
  if Result then
    IPAddress := IP
  else
    IPAddress := '';
{$ENDIF}
end;


Solve 3:

I use this with Delphi 6, but I think it works too with Delphi 2:

Other connection Value are

INTERNET_CONNECTION_MODEM = 1;
INTERNET_CONNECTION_LAN = 2;
INTERNET_CONNECTION_PROXY = 4;
INTERNET_CONNECTION_MODEM_BUSY = 8;

unit InternetConnected;

interface

uses
  Windows;

const
  INTERNET_CONNECTION_LAN = 2;

function InternetGetConnectedState(lpdwFlags: LPDWORD; dwReserved: DWORD): BOOL;
  stdcall;

implementation

uses
  SysUtils;

var
  winetdllHandle: THandle = 0;

const
  winetdll = 'wininet.dll';

function InternetGetConnectedState(lpdwFlags: LPDWORD; dwReserved: DWORD): BOOL;
  stdcall;
var
  fn_InternetGetConnectedState: function(lpdwFlags: LPDWORD; dwReserved: DWORD):
  BOOL; stdcall;
begin
  if (winetdllHandle = 0) then
    winetdllHandle := SafeLoadLibrary(winetdll);
  if (winetdllHandle <> 0) then
  begin
    @fn_InternetGetConnectedState := GetProcAddress(winetdllHandle,
      'InternetGetConnectedState');
    if (@fn_InternetGetConnectedState <> nil) then
      Result := fn_InternetGetConnectedState(lpdwFlags, dwReserved)
    else
      raise Exception.Create('Unable to locate function InternetGetConnectedState
        in library' + winetdll);
  end
  else
    raise Exception.Create('Unable to load library ' + winetdll);
end;

initialization

finalization
  try
    if (winetdllHandle <> 0) then
      FreeLibrary(winetdllHandle);
  except
  end;

end.


Solve 4:

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;

Nincsenek megjegyzések:

Megjegyzés küldése