Solved

Check If Connected

Posted on 2000-04-02
3
163 Views
Last Modified: 2010-04-04
How do I check if the system is connected to the internet and how do I get the IP? I want to add the function to a timer, and have it make a messagebox as soon as the person connects ;] Thanks!
0
Comment
Question by:nafa2221
3 Comments
 
LVL 1

Expert Comment

by:divnet
ID: 2677598
Hey nafa2221,

here is the complete source to a component that will tell you when your connected and the ip...i can't remember where i found this, and appologise to who ever actually wrote it. but it works great.

{ Save As OnlineIP.pas }
{ PROPERTIES:                                                
   IP: String - Current IP address of local machine          
   Online: Boolean - Online status of local machine          
   DispatchInterval - Determines in milliseconds the        
                      intervals of time between testing      
                      online of a mode of computer          
   Enabled: Boolean - As usual... =)                        
 EVENTS:                                                    
   OnChanged - causes if online status changed.              }

unit OnlineIP;

interface

uses
  Windows, Messages, Winsock, Classes, Forms;

type
  TOnlineIP = class(TComponent)
  private
    FEnabled: Boolean;
    FDispatchInterval: Cardinal;
    FWindowHandle: hWnd;
    FOnline: Boolean;
    FIP: String;
    FOnChanged: TNotifyEvent;

    procedure UpdateTimer;
    procedure SetEnabled(Value: Boolean);
    procedure SetDispatchInterval(Value: Cardinal);
    procedure SetNoneBool(Value: Boolean);
    procedure SetNoneStr(Value: String);
    procedure WndProc(var Msg: TMessage);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Enabled: Boolean read FEnabled write SetEnabled;
    property DispatchInterval: Cardinal read FDispatchInterval write SetDispatchInterval;
    property Online: Boolean read FOnline write SetNoneBool;
    property IP: String read FIP write SetNoneStr;
    property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
  end;

procedure Register;

implementation

function LocalIP: String;
type
  TaPInAddr = Array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: Array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := '';
  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
    Result := inet_ntoa(pptr^[I]^);
    Inc(I);
   end;
  WSACleanup;
end;

constructor TOnlineIP.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FEnabled := True;
  FDispatchInterval := 1000;
  FWindowHandle := AllocateHWnd(WndProc);
  UpdateTimer;
end;

destructor TOnlineIP.Destroy;
begin
  FEnabled := False;
  UpdateTimer;
  DeallocateHWnd(FWindowHandle);
  inherited Destroy;
end;

procedure TOnlineIP.WndProc(var Msg: TMessage);
var
  OldState: Boolean;
begin
  with Msg do
   if Msg = wm_Timer then
    try
     FIP := LocalIP;
     OldState := FOnline;
     FOnline := (FIP <> '') and (FIP <> '127.0.0.1');
     if (not OldState and FOnline) or
        (OldState and not FOnline) then
      if Assigned(FOnChanged) then FOnChanged(Self);
    except
     Application.HandleException(Self);
    end
   else
    Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;

procedure TOnlineIP.UpdateTimer;
begin
  KillTimer(FWindowHandle, 1);
  if (FDispatchInterval <> 0) and FEnabled then
   SetTimer(FWindowHandle, 1, FDispatchInterval, nil);
end;

procedure TOnlineIP.SetEnabled(Value: Boolean);
begin
  if Value <> FEnabled then
   begin
    FEnabled := Value;
    UpdateTimer;
   end;
end;

procedure TOnlineIP.SetDispatchInterval(Value: Cardinal);
begin
  if Value <> FDispatchInterval then
   begin
    FDispatchInterval := Value;
    UpdateTimer;
   end;
end;

procedure TOnlineIP.SetNoneBool(Value: Boolean); begin {} end;

procedure TOnlineIP.SetNoneStr(Value: String); begin {} end;

procedure Register;
begin
  RegisterComponents('Internet', [TOnlineIP]);
end;

end.

...Div
0
 

Author Comment

by:nafa2221
ID: 2677906
I'm new to delphi, can you tell me how to inster that code into my project and how to add it to a timers so I get a messagebox ass soon as the user connects....thanks ;]
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2677977
you must install the component not insert in in your project.
save the text above to a file called onlineip.pas.
(when saving i suggest saving it in a directory of its own under the delphi\libs\ directory).
open delphi click "component" - "install component" off the menu and click "into new package" tab then for "unit file name" browse to where the file was saved to. and give a package name also ("online" or something ..you can make up a name)
click "ok" and delphi ask you questions click ok or yes..
now you will have a new component on your internet component tab sheet.which you can drop on a form and on its onchanged event (double click in object inspector) and write:

if online
  then showmessage('your online');
................


i am thinking if you have network etc or with assigned ip address this component wont work anyway cause you ip will alrady be not 127.0.0.1

if the component dont work leave your email address as i have other componnet you should try.
and you can try another component from here also:

http://members.xoom.com/kirez/download.htm 



actually before installing components try this method:

//note i added Wininet to the uses section.

uses Wininet;

procedure TForm1.Button1Click(Sender: TObject);
var
   dwConnectionTypes: dWord;
begin
dwConnectionTypes := 0;
if InternetGetConnectedState(@dwConnectionTypes, 0) then
  showmessage('connected')
 else
  showmessage('not connected');
end;



0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question