Jeremy,

Here's the code you're looking for. We use it in our MBS NetSaver
application which monitors DUN connections and calculates time and costs
online - I put in a plug just in case you were thinking of writing one :)
You'll need TRAS available from Delphi Super Page etc, email me if you can't
find it.

This will fill a stringlist with any active connections. I have a timer set
to an interval of 1000 but turn the timer off and back on just in case it
takes longer to poll the connections. I've edited it but you should be able
to make out what it does.


// public variables
  RasConn: Array[1..20] of TRasConn ;
  RasConn2: THRasConn ;

// in a timer do the following
// lbConnected is a listbox of active connections
procedure TMain.Timer1Timer(Sender: TObject);
var
  stat: TRasConnStatus;
begin
  Timer1.Enabled := False;
  RasConn2 := 0;
  if gSessionActive then
  begin
      GetRASConnections;
      if lbConnected.Items.Count = 0 then
        gSessionActive := False;
  end
  else
  begin
      GetRASConnections;
      // do we have a connection?
      if lbConnected.Items.Count > 0 then
      begin
          // check the state of the connection
          RasConn2 := RasConn[1].hrasconn;
          FillChar(stat, Sizeof(TRasConnStatus), 0);
          stat.dwSize := Sizeof(TRasConnStatus);
          RasGetConnectStatus(RasConn2, stat);
          // if we're connected then set the session active
          if stat.rasconnstate = RASCS_Connected then
          begin
              // we're connected
              gSessionActive := True;
          end;
      end;
  Timer1.Enabled := True;
end;

procedure TMain.GetRasConnections;
var
  i, x: DWord ;
  BufSize, Connections: Longint ;
begin
  lbConnected.Clear;
  RasConn[1].dwSize := SizeOf(RasConn[1]) ;
  BufSize := SizeOf(RasConn) ;
  x := RasEnumConnections(@RasConn, BufSize, Connections) ;
  if (x=0) or (x=ERROR_BUFFER_TOO_SMALL) then
    for i := 1 to Connections do
      if (i<21) and (RasConn[i].szEntryName[0] <> #0) then
        lbConnected.Items.Add(StrPas(RasConn[i].szEntryName)) ;
end ;

Regards,
Laurence Bevan
Master Business Systems Ltd
Web site: http://www.master.co.nz


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to