这是我定义的TDialer类(用于拨号网络拨号)
type
TDialer = class(TObject)
private
hRasConn : THRASCONN;
DeviceType : array of string;//记录调制解调器的类型
DeviceName : array of string;//记录调制解调器的名称
DeviceNumber : Integer;//记录调制解调器的数量
PhoneBookName : array of string;//记录已存在的拨号网络名称
PhoneBookNumber : Integer; //记录已存在的拨号网络数量
//密码保护,将信息转换成指定数字
function Encrypt(S : String) : String;
//function Decrypt(S : String) : String;
function CrunchName(S : String) : String;
//读取拨号网络
function LoadPhoneBook : Boolean;
1. //拨号
function Dial(EntryName, UserName, Password : String):Boolean;
传入参数:EntryName:已存在的拨号网络名称
UserName:用户名
Password:密码
传出参数:result = true 拨号成功
resutl = False 拨号失败
2.//自动拨号
function AutoDial : Boolean;
传出参数result = true 拨号成功
resutl = False 拨号失败
3.//修改拨号网络属性
function DoProperties(AHandle:THandle;EntriesName : string) : Boolean;
传入参数:AHandle:THandle:应用程序句柄
EntriesName : string:拨号网络名称
传出参数result = true 成功
resutl = False 失败
4.//新建拨号网络
function NewConnection(AHandle:THandle) : Boolean;
传入参数:AHandle:THandle:应用程序句柄
传出参数result = true 成功
resutl = False 失败
5.//断开连接
procedure Disconnected;
6.//保存拨号信息
procedure SaveINIFile(sEntryName,sUserName,sPassword : String);
传入参数:sEntryName,拨号网络名称
sUserName,用户名
sPassword密码
7.//获得调制解调器信息
function GetModemInfo : Boolean;
8.//获得上网IP地址
function GetIPAddress : string;
传出参数:传出ip地址的字符串
9.//获得拨号网络名称
function GetPhoneName : string;
传出参数:传出字符串字符是以','隔开例如string (163,169,236,)
以','隔开每个拨号网络名称
10.//获得拨号网络数量
function GetPhoneNumber : Integer;
传出参数:拨号网络的个数
11.//获得调制解调器名称
function GetModemName : string;
传出字符串字符是以','隔开例如string (163,169,236,)
以','隔开每个调制解调器名称
12, //获得调制解调器数量
function GetModemNumner : Integer;
传出参数:调制解调器的个数
unit Dialer2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IniFiles, Ras, ExtCtrls,ShellApi;
type
TDialer = class(TObject)
private
hRasConn : THRASCONN;
DeviceType : array of string;//记录调制解调器的类型
DeviceName : array of string;//记录调制解调器的名称
DeviceNumber : Integer;//记录调制解调器的数量
PhoneBookName : array of string;//记录已存在的拨号网络名称
PhoneBookNumber : Integer; //记录已存在的拨号网络数量
//密码保护,将信息转换成指定数字
function Encrypt(S : String) : String;
//function Decrypt(S : String) : String;
function CrunchName(S : String) : String;
//读取拨号网络
function LoadPhoneBook : Boolean;
public
//拨号
function Dial(EntryName, UserName, Password : String):Boolean;
//自动拨号
function AutoDial : Boolean;
//修改拨号网络属性
function DoProperties(AHandle:THandle;EntriesName : string) : Boolean;
//新建拨号网络
function NewConnection(AHandle:THandle) : Boolean;
//断开连接
procedure Disconnected;
//保存拨号信息
procedure SaveINIFile(sEntryName,sUserName,sPassword : String);
//获得调制解调器信息
function GetModemInfo : Boolean;
//获得上网IP地址
function GetIPAddress : string;
//获得拨号网络名称
function GetPhoneName : string;
//获得拨号网络数量
function GetPhoneNumber : Integer;
//获得调制解调器名称
function GetModemName : string;
//获得调制解调器数量
function GetModemNumner : Integer;
//property PhoneBookName : array of string;
//property PhoneBookNumber : Integer;
function GetUserName(DialerName : string) : string;
function GetPassword(DialerName : string) : string;
end;
implementation
{* * * * * * * * * * * * * *拨号核心程序 * * * * * * * * * * * * * * * * * * * * * * * * *}
//
function TDialer.Dial(EntryName, UserName, Password : String):Boolean;
var
rdParams : TRASDIALPARAMS;
dwRet : DWORD;
Buf : array [0..255] of Char;
begin
Result := false;
// setup RAS Dial Parameters
FillChar(rdParams, SizeOf(rdParams), 0);
rdParams.dwSize := SizeOf(TRASDIALPARAMS);
strCopy(rdParams.szUserName, PChar(UserName));
strCopy(rdParams.szPassword, PChar(Password));
strCopy(rdParams.szEntryName, PChar(EntryName));
rdParams.szPhoneNumber[0] := #0;
rdParams.szCallbackNumber[0] := '*';
rdParams.szDomain := '*';
//g_hWnd := Handle;
hRasConn := 0;;
dwRet := RasDialA(nil, nil, @rdParams, 0, nil, @hRasConn);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
exit;
//Disconnected;
end;
result:=true;
end;
//断开连接
procedure TDialer.Disconnected;
begin
if hRasConn <> 0 then
begin
RasHangUpA(hRasConn);
hRasConn := 0;
end;
end;
//程序启动自动拨号
function TDialer.AutoDial : Boolean;
var
IniFile : TIniFile;
aEntryName : String;
aUserName : String;
aPassword : String;
EntryName : string;
UserName : string;
FIniFileName : String;
rdParams : TRASDIALPARAMS;
dwRet : DWORD;
Buf : array [0..255] of Char;
begin
Result := False;
FIniFileName := Copy(Application.ExeName, 1,
Length(Application.ExeName) - 3) + 'ini';
IniFile := TIniFile.Create(FIniFileName);
try
aEntryName := IniFile.ReadString('Last', 'EntryName', '');
aUserName := IniFile.ReadString('Last', 'UserName', '');
EntryName := CrunchName(Trim(aEntryName));
UserName := CrunchName(Trim(aUserName));
aPassword := IniFile.ReadString('RAS_ENTRY_' + EntryName,
'USER_' + UserName, '*');
finally
IniFile.Free;
end;
//Result := Dial(aEntryName,aUserName,aPassword);
FillChar(rdParams, SizeOf(rdParams), 0);
rdParams.dwSize := SizeOf(TRASDIALPARAMS);
strCopy(rdParams.szUserName, PChar(aUserName));
strCopy(rdParams.szPassword, PChar(aPassword));
strCopy(rdParams.szEntryName, PChar(aEntryName));
rdParams.szPhoneNumber[0] := #0;
rdParams.szCallbackNumber[0] := '*';
rdParams.szDomain := '*';
//g_hWnd := Handle;
hRasConn := 0;;
dwRet := RasDialA(nil, nil, @rdParams, 0, nil, @hRasConn);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
exit;
//Disconnected;
end;
Result := True;
end;
//读取拨号网络电话本
function TDialer.LoadPhoneBook : Boolean;
var
Entries : array [0..15] of TRASENTRYNAME;
cb : DWORD;
cEntries : DWORD;
dwRet : DWORD;
Buf : array [0..127] of char;
I : Integer;
begin
Result := False;
setlength(PhoneBookName,20);
FillChar(Entries, SizeOf(Entries), 0);
Entries[0].dwSize := SizeOf(TRASENTRYNAME);
cb := SizeOf(Entries);
cEntries := 0;
dwRet := RasEnumEntriesA(NIL, NIL, @Entries[0], @cb, @cEntries);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
exit; //LogMessage(Buf);
end
else
begin
Result := True;
PhoneBookNumber := cEntries;
for I := 0 to cEntries - 1 do
PhoneBookName := Entries.szEntryName;
end;
end;
function TDialer.GetPhoneName : string;
var
I : Integer;
begin
LoadPhoneBook;
Result:='';
for I := 0 to PhoneBookNumber -1 do
Result := Result+PhoneBookName + ',';
end;
//建立新的拨号网络连接
function TDialer.NewConnection(AHandle:THandle) : Boolean;
var
dwRet : DWORD;
Buf : Array [0..127] of char;
begin
Result := False;
//dwRet := RasCreatePhonebookEntryA(NewForm.Handle,nil);
dwRet := RasCreatePhonebookEntryA(AHandle,nil);
LoadPhoneBook;
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
exit;
//LogMessage(Buf);
end;
Result := True;
end;
//编辑修改拨号网络属性
//function DoProperties(NewForm : TForm; EntriesName : String) : Boolean;
function TDialer.DoProperties(AHandle:THandle;EntriesName : string) : Boolean;
var
dwRet : DWORD;
Buf : Array [0..127] of char;
begin
Result := False;
//dwRet := RasEditPhonebookEntryA(NewForm , nil, PChar(EntriesName));
dwRet := RasEditPhonebookEntryA(AHandle, nil, PChar(EntriesName));
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
//LogMessage(Buf);
end
else
Result := True;
end;
//获得调制解调器信息
function TDialer.GetModemInfo : Boolean;
var
dwRet : DWORD;
RasConn : array[0..15] of TRASCONN;
cb : DWORD;
cConnection : DWORD;
Buf : array [0..127] of char;
I : Integer;
begin
Result := True;
SetLength(DeviceName,20);
SetLength(DeviceType,20);
FillChar(RasConn, SizeOf(RasConn), 0);
RasConn[0].dwSize := SizeOf(TRASCONN);
cb :=Sizeof(RasConn);
cConnection := 0;
dwRet := RasEnumConnectionsA(@RasConn,@cb,@cConnection);
if dwRet<>0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
Result := False;
//LogMessage(Buf);
end;
DeviceNumber := cConnection;
for I:=0 to cConnection-1 do
begin
DeviceType:= RasConn.szDeviceType;
DeviceName:= RasConn.szDeviceName;
end;
end;
//获得上网连接的IP地址
function TDialer.GetIPAddress : string;
begin
result := RasGetIPAddress;
end;
//保存拨号上网信息
procedure TDialer.SaveINIFile(sEntryName,sUserName,sPassword : String);
var
IniFile : TIniFile;
FIniFileName : String;
EntryName : String;
UserName : String;
begin
FIniFileName := Copy(Application.ExeName, 1,
Length(Application.ExeName) - 3) + 'ini';
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteString('Last', 'EntryName', sEntryName);
IniFile.WriteString('Last', 'UserName', sUserName);
IniFile.WriteString('Last', 'Password', Encrypt(sPassword));
//if SavePWCheckBox.Checked then begin
// IniFile.WriteString('最后一次上网的信息', ' Password', EnCrypt(PasswordEdit.Text));
// IniFile.WriteString('最后一次上网的信息', 'SavePW', '1');
EntryName := CrunchName(Trim(sEntryName));
UserName := CrunchName(Trim(sUserName));
if (UserName <> '') and (EntryName <> '') then
IniFile.WriteString('RAS_ENTRY_' + EntryName,
'USER_' + UserName,
EnCrypt(sPassword))
//end
else begin
IniFile.WriteString('Last', 'Password', '');
IniFile.WriteString('Last', 'SavePW', '0');
end;
IniFile.Free;
end;
//
function TDialer.Encrypt(S : String) : String;
type
PWORD = ^WORD;
var
Len : Integer;
I : Integer;
V : DWORD;
P : PChar;
Buffer : String[255];
begin
Buffer := S;
Len := Length(Buffer) + 1;
if (Len mod 2) <> 0 then
Inc(Len);
if Len < 10 then
Len := 10;
I := Length(Buffer);
if I = 0 then
Buffer := IntToStr(GetTickCount)
else
while Length(Buffer) < 10 do
Buffer := Buffer + Buffer;
SetLength(Buffer, I);
Result := '';
P := PChar(@Buffer[0]);
for I := 1 to Len div 2 do begin
V := 34567 + PWORD(P)^;
P := P + 2;
Result := Result + Format('%5.5d', [V]);
end;
end;
{function TDialer.Decrypt(S : String) : String;
type
PWORD = ^WORD;
var
Buffer : String;
PW : String[255];
P : PWORD;
I : Integer;
V : Integer;
begin
PW := ' ';
P := PWORD(@PW[0]);
I := 1;
while I <= Length(S) do begin
Buffer := Copy(S, I, 5);
I := I + 5;
V := StrToInt(Buffer) - 34567;
P^ := V;
Inc(P);
end;
Result := PW;
end;}
function TDialer.CrunchName(S : String) : String;
var
I : Integer;
begin
Result := '';
for I := 1 to Length(S) do begin
case S of
' ' : Result := Result + '_';
'=' : Result := Result + '/-';
'@' : Result := Result + '/A';
'_' : Result := Result + '/_';
'/' : Result := Result + '//';
else
Result := Result + S;
end;
end;
end;
function TDialer.GetPhoneNumber: Integer;
begin
LoadPhoneBook;
Result:=PhoneBookNumber;
end;
function TDialer.GetModemName: string;
var
I : Integer;
begin
GetModemInfo;
Result := '';
for I := 0 to DeviceNumber -1 do
Result := Result + DeviceName + ',';
end;
function TDialer.GetModemNumner: Integer;
begin
GetModemInfo;
Result := DeviceNumber;
end;
function TDialer.GetUserName(DialerName : string): string;
var
iReturn : Integer;
RASParams : TRasDialParams;
lpfp : LongBool;
begin
//FillChar(RASParams,Sizeof(TRasDialParams),0);
RASParams.dwSize := Sizeof(TRasDialParams);
StrPCopy(RASParams.szEntryName,DialerName);
lpfp := False;
iReturn := RasGetEntryDialParamsA(nil,@RASParams,@lpfp);
result :=RASParams.szUserName;
end;
function TDialer.GetPassword(DialerName: string): string;
var
iReturn : Integer;
RASParams : TRasDialParams;
lpfp : LongBool;
begin
//FillChar(RASParams,Sizeof(TRasDialParams),0);
RASParams.dwSize := Sizeof(TRasDialParams);
StrPCopy(RASParams.szEntryName,DialerName);
lpfp := False;
iReturn := RasGetEntryDialParamsA(nil,@RASParams,@lpfp);
result :=RASParams.szPassword;
end;
end.