home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue56 / System / RASConnection.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-03-04  |  6.2 KB  |  183 lines

  1. unit RASConnection;
  2.  
  3. interface
  4.  
  5. uses
  6.     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, RASBase;
  7.  
  8. type
  9.     TRASConnectionManager = class (TRASBaseComponent)
  10.     private
  11.         { Private declarations }
  12.         fTimer: Integer;
  13.         fConnectionNames, fDummy1: TStrings;
  14.         fOnStatusChange: TNotifyEvent;
  15.         function GetStatus (Index: Integer): String;
  16.         function StatusChanged (NewStatus: TStringList): Boolean;
  17.     protected
  18.         { Protected declarations }
  19.     public
  20.         { Public declarations }
  21.         constructor Create (AOwner: TComponent); override;
  22.         destructor Destroy; override;
  23.         procedure Refresh;
  24.         property Status [Index: Integer]: String read GetStatus;
  25.     published
  26.         { Published declarations }
  27.         property ConnectionNames: TStrings read fConnectionNames write fDummy1 stored False;
  28.         property OnStatusChange: TNotifyEvent read fOnStatusChange write fOnStatusChange;
  29.     end;
  30.  
  31. procedure Register;
  32.  
  33. implementation
  34.  
  35. var
  36.     InstanceHack: TRASConnectionManager;
  37.  
  38. procedure TimerTickProc (Wnd: hWnd; Msg, Event, Time: Integer); stdcall;
  39. begin
  40.     InstanceHack.Refresh;
  41. end;
  42.  
  43. function ConStateToString (State: Integer): String;
  44. begin
  45.     case State of
  46.     $0000:      Result := 'Com Port Opening';
  47.     $0001:      Result := 'Com Port Opened';
  48.     $0002:      Result := 'Connecting Device';
  49.     $0003:      Result := 'Device Connected';
  50.     $0004:      Result := 'All Connected';
  51.     $0005:      Result := 'Starting Authenticate';
  52.     $0006:      Result := 'Authentication Event';
  53.     $0007:      Result := 'Retrying Authentication';
  54.     $0008:      Result := 'Callback number requested';
  55.     $0009:      Result := 'Password change request';
  56.     $000A:      Result := 'Projection Starting';
  57.     $000B:      Result := 'Calculating Link Speed';
  58.     $000C:      Result := 'Acknowledging Authentication Request';
  59.     $000D:      Result := 'Starting Reauthentication';
  60.     $000E:      Result := 'Successfully Authenticated';
  61.     $000F:      Result := 'Disconnecting for callback';
  62.     $0010:      Result := 'Resetting modem for callback';
  63.     $0011:      Result := 'Waiting for callback';
  64.     $0012:      Result := 'Projection complete';
  65.     $0013:      Result := 'Authenticating user';
  66.     $0014:      Result := 'Callback complete';
  67.     $0015:      Result := 'Logging onto network';
  68.     $0016:      Result := 'Subentry connected';
  69.     $0017:      Result := 'Subentry disconnected';
  70.     $1000:      Result := 'Terminal State';
  71.     $1001:      Result := 'Retrying Authentication';
  72.     $1002:      Result := 'Callback set by user';
  73.     $1003:      Result := 'Password has expired';
  74.     $1004:      Result := 'Paused for EAPUI';
  75.     $2000:      Result := 'Successful connection';
  76.     $2001:      Result := 'Failed connection';
  77.     else        Result := 'Unknown';
  78.     end;
  79. end;
  80.  
  81. constructor TRASConnectionManager.Create (AOwner: TComponent);
  82. begin
  83.     // Yes, it's dirty, but it's also quick. ;-)
  84.     if InstanceHack <> Nil then Exception.Create ('Only one TRASConnectionManager allowed');
  85.     Inherited Create (AOwner);
  86.     InstanceHack := Self;
  87.     fConnectionNames := TStringList.Create;
  88.     Refresh;
  89.     fTimer := SetTimer (0, 999, 1000, @TimerTickProc);
  90. end;
  91.  
  92. destructor TRASConnectionManager.Destroy;
  93. begin
  94.     if fTimer <> 0 then KillTimer (0, fTimer);
  95.     fConnectionNames.Free;
  96.     Inherited;
  97.     InstanceHack := Nil;
  98. end;
  99.  
  100. function TRASConnectionManager.StatusChanged (NewStatus: TStringList): Boolean;
  101. var
  102.     Idx: Integer;
  103. begin
  104.     Result := fConnectionNames.Count <> NewStatus.Count;
  105.     if not Result then begin
  106.         Result := True;
  107.         for Idx := 0 to fConnectionNames.Count - 1 do begin
  108.             if fConnectionNames [Idx] <> NewStatus [Idx] then Exit;
  109.             if fConnectionNames.Objects [Idx] <> NewStatus.Objects [Idx] then Exit;
  110.         end;
  111.  
  112.         Result := False;
  113.     end;
  114. end;
  115.  
  116. procedure TRASConnectionManager.Refresh;
  117. type
  118.     TRasConn = record
  119.         dwSize: DWord;
  120.         hConn: THandle;
  121.         EntryName: array [0..20] of Char;
  122.     end;
  123.  
  124.     TRasConStatus = record
  125.         dwSize: DWord;
  126.         ConState: Integer;
  127.         Error: Integer;
  128.         DeviceType: array [0..16] of Char;
  129.         DeviceName: array [0..32] of Char;
  130.     end;
  131.  
  132. var
  133.     CurCon: ^TRasConn;
  134.     NewStatus: TStringList;
  135.     Status: TRasConStatus;
  136.     Buffer: array [0..10000] of Char;
  137.     Idx, BufSize, NumConnections: Integer;
  138.     RasEnumConnections: function (Buffer: PChar; var BufSize, NumConnections: Integer): Integer; stdcall;
  139.     RasGetConnectStatus: function (hConn: THandle; var Status: TRasConStatus): Integer; stdcall;
  140. begin
  141.     if Available then begin
  142.         RasEnumConnections := GetProc ('RasEnumConnectionsA');
  143.         RasGetConnectStatus := GetProc ('RasGetConnectStatusA');
  144.         if Assigned (RasEnumConnections) and Assigned (RasGetConnectStatus) then begin
  145.             NewStatus := TStringList.Create;
  146.             try
  147.                 CurCon := @Buffer;
  148.                 CurCon^.dwSize := sizeof (TRasConn);
  149.                 BufSize := sizeof (Buffer);
  150.                 if CallProc (RasEnumConnections (Buffer, BufSize, NumConnections)) then
  151.                     for Idx := 0 to NumConnections - 1 do begin
  152.                         Status.dwSize := sizeof (Status);
  153.                         if CallProc (RasGetConnectStatus (CurCon^.hConn, Status)) then
  154.                             NewStatus.AddObject (CurCon^.EntryName, TObject (Status.ConState));
  155.                         Inc (CurCon);
  156.                     end;
  157.  
  158.                 // Now see if anything has changed
  159.                 if StatusChanged (NewStatus) then begin
  160.                     fConnectionNames.Assign (NewStatus);
  161.                     if Assigned (OnStatusChange) then OnStatusChange (Self);
  162.                 end;
  163.             finally
  164.                 NewStatus.Free;
  165.             end;
  166.         end;
  167.     end;
  168. end;
  169.  
  170. function TRASConnectionManager.GetStatus (Index: Integer): String;
  171. begin
  172.     Result := '';
  173.     if Index < fConnectionNames.Count then
  174.         Result := ConStateToString (Integer (fConnectionNames.Objects [Index]));
  175. end;
  176.  
  177. procedure Register;
  178. begin
  179.     RegisterComponents ('DelphiMag', [TRASConnectionManager]);
  180. end;
  181.  
  182. end.
  183.