//---------------------------------------------------------------------------
#include <vcl.h>
#include <ras.h>
#include <raserror.h>
#pragma hdrstop
#include "SigMain.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TfrmMain *frmMain;
AnsiString Winver;
HRASCONN FRASHandle;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormActivate(TObject *Sender)
{
GetEntries(0);
}
//---------------------------------------------------------------------------
VOID WINAPI RasDialFunc1(HRASCONN hrasconn, UINT unMsg, RASCONNSTATE rascs,
DWORD dwError, DWORD dwExtendedError)
{
AnsiString strStatus;
switch (rascs)
{
case RASCS_OpenPort:
case RASCS_PortOpened:
/*** update status with "Opening Port" ***/
break;
case RASCS_Authenticate:
case RASCS_AuthNotify:
case RASCS_AuthRetry:
case RASCS_AuthCallback:
case RASCS_AuthChangePassword:
case RASCS_AuthProject:
case RASCS_AuthLinkSpeed:
case RASCS_AuthAck:
case RASCS_ReAuthenticate:
case RASCS_Authenticated:
case RASCS_StartAuthentication:
case RASCS_CallbackComplete:
/*** update status with "Verifying User Name and Passwd" ***/
break;
case RASCS_LogonNetwork:
/*** update status with "Logging In" ***/
break;
case RASCS_Connected:
//RemoteAccess.OnDialSuccess();
frmMain->OnDialSuccess();
return;
case RASCS_Disconnected:
/*** update status with "Disconnected" ***/
//RemoteAccess.m_dwDialError = dwError;
//RemoteAccess.OnDialFailure();
frmMain->m_dwDialError = dwError;
frmMain->OnDialFailure();
return;
case RASCS_ConnectDevice:
default:
/*** update status with "Dialing" ***/
break;
}
if (dwError != 0)
{
//RemoteAccess.m_dwDialError = dwError;
//RemoteAccess.OnDialFailure();
frmMain->m_dwDialError = dwError;
frmMain->OnDialFailure();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OnDialFailure()
{
char sErrorMsg[101];
AnsiString strMsg;
DWORD dwRetCode;
switch (m_dwDialError)
{
case ERROR_PORT_ALREADY_OPEN:
strMsg = "Modem In Use";
break;
case ERROR_LINE_BUSY:
strMsg = "Line is Busy";
break;
case ERROR_NO_ANSWER:
strMsg = "No Answer";
break;
case ERROR_CANNOT_FIND_PHONEBOOK_ENTRY:
strMsg = "Invalid Server Name";
break;
case ERROR_USER_DISCONNECTION:
strMsg = "";
break; // sample only - add all other errors here
default:
RasGetErrorString(m_dwDialError, sErrorMsg, sizeof(sErrorMsg));
strMsg = "Error. Check Log";
break;
}
if (!strMsg.IsEmpty())
{
strMsg += " - " + IntToStr(m_dwDialError) + " - " + sErrorMsg;
/*** display error string here***/
MemoPad->Lines->Add(strMsg);
// Hang-up the phone line
dwRetCode = RasHangUp(m_hRasConn);
if (dwRetCode == 0)
{
// The RAS connection state machine needs time to properly terminate.
// If the system prematurely terminates the state machine, the state
// machine may fail to properly close a port, leaving the port in an
// inconsistent state.
Sleep(3000);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OnDialSuccess()
{
/*** ready for socket connection ***/
MemoPad->Lines->Add("Successful connection");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::DisconnectFromServer()
{
Sleep(1000); // give the socket time to disconnect before hangup
// Hang-up
DWORD dwRetCode = RasHangUp(m_hRasConn);
Sleep(3000);
if (dwRetCode != 0)
m_hRasConn = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnConnectClick(TObject *Sender)
{
ConnectToServer();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnDisconnectClick(TObject *Sender)
{
DisconnectFromServer();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnQuitClick(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::GetEntries(TObject *Sender)
{
// Enumerate the entries. This is the same
// code used in the part 1 of the RAS
// articles so it is not explained here.
char eBuff[101];
RASENTRYNAME* entries = new RASENTRYNAME[1];
entries[0].dwSize = sizeof(RASENTRYNAME);
DWORD numEntries;
DWORD size = entries[0].dwSize;
DWORD res = RasEnumEntries(0, 0, entries, &size, &numEntries);
if (res == ERROR_BUFFER_TOO_SMALL)
{
// Allocate enough memory to get
// all the phonebook entries.
delete[] entries;
entries = new RASENTRYNAME[numEntries];
entries[0].dwSize = sizeof(RASENTRYNAME);
res = RasEnumEntries(0, 0, entries, &size, &numEntries);
if (res)
{
char buff[256];
RasGetErrorString(res, buff, sizeof(buff));
MemoPad->Lines->Add(buff);
}
}
if (res != 0)
{
RasGetErrorString(res, eBuff, 100);
AnsiString sTemp = eBuff;
MemoPad->Lines->Add("Error X = " + IntToStr(res) + " = " + sTemp);
return;
}
ComboBoxEntries->Items->Clear();
for (int i=0;i<(int)numEntries;i++)
ComboBoxEntries->Items->Add(entries[i].szEntryName);
ComboBoxEntries->ItemIndex = 0;
}
//---------------------------------------------------------------------------
bool __fastcall TfrmMain::ConnectToServer()
{
DWORD dwRetCode;
BOOL bPasswd;
AnsiString strMsg;
char sErrorMsg[101];
// Get the dial parameters (used by RasDial)
RasDialParams.dwSize = sizeof(RASDIALPARAMS);
/***Enter host name as defined in dialup networking ***/
strcpy(RasDialParams.szEntryName, EditConnection->Text.c_str());
dwRetCode = RasGetEntryDialParams(NULL, &RasDialParams, &bPasswd);
// Did RasGetEntryDialParams() fail?
if (dwRetCode)
{
MemoPad->Lines->Add("Connection Failed!");
m_dwDialError = dwRetCode;
OnDialFailure();
return(false);
}
strcpy(RasDialParams.szUserName, EditUsername->Text.c_str()/*** Enter user name here ***/);
strcpy(RasDialParams.szPassword, EditPassword->Text.c_str()/*** Enter password here ***/);
// Dial the remote server
dwRetCode = RasDial(NULL, NULL, &RasDialParams, 1,
(RASDIALFUNC1)RasDialFunc1, &m_hRasConn);
// Was the dial-up completed successfully?
if (dwRetCode)
{
RasGetErrorString(m_dwDialError, sErrorMsg, sizeof(sErrorMsg));
MemoPad->Lines->Add("Connection failed!");
m_dwDialError = dwRetCode;
OnDialFailure();
return(false);
}
else
{
MemoPad->Lines->Add("Connected");
FRASHandle = m_hRasConn;
return(true);
}
}
//---------------------------------------------------------------------------