Solved

Check If Connected

Posted on 2000-04-02
3
159 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now