一位富翁以前写的获取当前的拨号连接,且可以挂断连接的控件。
用法:HangUpConn()挂断连接。
unit RAS;
//written by David Jiang in Oct. 24th 1999
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
const
RASCS_DONE = $2000;
RASCS_Connected = RASCS_DONE;
RASCS_Disconnected = RASCS_DONE + 1;
RAS_MaxDeviceType = 16;
{$IFDEF WINVER31}
{Version 3.x sizes }
RAS_MaxEntryName = 20;
RAS_MaxDeviceName = 32;
{$ELSE}
{Version 4.x sizes }
RAS_MaxEntryName = 256;
RAS_MaxDeviceName = 128;
{$ENDIF}
type
THRasConn = Longint;
TRasConnState = Integer;
LPRasConn = ^TRasConn;
TRasConn = record
dwSize: Longint;
hrasconn: THRasConn;
szEntryName: Array[0..RAS_MaxEntryName] of AnsiChar;
{$IFNDEF WINVER31}
szDeviceType: Array[0..RAS_MaxDeviceType] of AnsiChar;
szDeviceName: Array[0..RAS_MaxDeviceName] of AnsiChar;
{$ENDIF}
{$IFDEF WINVER41}
szPhonebook: Array[0..MAX_PATH - 1] of AnsiChar;
dwSubEntry: Longint;
{$ENDIF}
end;
TRasConnStatus = record
dwSize: Longint;
rasconnstate: TRasConnState;
dwError: LongInt;
szDeviceType: Array[0..RAS_MaxDeviceType] of AnsiChar;
szDeviceName: Array[0..RAS_MaxDeviceName] of AnsiChar;
{$IFDEF WINVER41}
swPhoneNumber: Array[0..RAS_MaxPhoneNumber] of AnsiChar;
{$ENDIF}
end;
function RasHangUp(hConn:THRasConn):Longint;stdcall;
external 'rasapi32.dll' name 'RasHangUpA';
function RasEnumConnections( rasconnArray: LPRasConn; var lpcb: Longint;
var lpcConnections: Longint ): Longint; stdcall;
external 'rasapi32.dll' name 'RasEnumConnectionsA';
function RasGetConnectStatus( hConn: THRasConn; var lpStatus: TRasConnStatus
): Longint; stdcall;
external 'rasapi32.dll' name 'RasGetConnectStatusA';
type
TRAS = class(TComponent)
private
{ Private declarations }
rasconn:array [0..15] of TRasConn;
FEnabled: Boolean;
timer:Ttimer;
FOndisconnected,FOnconnected: TNotifyEvent;
procedure Ontimer(sender:Tobject);
procedure SetOnconnected(Value: TNotifyEvent);
procedure SetOndisconnected(Value: TNotifyEvent);
procedure SetEnabled(Value: Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function HangUpConn(num:integer=0):boolean;
published
{ Published declarations }
property Onconnected: TNotifyEvent read FOnconnected write SetOnconnected;
property Ondisconnected: TNotifyEvent read FOndisconnected write SetOndisconnected;
property Enabled: Boolean read FEnabled write SetEnabled default True;
end;
procedure Register;
implementation
function TRAS.HangUpConn(num:integer=0):boolean;
begin
result:=RasHangUp(rasconn[num].hrasconn)=0;
end;
procedure TRAS.SetOndisconnected(value:TNotifyEvent);
begin
if assigned(FOndisconnected) then FOndisconnected(self);
FOndisconnected:=value;
end;
procedure Tras.SetOnconnected(value:TNotifyEvent);
begin
if assigned(FOnconnected) then FOnconnected(self);
FOnconnected:=value;
end;
procedure TRAS.SetEnabled(value:Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
if timer<>nil then
timer.Enabled :=value;
end;
end;
procedure TRAS.Ontimer(sender:Tobject);
var
return:dword;
size,num:integer;
i:integer;
connstatus:TRasConnStatus;
begin
size:=16*sizeof(TRasConn);
rasconn[0].dwsize:=sizeof(Trasconn);
num:=0;
return:=RasEnumConnections(@rasconn,size,num);
if return<>0 then exit;
for i:=0 to num-1 do
begin
connstatus.dwSize:=sizeof(Trasconnstatus);
if RasGetConnectStatus(rasconn.hrasconn,
connstatus)=0 then
case connstatus.rasconnstate of
RASCS_Connected:if assigned(FOnconnected)
then FOnconnected(sender);
RASCS_DisConnected:if assigned(FOndisconnected)
then FOndisconnected(sender);
end;
end;
end;
constructor TRAS.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
timer:=Ttimer.Create(self);
timer.OnTimer :=ontimer;
end;
destructor TRAS.Destroy;
begin
inherited Destroy;
end;
procedure Register;
begin
RegisterComponents('Samples', [TRAS]);
end;
end.