http://www.kyler.com/pubs/ddj9855.php Creating your own dial-up application with RAS unit RasAppf;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Ras;type TForm1 = class(TForm) Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label1: TLabel; btnNew: TButton; btnEdit: TButton; btnRename: TButton; btnDelete: TButton; btnHangUp: TButton; btnDial: TButton; ListBox1: TListBox; StatusBar1: TStatusBar; eUserName: TEdit; ePassWord: TEdit; ePhone: TEdit; eIPAddress: TEdit; Panel1: TPanel; procedure FormCreate(Sender: TObject); procedure FormCloseQuery (Sender: TObject; var CanClose: Boolean); procedure ListBox1Click(Sender: TObject); procedure btnDialClick(Sender: TObject); procedure btnHangUpClick(Sender: TObject); procedure btnNewClick(Sender: TObject); procedure btnRenameClick(Sender: TObject); procedure btnEditClick(Sender: TObject); procedure btnDeleteClick(Sender: TObject); private { Private declarations } procedure RasGetPhoneBookEntries; function GetParams: boolean; public { Public declarations } end;var Form1: TForm1; // Pointer to variable to receive connection handle RasConn: THRasConn; // Pointer to calling parameters RasDialParams: TRasDialParams;implementation{$R *.DFM}procedure RasCallBack(msg: Integer; state: TRasConnState; dwError: Longint); stdcall; var S: string; cTxt: Array[0..255] of Char; begin with Form1 do begin if (dwError <> 0) then begin RasGetErrorString(dwError, cTxt, 256); S := cTxt; end else case state of RASCS_OpenPort: S := 'The comm port is about to be opened.'; RASCS_PortOpened: S := 'The comm port has been opened successfully.'; RASCS_ConnectDevice: S := 'A device is about to be connected.'; RASCS_DeviceConnected: S := 'A device has connected successfully.'; RASCS_AllDevicesConnected: S := 'All devices have successfully connected.'; RASCS_Authenticate: S := 'The authentication process is starting.'; RASCS_AuthNotify: S := 'An authentication event has occurred.'; RASCS_AuthRetry: S := 'New validation attempt requested.'; RASCS_AuthCallback: S := 'The remote server has requested a callback.'; RASCS_AuthChangePassword: S := 'The client has requested a password change.'; RASCS_AuthProject: S := 'The projection phase is starting.'; RASCS_AuthLinkSpeed: S := 'The link-speed calculation phase is starting.'; RASCS_AuthAck: S := 'An authentication request is being acknowledged.'; RASCS_ReAuthenticate: S := 'Reauthentication (after callback) is starting.'; RASCS_Authenticated: S := 'The client has successfully completed authentication.'; RASCS_PrepareForCallback: S := 'The line will disconnect in preparation for callback.'; RASCS_WaitForModemReset: S := 'Delaying; getting ready for a callback.'; RASCS_WaitForCallback: S := 'Waiting for an incoming call from the remote server.'; RASCS_Projected: S := 'Projection result information is available.'; RASCS_StartAuthentication: S := 'Windows 95 only: User authentication initiated.'; RASCS_CallbackComplete: S := 'Windows 95 only: Client has been called back.'; RASCS_LogonNetwork: S := 'Windows 95 only: Client is logging on to the network.'; RASCS_Connected: begin S := 'Successful connection.'; Panel1.Color := clGreen; end; RASCS_Disconnected: S := 'Disconnection or failed connection.'; end; StatusBar1.SimpleText := S; end; end;procedure TForm1.RasGetPhoneBookEntries; var RasEntryName: array[1..20] of TRasEntryName; i, x, BufSize, Entries: DWord; begin ListBox1.Clear; RasEntryName[1].dwSize := SizeOf(RasEntryName[1]); BufSize := SizeOf(RasEntryName); x := RasEnumEntries(nil, nil, @RasEntryName, BufSize, Entries); if (x = 0) or (x = ERROR_BUFFER_TOO_SMALL) then for i := 1 to Entries do if ( i < 21) and (RasEntryName[i] .szEntryName[0] <> #0) then ListBox1.Items.Add(StrPas (RasEntryName[i].szEntryName)); end;
function TForm1.GetParams: boolean; var fp: LongBool; ErrNo, ESize, DSize: Longint; Entry: TRasEntry; S: string; cTxt: Array[0..255] of Char; begin Result := false; if (ListBox1.ItemIndex < 0) then begin ShowMessage('Select a phonebook entry first!'); Exit; end; with RasDialParams do begin dwSize := Sizeof(TRasDialParams); StrLCopy(szEntryName, PChar(ListBox1.Items [ListBox1.ItemIndex]), Ras_MaxEntryName); end; ErrNo := RasGetEntryDialParams(nil, RasDialParams, fp); if (ErrNo = 0) then with RasDialParams do begin eUserName.Text := szUserName; if fp then ePassword.Text := szPassword; end else begin RasGetErrorString(ErrNo, cTxt, 256); ShowMessage('RasGetEntryDialParams failed: ' + cTxt); Exit; end; ESize := 0; DSize := 0; // Try to create the TRasEntry dynamically: Entry.dwSize := SizeOf(TRasEntry); RasGetEntryProperties(nil, PChar(ListBox1.Items [ListBox1.ItemIndex]), nil, ESize, nil, DSize); ErrNo := RasGetEntryProperties(nil, PChar(ListBox1.Items [ListBox1.ItemIndex]), @Entry, ESize, nil, DSize); if (ErrNo = 0) then with Entry do begin if (dwCountryCode <> null) and (szAreaCode <> '') then ePhone.Text := IntToStr(dwCountryCode) + ' (' + szAreaCode + ') ' + szLocalPhoneNumber else if (szAreaCode <> '') then ePhone.Text := '(' + szAreaCode + ') ' + szLocalPhoneNumber else ePhone.Text := szLocalPhoneNumber; with IPAddr do eIPAddress.Text := IntToStr(a) + '.' + IntToStr(b) + '.' + IntToStr(c) + '.' + IntToStr(d); Result := true; btnDial.Enabled := true; end else begin case RasGetErrorString(ErrNo, cTxt, 256) of 0: S := cTxt; ERROR_INSUFFICIENT_BUFFER: S := 'ERROR_INSUFFICIENT_BUFFER'; ERROR_INVALID_PARAMETER: S := 'ERROR_INVALID_PARAMETER'; // Error codes not defined in RasError.h else case ErrNo of ERROR_INVALID_USER_BUFFER: S := 'ERROR_INVALID_USER_BUFFER'; ERROR_INVALID_PARAMETER: S := 'ERROR_INVALID_PARAMETER'; ERROR_BUFFER_INVALID: S := 'ERROR_BUFFER_INVALID'; ERROR_BUFFER_TOO_SMALL: S := 'ERROR_BUFFER_TOO_SMALL'; ERROR_CANNOT_OPEN_PHONEBOOK: S := 'ERROR_CANNOT_OPEN_PHONEBOOK'; ERROR_CANNOT_FIND_PHONEBOOK_ENTRY: S := 'ERROR_CANNOT_FIND_PHONEBOOK_ENTRY'; else S := 'Unknown => ' + IntToStr(ErrNo); end; end; ShowMessage('RasGetEntryProperties failed with: ' + S); end; end;procedure TForm1.FormCreate(Sender: TObject); begin RasConn := 0; btnHangUp.Enabled := false; RasGetPhoneBookEntries; end;procedure TForm1.FormCloseQuery (Sender: TObject; var CanClose: Boolean); begin if (RasConn <> 0) then btnHangup.Click; end;procedure TForm1.btnDialClick(Sender: TObject); var ErrNo: longint; S: string; begin btnDial.Enabled := false; btnHangUp.Enabled := true; ErrNo := RasDial(nil, nil, RasDialParams, 0, @RasCallBack, RasConn); if (ErrNo <> 0) then begin case ErrNo of ERROR_BUFFER_TOO_SMALL: S := 'ERROR_BUFFER_TOO_SMALL'; ERROR_NOT_ENOUGH_MEMORY: S := 'ERROR_NOT_ENOUGH_MEMORY'; ERROR_BUFFER_INVALID: S := 'ERROR_BUFFER_INVALID'; else S := 'Unknown error (' + IntToStr(ErrNo) + ')'; end; ShowMessage('RasDial Failed with ' + S); btnHangUp.Click; end; end;procedure TForm1.btnHangUpClick(Sender: TObject); begin if (RasConn <> 0) then RasHangUp(RasConn); RasConn := 0; StatusBar1.SimpleText := 'Call Terminated'; Panel1.Color := clRed; btnDial.Enabled := true; btnHangUp.Enabled := false; end;procedure TForm1.btnNewClick(Sender: TObject); begin if (RasCreatePhonebookEntry(Handle, nil) <> 0) then ShowMessage('Failed to create a new Phonebook Entry!'); // Refresh the list box RasGetPhoneBookEntries; end;procedure TForm1.btnRenameClick(Sender: TObject); var New, Old: String; begin // Test for a selected entry if (ListBox1.ItemIndex >= 0) then begin Old := ListBox1.Items[ListBox1.ItemIndex]; New := InputBox('Rename Phonebook Entry', 'Enter new name', ListBox1.Items[ListBox1.ItemIndex]); if (New <> '') then if (RasRenameEntry(nil, PChar(Old), PChar(New)) <> 0) then ShowMessage('RasRenameEntry failed.') else RasGetPhoneBookEntries; end else ShowMessage('Select a phonebook entry first!'); end;procedure TForm1.btnEditClick(Sender: TObject); begin if (ListBox1.ItemIndex >= 0) then begin if (RasEditPhonebookEntry(Handle, nil, PChar(ListBox1.Items[ListBox1.ItemIndex])) <> 0) then ShowMessage('Edit Phonebook Entry Failed.') else RasGetPhoneBookEntries; end else ShowMessage('Select a phonebook entry first!'); end;
procedure TForm1.btnDeleteClick(Sender: TObject); begin if (ListBox1.ItemIndex >= 0) then begin if (Application.MessageBox(PChar(ListBox1.Items [ListBox1.ItemIndex] + ' connection will be removed.' + #13#10 + 'Are you sure?'), PChar(Application.Title), MB_YESNO or MB_APPLMODAL or MB_ICONWARNING) = mrYes) then if (RasDeleteEntry(nil, PChar(ListBox1.Items [ListBox1.ItemIndex])) <> 0) then ShowMessage('RasDeleteEntry failed.') else RasGetPhoneBookEntries; end else ShowMessage('Select a phonebook entry first!'); end;procedure TForm1.ListBox1Click(Sender: TObject); begin if not GetParams then btnDial.Enabled := false; end;end.
Creating your own dial-up application
with RAS
unit RasAppf;interfaceuses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ComCtrls, Ras;type
TForm1 = class(TForm)
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label1: TLabel;
btnNew: TButton;
btnEdit: TButton;
btnRename: TButton;
btnDelete: TButton;
btnHangUp: TButton;
btnDial: TButton;
ListBox1: TListBox;
StatusBar1: TStatusBar;
eUserName: TEdit;
ePassWord: TEdit;
ePhone: TEdit;
eIPAddress: TEdit;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery
(Sender: TObject; var CanClose: Boolean);
procedure ListBox1Click(Sender: TObject);
procedure btnDialClick(Sender: TObject);
procedure btnHangUpClick(Sender: TObject);
procedure btnNewClick(Sender: TObject);
procedure btnRenameClick(Sender: TObject);
procedure btnEditClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
private
{ Private declarations }
procedure RasGetPhoneBookEntries;
function GetParams: boolean;
public
{ Public declarations }
end;var
Form1: TForm1;
// Pointer to variable to receive connection handle
RasConn: THRasConn;
// Pointer to calling parameters
RasDialParams: TRasDialParams;implementation{$R *.DFM}procedure RasCallBack(msg: Integer; state: TRasConnState;
dwError: Longint); stdcall;
var
S: string;
cTxt: Array[0..255] of Char;
begin
with Form1 do begin
if (dwError <> 0) then begin
RasGetErrorString(dwError, cTxt, 256);
S := cTxt;
end
else
case state of
RASCS_OpenPort:
S := 'The comm port is about to be opened.';
RASCS_PortOpened:
S := 'The comm port has been opened successfully.';
RASCS_ConnectDevice:
S := 'A device is about to be connected.';
RASCS_DeviceConnected:
S := 'A device has connected successfully.';
RASCS_AllDevicesConnected:
S := 'All devices have successfully connected.';
RASCS_Authenticate:
S := 'The authentication process is starting.';
RASCS_AuthNotify:
S := 'An authentication event has occurred.';
RASCS_AuthRetry:
S := 'New validation attempt requested.';
RASCS_AuthCallback:
S := 'The remote server has requested a callback.';
RASCS_AuthChangePassword:
S := 'The client has requested a password change.';
RASCS_AuthProject:
S := 'The projection phase is starting.';
RASCS_AuthLinkSpeed:
S := 'The link-speed calculation phase is starting.';
RASCS_AuthAck:
S := 'An authentication request
is being acknowledged.';
RASCS_ReAuthenticate:
S := 'Reauthentication (after callback) is starting.';
RASCS_Authenticated:
S := 'The client has successfully
completed authentication.';
RASCS_PrepareForCallback:
S := 'The line will disconnect
in preparation for callback.';
RASCS_WaitForModemReset:
S := 'Delaying; getting ready for a callback.';
RASCS_WaitForCallback:
S := 'Waiting for an incoming
call from the remote server.';
RASCS_Projected:
S := 'Projection result information is available.';
RASCS_StartAuthentication:
S := 'Windows 95 only:
User authentication initiated.';
RASCS_CallbackComplete:
S := 'Windows 95 only: Client has been called back.';
RASCS_LogonNetwork:
S := 'Windows 95 only:
Client is logging on to the network.';
RASCS_Connected: begin
S := 'Successful connection.';
Panel1.Color := clGreen;
end;
RASCS_Disconnected:
S := 'Disconnection or failed connection.';
end;
StatusBar1.SimpleText := S; end;
end;procedure TForm1.RasGetPhoneBookEntries;
var
RasEntryName: array[1..20] of TRasEntryName;
i, x, BufSize, Entries: DWord;
begin
ListBox1.Clear;
RasEntryName[1].dwSize := SizeOf(RasEntryName[1]);
BufSize := SizeOf(RasEntryName);
x := RasEnumEntries(nil, nil,
@RasEntryName, BufSize, Entries);
if (x = 0) or (x = ERROR_BUFFER_TOO_SMALL) then
for i := 1 to Entries do
if ( i < 21) and (RasEntryName[i]
.szEntryName[0] <> #0) then
ListBox1.Items.Add(StrPas
(RasEntryName[i].szEntryName));
end;
var
fp: LongBool;
ErrNo, ESize, DSize: Longint;
Entry: TRasEntry;
S: string;
cTxt: Array[0..255] of Char;
begin
Result := false;
if (ListBox1.ItemIndex < 0) then begin
ShowMessage('Select a phonebook entry first!');
Exit;
end;
with RasDialParams do begin
dwSize := Sizeof(TRasDialParams);
StrLCopy(szEntryName, PChar(ListBox1.Items
[ListBox1.ItemIndex]), Ras_MaxEntryName);
end;
ErrNo := RasGetEntryDialParams(nil, RasDialParams, fp);
if (ErrNo = 0) then
with RasDialParams do begin
eUserName.Text := szUserName;
if fp then
ePassword.Text := szPassword;
end
else begin
RasGetErrorString(ErrNo, cTxt, 256);
ShowMessage('RasGetEntryDialParams failed: ' + cTxt);
Exit;
end; ESize := 0;
DSize := 0;
// Try to create the TRasEntry dynamically:
Entry.dwSize := SizeOf(TRasEntry);
RasGetEntryProperties(nil, PChar(ListBox1.Items
[ListBox1.ItemIndex]), nil,
ESize, nil, DSize);
ErrNo := RasGetEntryProperties(nil, PChar(ListBox1.Items
[ListBox1.ItemIndex]),
@Entry, ESize, nil, DSize);
if (ErrNo = 0) then with Entry do begin
if (dwCountryCode <> null) and (szAreaCode <> '') then
ePhone.Text := IntToStr(dwCountryCode) + '
(' + szAreaCode +
') ' + szLocalPhoneNumber
else if (szAreaCode <> '') then
ePhone.Text := '(' + szAreaCode + ') '
+ szLocalPhoneNumber
else
ePhone.Text := szLocalPhoneNumber;
with IPAddr do
eIPAddress.Text := IntToStr(a) + '.'
+ IntToStr(b) + '.' +
IntToStr(c) + '.' + IntToStr(d);
Result := true;
btnDial.Enabled := true;
end
else begin
case RasGetErrorString(ErrNo, cTxt, 256) of
0: S := cTxt;
ERROR_INSUFFICIENT_BUFFER:
S := 'ERROR_INSUFFICIENT_BUFFER';
ERROR_INVALID_PARAMETER:
S := 'ERROR_INVALID_PARAMETER';
// Error codes not defined in RasError.h
else
case ErrNo of
ERROR_INVALID_USER_BUFFER:
S := 'ERROR_INVALID_USER_BUFFER';
ERROR_INVALID_PARAMETER:
S := 'ERROR_INVALID_PARAMETER';
ERROR_BUFFER_INVALID:
S := 'ERROR_BUFFER_INVALID';
ERROR_BUFFER_TOO_SMALL:
S := 'ERROR_BUFFER_TOO_SMALL';
ERROR_CANNOT_OPEN_PHONEBOOK:
S := 'ERROR_CANNOT_OPEN_PHONEBOOK';
ERROR_CANNOT_FIND_PHONEBOOK_ENTRY:
S := 'ERROR_CANNOT_FIND_PHONEBOOK_ENTRY';
else
S := 'Unknown => ' + IntToStr(ErrNo);
end;
end;
ShowMessage('RasGetEntryProperties failed with: ' + S);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
RasConn := 0;
btnHangUp.Enabled := false;
RasGetPhoneBookEntries;
end;procedure TForm1.FormCloseQuery
(Sender: TObject; var CanClose: Boolean);
begin
if (RasConn <> 0) then
btnHangup.Click;
end;procedure TForm1.btnDialClick(Sender: TObject);
var
ErrNo: longint;
S: string;
begin
btnDial.Enabled := false;
btnHangUp.Enabled := true;
ErrNo := RasDial(nil, nil, RasDialParams, 0,
@RasCallBack, RasConn);
if (ErrNo <> 0) then begin
case ErrNo of
ERROR_BUFFER_TOO_SMALL: S := 'ERROR_BUFFER_TOO_SMALL';
ERROR_NOT_ENOUGH_MEMORY: S := 'ERROR_NOT_ENOUGH_MEMORY';
ERROR_BUFFER_INVALID: S := 'ERROR_BUFFER_INVALID';
else
S := 'Unknown error (' + IntToStr(ErrNo) + ')';
end;
ShowMessage('RasDial Failed with ' + S);
btnHangUp.Click;
end;
end;procedure TForm1.btnHangUpClick(Sender: TObject);
begin
if (RasConn <> 0) then
RasHangUp(RasConn);
RasConn := 0;
StatusBar1.SimpleText := 'Call Terminated';
Panel1.Color := clRed;
btnDial.Enabled := true;
btnHangUp.Enabled := false;
end;procedure TForm1.btnNewClick(Sender: TObject);
begin
if (RasCreatePhonebookEntry(Handle, nil) <> 0) then
ShowMessage('Failed to create a new Phonebook Entry!');
// Refresh the list box
RasGetPhoneBookEntries;
end;procedure TForm1.btnRenameClick(Sender: TObject);
var
New, Old: String;
begin
// Test for a selected entry
if (ListBox1.ItemIndex >= 0) then begin
Old := ListBox1.Items[ListBox1.ItemIndex];
New := InputBox('Rename Phonebook Entry', 'Enter new name',
ListBox1.Items[ListBox1.ItemIndex]);
if (New <> '') then
if (RasRenameEntry(nil, PChar(Old), PChar(New)) <> 0)
then
ShowMessage('RasRenameEntry failed.')
else
RasGetPhoneBookEntries;
end
else
ShowMessage('Select a phonebook entry first!');
end;procedure TForm1.btnEditClick(Sender: TObject);
begin
if (ListBox1.ItemIndex >= 0) then begin
if (RasEditPhonebookEntry(Handle, nil,
PChar(ListBox1.Items[ListBox1.ItemIndex])) <> 0) then
ShowMessage('Edit Phonebook Entry Failed.')
else
RasGetPhoneBookEntries;
end
else
ShowMessage('Select a phonebook entry first!');
end;
begin
if (ListBox1.ItemIndex >= 0) then begin
if (Application.MessageBox(PChar(ListBox1.Items
[ListBox1.ItemIndex] +
' connection will be removed.' + #13#10
+ 'Are you sure?'),
PChar(Application.Title), MB_YESNO or
MB_APPLMODAL or MB_ICONWARNING)
= mrYes) then
if (RasDeleteEntry(nil, PChar(ListBox1.Items
[ListBox1.ItemIndex])) <> 0) then
ShowMessage('RasDeleteEntry failed.')
else
RasGetPhoneBookEntries;
end
else
ShowMessage('Select a phonebook entry first!');
end;procedure TForm1.ListBox1Click(Sender: TObject);
begin
if not GetParams then
btnDial.Enabled := false;
end;end.