Solved

Check If Connected

Posted on 2000-04-02
3
161 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 89
Delphi selector screen 2 78
control image tags in a string ? 12 129
how to resize animated Gif image in delphi ? 1 31
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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