分享
 
 
 

Delphi的拨号连接类

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

前一阵因为工作需要写了一个类来进行windows拨号,整理了一下,多封装了几个windows ras api,放上来大家提提意见。现在支持读取windows拨号连接列表、拨号、挂断、创建/删除连接,可以适用98/2000/XP,windows me 和NT没测试过,想来应该是可以的。以后有时间写成component,加入对拨号事件的支持。

uses

ras, Classes, SysUtils, StrUtils, Windows, Forms;

type

ERasError = Exception;

type

TRASConnection = class

private

FPlatForm: integer;

FConnected: Boolean;

FRasEntries: TStringList;

FRasConn: HRASCONN;

FParams: RasDialParams;

Ferrno: integer;

function GetPassword: string;

procedure SetPassword(Password: string);

function GetPhoneNo: string;

procedure SetPhoneNo(Number: string);

function GetCallBack: string;

procedure SetCallBack(Number: string);

function GetDomain: string;

procedure SetDomain(Domain: string);

function GetUserName: string;

procedure SetUserName(UserName: string);

function GetEntryName: string;

procedure SetEntryName(Entry: string);

function GetConnected: Boolean;

procedure SetConnected(AValue: Boolean);

function GetRasEntries: TStringList;

public

property RasEntries: TStringList read GetRasEntries;

property PhoneNumber: string read GetPhoneNo write SetPhoneNo;

property CallBackNumber: string read GetCallBack write SetCallBack;

property Domain: string read GetDomain write SetDomain;

property EntryName: string read GetEntryName write SetEntryName;

property username: string read GetUsername write SetUsername;

property password: string read GetPassword write SetPassword;

property Active: Boolean read GetConnected write SetConnected;

procedure Connect;

procedure DisConnect;

function GetErrorCode: integer;

procedure FreeAndHangup;

constructor Create; reintroduce;

destructor Destroy; override;

procedure CreateRasEntry;

procedure DeleteRasEntry(AEntryName: string);

//function GetErrorReason: integer;

end;

implementation

{ TRASConnection }

procedure TRASConnection.Connect;

var

i: integer;

s: string;

begin

FParams.dwSize := sizeof(RasDialParams);

i := RasDial(nil, nil, @FParams, 0, nil, @FRasConn);

if i <> 0 then begin

Ferrno := i;

case i of

691: s := '身分验证失败!';

692: s := '打开端口失败!';

676: s := '线路忙,请稍候再拨!';

677: s := '语音响应错误!';

678: s := '没有应答!';

679: s := '无载波信号!';

680: s := '无拨号音!';

else

s := '未知错误!';

end;

RasHangUp(FRasConn);

raise ERasError.Create(s);

end

else FConnected := True;

end;

procedure TRASConnection.DisConnect;

begin

RasHangup(FRasConn);

FRasConn := 0;

end;

function TRASConnection.GetCallBack: string;

begin

Result := string(FParams.szCallbackNumber);

end;

function TRASConnection.GetConnected: Boolean;

var

i, len, num: integer;

x: array of RASCONN;

begin

Result := False;

SetLength(x, 1);

x[0].dwSize := sizeof(RASCONN);

len := x[0].dwSize;

num := 0;

RasEnumConnections(@x[0], @len, @num);

if num > 0 then begin

SetLength(x, num);

x[0].dwSize := sizeof(RASCONN);

len := x[0].dwSize; num := 0;

RasEnumConnections(@x[0], @len, @num);

for i := 1 to num do

if StrComp(x[i - 1].szEntryName, PChar(EntryName)) = 0 then begin

FRasConn := x[i - 1].hrasconn;

Result := True;

Break;

end;

end;

SetLength(x, 0);

end;

function TRASConnection.GetDomain: string;

begin

Result := string(FParams.szDomain);

end;

function TRASConnection.GetErrorCode: integer;

begin

Result := FErrno;

end;

function TRASConnection.GetPassword: string;

begin

Result := '**********';

end;

function TRASConnection.GetPhoneNo: string;

begin

Result := string(FParams.szPhoneNumber);

end;

function TRASConnection.GetEntryName: string;

begin

Result := string(FParams.szEntryName);

end;

function TRASConnection.GetUserName: string;

begin

Result := string(FParams.szUserName);

end;

procedure TRASConnection.SetCallBack(Number: string);

begin

StrLCopy(FParams.szCallbackNumber, PChar(Number), sizeof(FParams.szCallbackNumber) - 1);

end;

procedure TRASConnection.SetConnected(AValue: Boolean);

begin

if AValue and not GetConnected then Connect

else if not AValue and GetConnected then DisConnect;

end;

procedure TRASConnection.SetDomain(Domain: string);

begin

StrLCopy(FParams.szDomain, PChar(Domain), sizeof(FParams.szDomain) - 1);

end;

procedure TRASConnection.SetPassword(Password: string);

begin

StrLCopy(FParams.szPassword, PChar(Password), sizeof(FParams.szPassword) - 1);

end;

procedure TRASConnection.SetPhoneNo(Number: string);

begin

StrLCopy(FParams.szPhoneNumber, PChar(Number), sizeof(FParams.szPhoneNumber) - 1);

end;

procedure TRASConnection.SetEntryName(Entry: string);

var

i: integer;

begin

for i := 0 to FRasEntries.Count - 1 do

if FRasEntries.Strings[i] = Entry then begin

StrCopy(FParams.szEntryName, PChar(Entry));

Exit;

end;

StrCopy(FParams.szEntryName, '');

end;

procedure TRASConnection.SetUserName(UserName: string);

begin

StrLCopy(FParams.szUserName, PChar(UserName), sizeof(FPArams.szUserName) - 1);

end;

procedure TRASConnection.FreeAndHangup;

begin

if Active then DisConnect;

Free;

end;

function TRASConnection.GetRasEntries: TStringList;

var

ren: array of RASEntryName;

ren98: array of RASEntryName98;

c, l: integer;

begin

FRasEntries.Clear;

c := 0;

case FPlatForm of

VER_PLATFORM_WIN32_WINDOWS:

begin

setlength(ren98, 1);

ren98[0].dwSize := sizeof(RASENTRYNAME98);

l := sizeof(RASENTRYNAME98);

if RasEnumEntries(nil, nil, @ren98[0], @l, @c) = ERROR_BUFFER_TOO_SMALL then begin

setlength(ren, c);

RasEnumEntries(nil, nil, @ren98[0], @l, @c);

end;

while c > 0 do begin

FRasEntries.Add(string(ren98[c - 1].szEntryName));

Dec(c);

end;

SetLength(ren98, 0);

end;

VER_PLATFORM_WIN32_NT:

begin

setlength(ren, 1);

ren[0].dwSize := sizeof(RASENTRYNAME);

l := sizeof(RASENTRYNAME);

if RasEnumEntries(nil, nil, @ren[0], @l, @c) = ERROR_BUFFER_TOO_SMALL then begin

setlength(ren, c);

RasEnumEntries(nil, nil, @ren[0], @l, @c);

end;

while c > 0 do begin

FRasEntries.Add(string(ren[c - 1].szEntryName));

Dec(c);

end;

SetLength(ren, 0);

end;

end;

if FRasEntries.Count>0 then EntryName:=FRasEntries.Strings[0];

Result := FRasEntries;

end;

constructor TRASConnection.Create;

var

OS: OSVersionInfoA;

begin

inherited;

OS.dwOSVersionInfoSize:=sizeof(OSVersionInfoA);

GetVersionEx(OS);

FPlatForm:=OS.dwPlatformId;

FRasEntries := TStringList.Create;

GetRasEntries;

end;

destructor TRASConnection.Destroy;

begin

FRasEntries.Free;

inherited;

end;

procedure TRASConnection.CreateRasEntry;

begin

RasCreatePhonebookEntry(Application.Handle,nil);

end;

procedure TRASConnection.DeleteRasEntry(AEntryName: string);

var

i: integer;

begin

i:=FRasEntries.IndexOf(AEntryName);

if i=-1 then Exit;

FRasEntries.Delete(i);

if AEntryName=EntryName then

if FRasEntries.Count>0 then EntryName:=FRasEntries.Strings[0]

else EntryName:='';

RasDeleteEntry(nil,PChar(AEntryName));

end;

Api声明:

function RasDial(

lpRasDialExtensions: PRASDIALEXTENSIONS;

lpszPhonebook: LPCTSTR;

lpRasDialParams: LPRASDIALPARAMS;

dwNotifierType: DWORD;

lpvNotifier: Pointer;

lphRasConn: LPHRASCONN): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasDialA';

function RasHangUp(rasconn: HRASCONN): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasHangUpA';

function RasGetEntryDialParams(

lpszPhonebook: LPCTSTR;

lprasdialparams: LPRASDIALPARAMS;

lpfPassword: LPBOOL): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasGetEntryDialParamsA';

function RasEnumEntries(

reserved: LPCTSTR;

lpszPhonebook: LPCTSTR;

lprasentryname: LPRASENTRYNAME;

lpcb: LPDWORD;

lpcEntries: LPDWORD): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasEnumEntriesA';

function RasEditPhonebookEntry(

hwnd: HWND;

lpszPhonebook: LPCTSTR;

lpszEntryName: LPCTSTR): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasEditPhonebookEntryA';

function RasGetEntryProperties(

lpszPhonebook: LPCTSTR;

lpszEntry: LPCTSTR;

lpRasEntry: LPRASENTRY;

lpdwEntryInfoSize: LPDWORD;

lpbDeviceInfo: PBYTE;

lpdwDeviceInfoSize: LPDWORD): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasGetEntryPropertiesA';

function RasSetEntryProperties(

lpszPhonebook: LPCTSTR;

lpszEntry: LPCTSTR;

lpRasEntry: LPRASENTRY;

dwEntryInfoSize: DWORD;

lpbDeviceInfo: PByte;

dwDeviceInfoSize: DWORD): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasSetEntryPropertiesA';

function RasEnumConnections(

lprasconn: LPRASCONN;

lpcb: LPDWORD;

lpcConnections: LPDWORD): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasEnumConnectionsA';

function RasEnumDevices(

lpRasDevInfo: LpRasDevInfo;

lpcb: LPDWORD;

lpcdevices: LPDWORD): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasEnumDevicesA';

function RasCreatePhonebookEntry(

Handle: Hwnd; LpszPhoneBook: PChar): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasCreatePhonebookEntryA';

function RasDeleteEntry(

lpszPhonebook: PChar;

lpszEntry: PChar): DWORD;

stdcall; external 'RASAPI32.dll' name 'RasDeleteEntryA';

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有