前一段时间我查阅资料,看了一处用“猫”拔号到异地计算机,并建立连接的例子,整个过程只需要一个猫和一根电话线即可。它可以通过拔号连接到对方的计算机(但要求对方计算机也有一个“猫”和电话线,并己建立了正常的拔号连接)或直接拔到某一部电话机上。其流程用文字表示即为:本地计算机------>本地猫-----电话线----->PSTN-----电话线----->异地猫---->异地计算机。
我使用的环境是Windows2000,在安装盘WINNT的system32下有一个名为rasapi32.dll的文件至关重要,它是拔号程序运行的基础库,Windows己将拔号的接口封装在了里面,如果要验证其是否正常,可以先在计算机中连接一个猫,并接上电话线。打开“网上邻居”----->属性,双击“新建连接”,选择“拔号到专用网络”选项,输入电话号码等。如果成功建立连接,则表示正常;如果本地计算机要做为接收呼入的被叫方,则需要创建一个呼入连接,具体的办法是:“网上邻居”----->“属性”,双击“新建连接”,选择“接受传入的连接”选项,再根据向导完成创建。由此便会在“网上邻居”里新出现一个呼入的连接图标。即可开始以下程序的编制工作。
首先,需要一个与rasapi32.dll文件的接口文件,我很幸运地找到了一个名为Ras.pas的文件,它将接口库中的函数再次封装,使用起来非常方便。
//Ras.pas
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Unit: Remote Access Service (RAS)
Creation: Feb 18, 1997. Translated from MS-Visual C 4.2 header files
EMail: francois.piette@pophost.eunet.be francois.piette@rtfm.be
http://www.rtfm.be/fpiette
Legal issues: Copyright (C) 1997, 1998 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Updates:
Sep 25, 1998 V1.10 Added RasGetIPAddress and RasGetProjectionInfoA. Thanks to
Jan Tomasek <xtomasej@fel.cvut.cz> for his help.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit Ras;
interface
uses
Windows, SysUtils;
{$DEFINE WINVER400}
const
RasUnitVersion = 110;
CopyRight : String = ' RasUnit (c) 97-98 F. Piette V1.10 ';
rasapi32 = 'rasapi32.dll';
UNLEN = 256; // Maximum user name length
PWLEN = 256; // Maximum password length
CNLEN = 15; // Computer name length
DNLEN = CNLEN; // Maximum domain name length
RAS_MaxDeviceType = 16;
RAS_MaxPhoneNumber = 128;
RAS_MaxIpAddress = 15;
RAS_MaxIpxAddress = 21;
{$IFDEF WINVER400}
RAS_MaxEntryName = 256;
RAS_MaxDeviceName = 128;
RAS_MaxCallbackNumber = RAS_MaxPhoneNumber;
{$ELSE}
RAS_MaxEntryName = 20;
RAS_MaxDeviceName = 32;
RAS_MaxCallbackNumber = 48;
{$ENDIF}
RAS_MaxAreaCode = 10;
RAS_MaxPadType = 32;
RAS_MaxX25Address = 200;
RAS_MaxFacilities = 200;
RAS_MaxUserData = 200;
RASCS_OpenPort = 0;
RASCS_PortOpened = 1;
RASCS_ConnectDevice = 2;
RASCS_DeviceConnected = 3;
RASCS_AllDevicesConnected = 4;
RASCS_Authenticate = 5;
RASCS_AuthNotify = 6;
RASCS_AuthRetry = 7;
RASCS_AuthCallback = 8;
RASCS_AuthChangePassword = 9;
RASCS_AuthProject = 10;
RASCS_AuthLinkSpeed = 11;
RASCS_AuthAck = 12;
RASCS_ReAuthenticate = 13;
RASCS_Authenticated = 14;
RASCS_PrepareForCallback = 15;
RASCS_WaitForModemReset = 16;
RASCS_WaitForCallback = 17;
RASCS_Projected = 18;
{$IFDEF WINVER400}
RASCS_StartAuthentication = 19;
RASCS_CallbackComplete = 20;
RASCS_LogonNetwork = 21;
{$ENDIF}
RASCS_SubEntryConnected = 22;
RASCS_SubEntryDisconnected= 23;
RASCS_PAUSED = $1000;
RASCS_Interactive = RASCS_PAUSED;
RASCS_RetryAuthentication = (RASCS_PAUSED + 1);
RASCS_CallbackSetByCaller = (RASCS_PAUSED + 2);
RASCS_PasswordExpired = (RASCS_PAUSED + 3);
RASCS_DONE = $2000;
RASCS_Connected = RASCS_DONE;
RASCS_Disconnected = (RASCS_DONE + 1);
// If using RasDial message notifications, get the notification message code
// by passing this string to the RegisterWindowMessageA() API.
// WM_RASDIALEVENT is used only if a unique message cannot be registered.
RASDIALEVENT = 'RasDialEvent';
WM_RASDIALEVENT = $CCCD;
// TRASPROJECTION
RASP_Amb = $10000;
RASP_PppNbf = $0803F;
RASP_PppIpx = $0802B;
RASP_PppIp = $08021;
RASP_Slip = $20000;
type
THRASCONN = THandle;
PHRASCONN = ^THRASCONN;
TRASCONNSTATE = DWORD;
PDWORD = ^DWORD;
PBOOL = ^BOOL;
TRASDIALPARAMS = packed record
dwSize : DWORD;
szEntryName : array [0..RAS_MaxEntryName] of Char;
szPhoneNumber : array [0..RAS_MaxPhoneNumber] of Char;
szCallbackNumber : array [0..RAS_MaxCallbackNumber] of Char;
szUserName : array [0..UNLEN] of Char;
szPassword : array [0..PWLEN] of Char;
szDomain : array [0..DNLEN] of Char;
{$IFDEF WINVER401}
dwSubEntry : DWORD;
dwCallbackId : DWORD;
{$ENDIF}
szPadding : array [0..2] of Char;
end;
PRASDIALPARAMS = ^TRASDIALPARAMS;
TRASDIALEXTENSIONS = packed record
dwSize : DWORD;
dwfOptions : DWORD;
hwndParent : HWND;
reserved : DWORD;
end;
PRASDIALEXTENSIONS = ^TRASDIALEXTENSIONS;
TRASCONNSTATUS = packed record
dwSize : DWORD;
RasConnState : TRASCONNSTATE;
dwError : DWORD;
szDeviceType : array [0..RAS_MaxDeviceType] of char;
szDeviceName : array [0..RAS_MaxDeviceName] of char;
szPadding : array [0..1] of Char;
end;
PRASCONNSTATUS = ^TRASCONNSTATUS;
TRASCONN = packed record
dwSize : DWORD;
hRasConn : THRASCONN;
szEntryName : array [0..RAS_MaxEntryName] of char;
{$IFDEF WINVER400}
szDeviceType : array [0..RAS_MaxDeviceType] of char;
szDeviceName : array [0..RAS_MaxDeviceName] of char;
{$ENDIF}
szPadding : array [0..0] of Char;
end;
PRASCONN = ^TRASCONN;
TRASENTRYNAME = packed record
dwSize : DWORD;
szEntryName : array [0..RAS_MaxEntryName] of char;
szPadding : array [0..2] of Char;
end;
PRASENTRYNAME = ^TRASENTRYNAME;
TRASENTRYDLG = packed record
dwSize : DWORD;
hWndOwner : HWND;
dwFlags : DWORD;
xDlg : LongInt;
yDlg : LongInt;
szEntry : array [0..RAS_MaxEntryName] of char;
dwError : DWORD;
Reserved : DWORD;
Reserved2 : DWORD;
szPadding : array [0..2] of Char;
end;
PRASENTRYDLG = ^TRASENTRYDLG;
TRASPROJECTION = integer;
TRASPPPIP = record
dwSize : DWORD;
dwError : DWORD;
szIpAddress : array [0..RAS_MaxIpAddress] of char;
end;
function RasDialA(RasDialExtensions: PRASDIALEXTENSIONS;
PhoneBook : PChar;
RasDialParams : PRASDIALPARAMS;
NotifierType : DWORD;
Notifier : Pointer;
RasConn : PHRASCONN
): DWORD; stdcall;
function RasGetErrorStringA(
uErrorValue : DWORD; // error to get string for
szErrorString : PChar; // buffer to hold error string
cBufSize : DWORD // size, in characters, of buffer
): DWORD; stdcall;
function RasHangupA(RasConn: THRASCONN): DWORD; stdcall;
function RasConnectionStateToString(nState : Integer) : String;
function RasGetConnectStatusA(
hRasConn: THRASCONN; // handle to RAS connection of interest
lpRasConnStatus : PRASCONNSTATUS // buffer to receive status data
): DWORD; stdcall;
function RasEnumConnectionsA(
pRasConn : PRASCONN; // buffer to receive connections data
pCB : PDWORD; // size in bytes of buffer
pcConnections : PDWORD // number of connections written to buffer
) : DWORD; stdcall
function RasEnumEntriesA(
Reserved : Pointer; // reserved, must be NIL
szPhonebook : PChar; // full path and filename of phonebook file
lpRasEntryName : PRASENTRYNAME; // buffer to receive entries
lpcb : PDWORD; // size in bytes of buffer
lpcEntries : PDWORD // number of entries written to buffer
) : DWORD; stdcall;
function RasGetEntryDialParamsA(
lpszPhonebook : PChar; // pointer to the full path and filename of the phonebook file
lprasdialparams : PRASDIALPARAMS; // pointer to a structure that receives the connection parameters
lpfPassword : PBOOL // indicates whether the user's password was retrieved
) : DWORD; stdcall;
function RasEditPhonebookEntryA(
hWndParent : HWND; // handle to the parent window of the dialog box
lpszPhonebook : PChar; // pointer to the full path and filename of the phonebook file
lpszEntryName : PChar // pointer to the phonebook entry name
) : DWORD; stdcall;
//function RasEntryDlgA(
// lpszPhonebook : PChar; // pointer to the full path and filename of the phone-book file
// lpszEntry : PChar; // pointer to the name of the phone-book entry to edit, copy, or create
// lpInfo : PRASENTRYDLG // pointer to a structure that contains additional parameters
// ) : DWORD; stdcall;
function RasCreatePhonebookEntryA(
hWndParent : HWND; // handle to the parent window of the dialog box
lpszPhonebook : PChar // pointer to the full path and filename of the phonebook file
) : DWORD; stdcall;
function RasGetProjectionInfoA(
hRasConn : THRASCONN; // handle that specifies remote access connection of interest
RasProjection : TRASPROJECTION; // specifies type of projection information to obtain
lpProjection : Pointer; // points to buffer that receives projection information
lpcb : PDWORD // points to variable that specifies buffer size
) : DWORD; stdcall;
function RasGetIPAddress: string;
implementation
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasConnectionStateToString(nState : Integer) : String;
begin
case nState of
RASCS_OpenPort: Result := 'Opening Port';
RASCS_PortOpened: Result := 'Port Opened';
RASCS_ConnectDevice: Result := 'Connecting Device';
RASCS_DeviceConnected: Result := 'Device Connected';
RASCS_AllDevicesConnected: Result := 'All Devices Connected';
RASCS_Authenticate: Result := 'Starting Authentication';
RASCS_AuthNotify: Result := 'Authentication Notify';
RASCS_AuthRetry: Result := 'Authentication Retry';
RASCS_AuthCallback: Result := 'Callback Requested';
RASCS_AuthChangePassword: Result := 'Change Password Requested';
RASCS_AuthProject: Result := 'Projection Phase Started';
RASCS_AuthLinkSpeed: Result := 'Link Speed Calculation';
RASCS_AuthAck: Result := 'Authentication Acknowledged';
RASCS_ReAuthenticate: Result := 'Reauthentication Started';
RASCS_Authenticated: Result := 'Authenticated';
RASCS_PrepareForCallback: Result := 'Preparation For Callback';
RASCS_WaitForModemReset: Result := 'Waiting For Modem Reset';
RASCS_WaitForCallback: Result := 'Waiting For Callback';
RASCS_Projected: Result := 'Projected';
{$IFDEF WINVER400}
RASCS_StartAuthentication: Result := 'Start Authentication';
RASCS_CallbackComplete: Result := 'Callback Complete';
RASCS_LogonNetwork: Result := 'Logon Network';
{$ENDIF}
RASCS_SubEntryConnected: Result := '';
RASCS_SubEntryDisconnected: Result := '';
RASCS_Interactive: Result := 'Interactive';
RASCS_RetryAuthentication: Result := 'Retry Authentication';
RASCS_CallbackSetByCaller: Result := 'Callback Set By Caller';
RASCS_PasswordExpired: Result := 'Password Expired';
RASCS_Connected: Result := 'Connected';
RASCS_Disconnected: Result := 'Disconnected';
else
Result := 'Connection state #' + IntToStr(nState);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasGetIPAddress: string;
var
RASConns : TRasConn;
dwSize : DWORD;
dwCount : DWORD;
RASpppIP : TRASPPPIP;
begin
Result := '';
RASConns.dwSize := SizeOf(TRASConn);
RASpppIP.dwSize := SizeOf(RASpppIP);
dwSize := SizeOf(RASConns);
if RASEnumConnectionsA(@RASConns, @dwSize, @dwCount) = 0 then begin
if dwCount > 0 then begin
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := SizeOf(RASpppIP);
if RASGetProjectionInfoA(RASConns.hRasConn,
RASP_PppIp,
@RasPPPIP,
@dwSize) = 0 then
Result := StrPas(RASpppIP.szIPAddress);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasDialA; external rasapi32 name 'RasDialA';
function RasGetErrorStringA; external rasapi32 name 'RasGetErrorStringA';
function RasHangUpA; external rasapi32 name 'RasHangUpA';
function RasGetConnectStatusA; external rasapi32 name 'RasGetConnectStatusA';
function RasEnumConnectionsA; external rasapi32 name 'RasEnumConnectionsA';
function RasEnumEntriesA; external rasapi32 name 'RasEnumEntriesA';
function RasGetEntryDialParamsA; external rasapi32 name 'RasGetEntryDialParamsA';
function RasEditPhonebookEntryA; external rasapi32 name 'RasEditPhonebookEntryA';
//function RasEntryDlgA; external rasapi32 name 'RasEntryDlgA';
function RasCreatePhonebookEntryA; external rasapi32 name 'RasCreatePhonebookEntryA';
function RasGetProjectionInfoA; external rasapi32 name 'RasGetProjectionInfoA';
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
end.
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
不管如何,这是老外的杰作,我们还是关注一下自己的“作品”吧,以下为我的Unit。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Ras;
type
TForm1 = class(TForm)
Button1: TButton;
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
LabeledEdit3: TLabeledEdit;
Button2: TButton;
Button3: TButton;
GroupBox1: TGroupBox;
Memo1: TMemo;
Timer1: TTimer;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
LabeledEdit4: TLabeledEdit;
Button8: TButton;
Button9: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
hRasConn : THRASCONN;
end;
var
Form1: TForm1;
g_hWnd: HWND; //连接句柄
implementation
{$R *.dfm}
procedure RasDialFunc();//unMsg : DWORD; RasConnState : TRASCONNSTATE; dwError : DWORD);
begin
{ PostMessage(g_hWnd,
WM_RASDIALEVENT,
RasConnState,
dwError); }
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tServer, tUserName, tPassWord : PChar;
rdParams : TRASDIALPARAMS;
dwRet : DWORD;
Buf : array [0..255] of Char;
begin //拔号
memo1.Lines.Clear; //清空消息提示框
FillChar(rdParams,Sizeof(rdParams),0);
rdParams.dwSize := Sizeof(rdParams);
strcopy(rdParams.szUserName, PChar(LabeledEdit2.Text)); //异地计算机用户名
strcopy(rdParams.szPassword, PChar(LabeledEdit3.Text)); //异地计算机密码
strcopy(rdParams.szEntryName, PChar(LabeledEdit1.Text)); //本地连接名,即刚才创建的拔号连接名
rdParams.szPhoneNumber[0] := #0;
rdParams.szCallbackNumber[0] := '*';
rdParams.szDomain := '*';
hRasConn := 0;; // @RasDialFunc
dwRet := RasDialA(nil, nil, @rdParams, 0, nil, @hRasConn); //发送拔号指令
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if hRasConn <> 0 then
begin
RasHangUpA(hRasConn); // 断开连接
hRasConn := 0;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
IP, Name : String;
begin //显示IP
IP := RasGetIPAddress;
// Name := GetPhoneName; +' Name = '+Name
memo1.Lines.Add('IP = '+IP);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
dwRet : Dword;
buf : PChar;
begin
dwRet := RasCreatePhonebookEntryA(handle,nil);
if dwRet <> 0 then
begin
RasGetErrorStringA(dwRet, buf, 128);
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
dwRet : DWORD;
begin
dwRet := RasEditPhonebookEntryA(handle,nil,Pchar(LabeledEdit1.Text));
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
//winexec(Pchar('rundll32.exe rnaui.dll,RnaDial'+'拨号连接'),SW_SHOWNORMAL);
winexec('rundll32.exe rnaui,rnadial'+'拨号连接',9);//若还没有建立拔号连接,则弹出建立向导,rundll32.exe 文件在system32下,可将其考到当前程序目录下
end;
procedure TForm1.Button7Click(Sender: TObject);
var
r: DWORD;
c: Array[0..100] of Char;
hRas: THRasConn;
DialParams: TRasDialParams;
begin
Screen.Cursor := crHourglass;
FillChar(DialParams, SizeOf(TRasDialParams), 0);
with dialparams do
begin
dwSize := Sizeof(TRasDialParams);
//StrPCopy(szEntryName, lstEntrys.Items[lstEntrys.ItemIndex]);
//StrPCopy(szEntryName, '审报连接');
StrPCopy(szUserName, LabeledEdit2.Text);//改变用户名
StrPCopy(szPassword, LabeledEdit3.Text);//改变密码
StrPCopy(szPhoneNumber,LabeledEdit4.Text);//改变电话号码
end;
hRas := 0;;
r := RasDialA(nil,nil,@dialparams,0,nil,@hRas);
if r <> 0 then
begin
RasGetErrorStringA(r, c, 100);
ShowMessage('错误: ' + c);
end
else
memo1.Lines.Add('ok');
Screen.Cursor := crDefault;
hRasConn := hRas;
end;
procedure TForm1.Button9Click(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
memo1.Lines.Add('Connection closed');
exit;
end;
if Status <> 0 then
begin
buf :='';
RasGetErrorStringA(Status,@buf[0],sizeof(buf));
memo1.Lines.Add('Error #'+inttostr(Status));
end;
end;
end.
此程序如果拔向电话,则对方话机会振铃;若拔向计算机(对方计算机也必须有连上电话线的猫,并且也要建立拔号连接),则对方计算机右下角会出现一个与网络连接相同的图标(注意,拔号连接也需要IP地址,其地址不要与现有的网络IP冲突),此时即己建立了正常的拔号连接,其使用与一般网络无差别,只是速度要慢与一般宽带。