H
hbgywx
Unregistered / Unconfirmed
GUEST, unregistred user!
最近我写了一个RAS拨号的动态连接库autodial.dll供pb调用delphi代码如下:
unit dialunit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Ras, ExtCtrls,inifiles, StdCtrls, ComCtrls;
type
Tformdial = class(TForm)
CheckTimer: TTimer;
fShow: TLabel;
stabar: TStatusBar;
modemstatus: TLabel;
modemshow: TLabel;
procedure CheckTimerTimer(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
formdial: Tformdial;
//*********************定义变量********************************************//
g_hWnd : HWND;
hRasConn : THRASCONN;
ConnectTime : DWORD;
aRasConn : array [0..10] of TRASCONN;
nRasConnCount : DWORD;
//*********************定义函数***********************************************//
Function RasDial(EntryName,UserName,PassWord:String;showstatus:Boolean):Integer;Export;Stdcall;
Function GetActiveConnHandle(szName : String) : THRASCONN;
procedure Connected;
procedure GetActiveConn;
Function Dial(EntryName,UserName,PassWord,PhoneNumber:String;showstatus:Boolean):Integer;
Function Disconnected:Boolean;export;stdcall;
procedure CallForm(show,modemsta,zcount,dcount,showhide:String);
implementation
{$R *.dfm}
//*******************************部调用***********************************
procedure RasDialFunc(unMsg : DWORD;RasConnState : TRASCONNSTATE;dwError : DWORD); stdcall;
begin
PostMessage(g_hWnd, WM_RASDIALEVENT,RasConnState,dwError);
end;
//***************************拨号函数*****************************************//
Function RasDial(EntryName,UserName,PassWord:String;showstatus:Boolean):Integer;Export;Stdcall;
var
Res:Integer;
sININame: TFileName;
SysIni:Tinifile;
Trycount,times:Integer;
PhoneList :TStringlist;
i:integer;
begin
PhoneList:=TStringlist.Create();
i:=1;
Res:=3;
//得到Ini文件的参数
sININame := ChangeFileExt(Extractfilepath(application.ExeName),'autodial.ini');
SysIni := TIniFile.Create(sININame);
Trycount := StrtoInt(SysIni.ReadString('RAS','TryTimes','1'));
PhoneList.Add(SysIni.ReadString('RAS','PhoneNumber','163'));
Phonelist.Add(SysIni.ReadString('RAS','BackUpNumber','163'));
//拨phoneList.count 次或拨号成功退出
while(Res<>2) and (i<=PhoneList.Count) do
begin
if showstatus then
CallForm('正在检测,请稍后. . . . .','检测'+EntryName+'是否已连接','','','-1'); //调用窗口显示modem状态
//检测是否有此连接项的连接
hRasConn := GetActiveConnHandle(EntryName);
if hRasConn <> 0 then begin
Connected;
ConnectTime := $FFFFFFFF;
Result:=1;
if showstatus then
begin
CallForm('提示:'+EntryName+'已经存在,稍候退出',EntryName+'已经连通','','','0');
formdial.Free;
end;
Exit;
end;
if Trycount <>0 then times:=0 else times:=1; //Trycount=0代表拨号无限次,直到拨号成功
if showstatus then
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...',inttostr(Trycount),'1','0');
//按重复次数拨号连接
Res:=0;
While(times<>Trycount) and(Res<>2) do begin
Res:=Dial(EntryName,UserName,PassWord,PhoneList.Strings[i-1],showstatus); //拨号
times:=times+1;
if showstatus then
if Trycount <>0 then
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...','',inttostr(times+1),'0')
else
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...','',inttostr(times),'0');
End;
if showstatus then
begin
if res=2 then
CallForm('恭喜:拨号连接项'+EntryName+'连接成功','连接成功','','','0')
else
CallForm('拨号连接项:'+EntryName+'连接失败!','已挂断','','','0');
formdial.Free;
end;
inc(i);
end;
Result:=Res;
end;
//***********************检测是否已经连接*************************************//
Function GetActiveConnHandle(szName : String) : THRASCONN;
var
I : Integer;
begin
GetActiveConn;
if nRasConnCount > 0 then begin
for I := 0 to nRasConnCount - 1 do begin
if StrIComp(PChar(szName), aRasConn.szEntryName) = 0 then begin
Result := aRasConn.hRasConn;
Exit;
end;
end;
end;
Result := 0;
end;
//***************************连接使时间件控件可用检测状态***********************//
procedure Connected;
begin
ConnectTime := GetTickCount;
//++ Form1.CheckTimer.Enabled := TRUE;
end;
//****************************************************************************//
procedure GetActiveConn;
var
dwRet : DWORD;
nCB : DWORD;
Buf : array [0..255] of Char;
begin
aRasConn[0].dwSize := SizeOf(aRasConn[0]);
nCB := SizeOf(aRasConn);
dwRet := RasEnumConnectionsA(@aRasConn, @nCB, @nRasConnCount);
if dwRet <> 0 then begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
end;
end;
//******************************拨号连接*************************************//
Function Dial(EntryName,UserName,PassWord,PhoneNumber:String;showstatus:Boolean):Integer;
var
rdParams : TRASDIALPARAMS;
dwRet : DWORD;
Buf : array [0..255] of Char;
begin
FillChar(rdParams, SizeOf(rdParams), 0);
rdParams.dwSize := SizeOf(TRASDIALPARAMS);
strCopy(rdParams.szUserName, PChar(UserName));
strCopy(rdParams.szPassword, PChar(Password));
strCopy(rdParams.szEntryName, PChar(EntryName));
strCopy(rdParams.szPhoneNumber,PChar(PhoneNumber));
rdParams.szCallbackNumber[0] := '*';
rdParams.szDomain := '*';
hRasConn := 0;
//dwRet := RasDialA(nil, nil, @rdParams, 0, @RasDialFunc, @hRasConn);
dwRet := RasDialA(nil, nil, @rdParams, 0, nil, @hRasConn);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
if showstatus then CallForm('拨号连接项:'+EntryName+'正在连接',buf,'','','0');
Disconnected;
Result:=3;
end
else
Result:=2;
end;
//*****************************挂断连接***********************************//
Function Disconnected:Boolean;export;stdcall;
begin
Result:=true;
if hRasConn <> 0 then begin
result:=RasHangUpA(hRasConn)=0;
hRasConn := 0;
sleep(2000);
end;
//++ form1.CheckTimer.Enabled := FALSE;
ConnectTime := 0;
end;
//*********************检测连接项状态*************************************//
procedure Tformdial.CheckTimerTimer(Sender: TObject);
var
RasConnStatus : TRASCONNSTATUS;
Status : DWORD;
Buf : array [0..255] of Char;
begin
if hRasConn = 0 then
Exit;
FillChar(RasConnStatus, SizeOf(RasConnStatus), 0);
RasConnStatus.dwSize := SizeOf(RasConnStatus);
Status := RasGetConnectStatusA(hRasConn, @RasConnStatus);
if Status = ERROR_INVALID_HANDLE then begin
Disconnected;
Exit;
end;
if Status <> 0 then begin
Buf := '';
RasGetErrorStringA(Status, @Buf[0], SizeOf(Buf));
end;
end;
//*************************调用显示窗体**************************************//
procedure CallForm(show,modemsta,zcount,dcount,showhide:String);
begin
if showhide='-1' then //创建显示窗口
begin
Try
formdial:=Tformdial.Create(application);
formdial.StaBar.Panels.Items [0].Text :='总拨号次数:';
formdial.StaBar.Panels.Items [1].Text :='';
formdial.StaBar.Panels.items [2].Text :='当前次数';
formdial.StaBar.Panels.items [3].Text :='';
formdial.fShow.Width:=formdial.Width ;
formdial.fShow.Font.Size :=9;
formdial.Show;
Except
formdial.Free;
End;
end;
if showhide='0' then
begin
if show<>'' then formdial.fShow.Caption:=Show;
if modemsta <>'' then formdial.modemstatus.Caption:= modemsta;
if dcount<>'' then formdial.StaBar.Panels.items [3].Text :=dcount;
if zcount<>'' then
if zcount='0' then
formdial.stabar.Panels[1].Text:='无限次'
else
formdial.stabar.Panels[1].Text:=zcount;
formdial.stabar.Visible :=True;
end;
Application.ProcessMessages ;
sleep(1000);
end;
procedure Tformdial.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if Disconnected then
begin
formdial.Free;
end;
end;
end.
单通过pb调用总是出现抵制错误!例FFX0FF之类的。请高手指点!!!
unit dialunit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Ras, ExtCtrls,inifiles, StdCtrls, ComCtrls;
type
Tformdial = class(TForm)
CheckTimer: TTimer;
fShow: TLabel;
stabar: TStatusBar;
modemstatus: TLabel;
modemshow: TLabel;
procedure CheckTimerTimer(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
formdial: Tformdial;
//*********************定义变量********************************************//
g_hWnd : HWND;
hRasConn : THRASCONN;
ConnectTime : DWORD;
aRasConn : array [0..10] of TRASCONN;
nRasConnCount : DWORD;
//*********************定义函数***********************************************//
Function RasDial(EntryName,UserName,PassWord:String;showstatus:Boolean):Integer;Export;Stdcall;
Function GetActiveConnHandle(szName : String) : THRASCONN;
procedure Connected;
procedure GetActiveConn;
Function Dial(EntryName,UserName,PassWord,PhoneNumber:String;showstatus:Boolean):Integer;
Function Disconnected:Boolean;export;stdcall;
procedure CallForm(show,modemsta,zcount,dcount,showhide:String);
implementation
{$R *.dfm}
//*******************************部调用***********************************
procedure RasDialFunc(unMsg : DWORD;RasConnState : TRASCONNSTATE;dwError : DWORD); stdcall;
begin
PostMessage(g_hWnd, WM_RASDIALEVENT,RasConnState,dwError);
end;
//***************************拨号函数*****************************************//
Function RasDial(EntryName,UserName,PassWord:String;showstatus:Boolean):Integer;Export;Stdcall;
var
Res:Integer;
sININame: TFileName;
SysIni:Tinifile;
Trycount,times:Integer;
PhoneList :TStringlist;
i:integer;
begin
PhoneList:=TStringlist.Create();
i:=1;
Res:=3;
//得到Ini文件的参数
sININame := ChangeFileExt(Extractfilepath(application.ExeName),'autodial.ini');
SysIni := TIniFile.Create(sININame);
Trycount := StrtoInt(SysIni.ReadString('RAS','TryTimes','1'));
PhoneList.Add(SysIni.ReadString('RAS','PhoneNumber','163'));
Phonelist.Add(SysIni.ReadString('RAS','BackUpNumber','163'));
//拨phoneList.count 次或拨号成功退出
while(Res<>2) and (i<=PhoneList.Count) do
begin
if showstatus then
CallForm('正在检测,请稍后. . . . .','检测'+EntryName+'是否已连接','','','-1'); //调用窗口显示modem状态
//检测是否有此连接项的连接
hRasConn := GetActiveConnHandle(EntryName);
if hRasConn <> 0 then begin
Connected;
ConnectTime := $FFFFFFFF;
Result:=1;
if showstatus then
begin
CallForm('提示:'+EntryName+'已经存在,稍候退出',EntryName+'已经连通','','','0');
formdial.Free;
end;
Exit;
end;
if Trycount <>0 then times:=0 else times:=1; //Trycount=0代表拨号无限次,直到拨号成功
if showstatus then
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...',inttostr(Trycount),'1','0');
//按重复次数拨号连接
Res:=0;
While(times<>Trycount) and(Res<>2) do begin
Res:=Dial(EntryName,UserName,PassWord,PhoneList.Strings[i-1],showstatus); //拨号
times:=times+1;
if showstatus then
if Trycount <>0 then
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...','',inttostr(times+1),'0')
else
CallForm('拨号连接项:'+EntryName+'正在连接','正在拨号...','',inttostr(times),'0');
End;
if showstatus then
begin
if res=2 then
CallForm('恭喜:拨号连接项'+EntryName+'连接成功','连接成功','','','0')
else
CallForm('拨号连接项:'+EntryName+'连接失败!','已挂断','','','0');
formdial.Free;
end;
inc(i);
end;
Result:=Res;
end;
//***********************检测是否已经连接*************************************//
Function GetActiveConnHandle(szName : String) : THRASCONN;
var
I : Integer;
begin
GetActiveConn;
if nRasConnCount > 0 then begin
for I := 0 to nRasConnCount - 1 do begin
if StrIComp(PChar(szName), aRasConn.szEntryName) = 0 then begin
Result := aRasConn.hRasConn;
Exit;
end;
end;
end;
Result := 0;
end;
//***************************连接使时间件控件可用检测状态***********************//
procedure Connected;
begin
ConnectTime := GetTickCount;
//++ Form1.CheckTimer.Enabled := TRUE;
end;
//****************************************************************************//
procedure GetActiveConn;
var
dwRet : DWORD;
nCB : DWORD;
Buf : array [0..255] of Char;
begin
aRasConn[0].dwSize := SizeOf(aRasConn[0]);
nCB := SizeOf(aRasConn);
dwRet := RasEnumConnectionsA(@aRasConn, @nCB, @nRasConnCount);
if dwRet <> 0 then begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
end;
end;
//******************************拨号连接*************************************//
Function Dial(EntryName,UserName,PassWord,PhoneNumber:String;showstatus:Boolean):Integer;
var
rdParams : TRASDIALPARAMS;
dwRet : DWORD;
Buf : array [0..255] of Char;
begin
FillChar(rdParams, SizeOf(rdParams), 0);
rdParams.dwSize := SizeOf(TRASDIALPARAMS);
strCopy(rdParams.szUserName, PChar(UserName));
strCopy(rdParams.szPassword, PChar(Password));
strCopy(rdParams.szEntryName, PChar(EntryName));
strCopy(rdParams.szPhoneNumber,PChar(PhoneNumber));
rdParams.szCallbackNumber[0] := '*';
rdParams.szDomain := '*';
hRasConn := 0;
//dwRet := RasDialA(nil, nil, @rdParams, 0, @RasDialFunc, @hRasConn);
dwRet := RasDialA(nil, nil, @rdParams, 0, nil, @hRasConn);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
if showstatus then CallForm('拨号连接项:'+EntryName+'正在连接',buf,'','','0');
Disconnected;
Result:=3;
end
else
Result:=2;
end;
//*****************************挂断连接***********************************//
Function Disconnected:Boolean;export;stdcall;
begin
Result:=true;
if hRasConn <> 0 then begin
result:=RasHangUpA(hRasConn)=0;
hRasConn := 0;
sleep(2000);
end;
//++ form1.CheckTimer.Enabled := FALSE;
ConnectTime := 0;
end;
//*********************检测连接项状态*************************************//
procedure Tformdial.CheckTimerTimer(Sender: TObject);
var
RasConnStatus : TRASCONNSTATUS;
Status : DWORD;
Buf : array [0..255] of Char;
begin
if hRasConn = 0 then
Exit;
FillChar(RasConnStatus, SizeOf(RasConnStatus), 0);
RasConnStatus.dwSize := SizeOf(RasConnStatus);
Status := RasGetConnectStatusA(hRasConn, @RasConnStatus);
if Status = ERROR_INVALID_HANDLE then begin
Disconnected;
Exit;
end;
if Status <> 0 then begin
Buf := '';
RasGetErrorStringA(Status, @Buf[0], SizeOf(Buf));
end;
end;
//*************************调用显示窗体**************************************//
procedure CallForm(show,modemsta,zcount,dcount,showhide:String);
begin
if showhide='-1' then //创建显示窗口
begin
Try
formdial:=Tformdial.Create(application);
formdial.StaBar.Panels.Items [0].Text :='总拨号次数:';
formdial.StaBar.Panels.Items [1].Text :='';
formdial.StaBar.Panels.items [2].Text :='当前次数';
formdial.StaBar.Panels.items [3].Text :='';
formdial.fShow.Width:=formdial.Width ;
formdial.fShow.Font.Size :=9;
formdial.Show;
Except
formdial.Free;
End;
end;
if showhide='0' then
begin
if show<>'' then formdial.fShow.Caption:=Show;
if modemsta <>'' then formdial.modemstatus.Caption:= modemsta;
if dcount<>'' then formdial.StaBar.Panels.items [3].Text :=dcount;
if zcount<>'' then
if zcount='0' then
formdial.stabar.Panels[1].Text:='无限次'
else
formdial.stabar.Panels[1].Text:=zcount;
formdial.stabar.Visible :=True;
end;
Application.ProcessMessages ;
sleep(1000);
end;
procedure Tformdial.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if Disconnected then
begin
formdial.Free;
end;
end;
end.
单通过pb调用总是出现抵制错误!例FFX0FF之类的。请高手指点!!!