unit powerdial;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
rasDialExtensions = record
dwSize,dwfOptions,hwndParent,reserved: integer;
end;
lpRasDialExtensions = ^rasDialExtensions;
rasDialParams = record
dw_size: Cardinal;
szEntryName:Array [0..256] of char;//string;
szPhoneNumber:Array [0..128] of char;//string;
szCallBackNumber:Array [0..128] of char;// string;
szUserName:Array [0..256] of char;//string;
szPassword:Array [0..256] of char;//string;
szDomain:Array [0..15] of char;// string;
end;
lprasDialParams = ^rasDialParams;
LPRASCONNSTATUS =^RASCONNSTATUS ;
RASCONNSTATUS = record
dw_size : Cardinal;
rasconnstate : integer;
dw_Error : Cardinal;
szDeviceType : Array [0..32] of char;
szDeviceName : Array [0..16] of char;
end;
function RasDialW(lpoRasDialExtensions: lprasDialExtensions;
lpszPhonebook: pChar; lpoRasDialParams: lprasDialParams;
dwNotifierType: integer; lpvNotifier: integer;
var lphRasConn: cardinal): integer;stdcall;external 'rasapi32.dll';
function RasDialA(lpoRasDialExtensions: lprasDialExtensions;
lpszPhonebook: pChar; lpoRasDialParams: lprasDialParams;
dwNotifierType: Cardinal; lpvNotifier: Cardinal;
var lphRasConn: cardinal): integer;stdcall;external 'rasapi32.dll';
Function RasHangUpW(lphRasConn : Cardinal): integer;stdcall;external 'rasapi32.dll';
function RasGetConnectStatusA(hrasconn:cardinal;
var rasconnstatus:RASCONNSTATUS): integer;stdcall;external 'rasapi32.dll';
function RasGetErrorStringW(errcode :integer;
Var errstring
char;
errlong:integer):integer;stdcall;external 'rasapi32.dll';
function ShowStatusString(param: Cardinal): string;
function rasdialconnect(DialEntry:string;
UserName:string;
UserPass:string;
Var dialhandle:Cardinal;
Var Dialerrorcode : integer): boolean;
Function powerhangup(DialHandle : Cardinal;
Var Dialerrorcode : integer):Boolean;
implementation
function ShowStatusString(param: Cardinal): string;
var
ls_Message: string;
begin
case param of
0:
ls_Message := '打开端口';//'Open Port';
1:
ls_Message := '端口已打开';//'Port Opened';
2:
ls_Message := '连接设备';//'Connect Device';
3:
ls_Message := '设备已连接';//'Device Connected';
4:
ls_Message := '所有设备已连接';//'All Devices Connected';
5:
ls_Message := '验证身份';//'Authenticate';
6:
ls_Message := '身份验证';//'Auth. Notify';
7:
ls_Message := '重新验证身份';//'Auth. Retry' ;
8:
ls_Message := '验证身份响应';//'Auth. Callback';
9:
ls_Message := '修改口令';//'Auth. Change Password';
10:
ls_Message := '注册网络';//'Auth. Project';
11:
ls_Message := '认证连接速度';//'Auth. Link Speed';
12:
ls_Message := 'Auth. Ack';
13:
ls_Message := '重新认证';//'ReAuthenticate';
14:
ls_Message := '身份已验证';//'Authenticated';
15:
ls_Message := 'Prepare For Callback';
16:
ls_Message := '等待调制解调器复位';//'Wait For Modem Reset';
17:
ls_Message := '等待响应';//'Wait For Callback';
8192:
ls_Message := '连接成功';//'Connected!';
end;
ShowStatusString := ls_Message;
end;
function rasdialconnect(DialEntry:string;
UserName:string;
UserPass:string;
Var dialhandle:Cardinal;
Var Dialerrorcode : integer): boolean;
var
Pdialparams: rasDialParams;
Pdialstatus : RASCONNSTATUS;
cc: cardinal;
s_ok: integer;
begin
Dialerrorcode := 0;
Pdialparams.dw_size := 1052;
Strcopy( Pdialparams.szEntryName ,Pchar( DialEntry));//PChar('ChinaNet');
Pdialparams.szPhoneNumber := '';//PChar('163');
Pdialparams.szCallBackNumber := '';//PChar('4892803');
strcopy( Pdialparams.szUserName ,Pchar(UserName));//PChar('jschem1');
strcopy( Pdialparams.szPassword ,Pchar(UserPass));//PChar('7365');
Pdialparams.szDomain := '';
DialHandle := 0;
cc := 2147483647;
Pdialstatus.dw_size := sizeof(RASCONNSTATUS);
DialErrorCode := RasDialA(nil,nil,@Pdialparams,cc,0,DialHandle);
if DialErrorCode = 0 Then
begin
Pdialstatus.dw_size := sizeof(RASCONNSTATUS);
s_ok := RasGetConnectStatusA(DialHandle,Pdialstatus);
while (Pdialstatus.dw_Error = 0 )and (Pdialstatus.RasConnstate <> 8192)
and (s_ok <> 6) do
begin
s_ok := RasGetConnectStatusA(dialhandle,Pdialstatus);
// Es.Text := ShowStatusString(temps.RasConnstate);
Application.ProcessMessages;
end;
If Pdialstatus.RasConnstate = 8192 Then
begin
rasdialconnect := True;
exit;
end;
end;
//Es.text := '连接失败!';
rasdialconnect := False;
end;
Function powerhangup(DialHandle : Cardinal;
Var Dialerrorcode : integer):Boolean;
var
s_ok : integer;
Pdialstatus : RASCONNSTATUS;
begin
DialErrorCode := 0;
DialErrorCode := RasHangUpW(DialHandle);
sleep(3000);
{ RasHangUp, to call RasGetConnectStatus(hrasconn) and Sleep(0)
in a loop until RasGetConnectStatus returns ERROR_INVALID_HANDLE.
}
Pdialstatus.dw_size := sizeof(RASCONNSTATUS);
s_ok := RasGetConnectStatusA(DialHandle,Pdialstatus);
while (Pdialstatus.dw_Error = 0 )and (s_ok =0) do
begin
s_ok := RasGetConnectStatusA(DialHandle,Pdialstatus);
Application.ProcessMessages ;
end;
if DialErrorCode <>0 Then
begin
powerhangup := False;
exit;
end;
if Pdialstatus.dw_Error <> 0 Then
begin
powerhangup := False;
exit;
end;
powerhangup := True;
end;
end.