Click here to Skip to main content
11,411,312 members (64,397 online)
Click here to Skip to main content

A web based dialup Internet application

, 6 Jul 2003
Rate this:
Please Sign up or sign in to vote.
A web application to connect and disconnect from a dial up Internet session

Sample Image - webdialup.gif


I recently changed my home network, basically I added a server to connect the network to the Internet. Unfortunately where I live we can't get broadband Internet, so the Internet connection server needs to dial into our ISP. I needed a simple way to connect, disconnect and see how long the connection has been up. Normally I would use terminal services to establish and view the connection, but since my wife also uses the Internet I needed another simpler way, one that even my wife can use. I built this web application, that displays the current connection's statistics or shows the phonebook entries so that the user can connect.

Using the code

I've wrapped up some of the RAS API's so I could use them with P/Invoke. They are:

  • RasEnumConnections
  • RasGetConnectionStatistics
  • RasHangUp
  • RasEnumEntries
  • InternetDial

I also had to create some structures that these API's could use:

  • RasEntryName
  • RasStats

I created a simple class called RASDisplay which has the following methods and properties:


  • int Connect(string Connection)
  • void Disconnect()


  • bool IsConnected
  • string ConnectionName
  • double BytesReceived
  • double BytesTransmitted
  • string[] Connections
  • string Duration

The RASDisplay class takes care of all the complexity of using the RAS API. If you not familiar with using the RAS API, you have to pass in the structure sizes, so the API knows which version you are working with. The constructor for this class uses the following code to set the above properties:

private string m_duration;
private string m_ConnectionName;
private string[] m_ConnectionNames;
private double m_TX;
private double m_RX;
private bool m_connected;
private IntPtr m_ConnectedRasHandle;

public RASDisplay()
    m_connected = true;

    RAS lpras = new RAS();
    RASCONN lprasConn = new RASCONN();            

    lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
    lprasConn.hrasconn = IntPtr.Zero;

    int lpcb = 0;
    int lpcConnections = 0;
    int nRet = 0;
    lpcb = Marshal.SizeOf(typeof(RASCONN));

    nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, 
                                         ref lpcConnections);

    if(nRet != 0)
        m_connected = false;

    if(lpcConnections > 0)
        RasStats stats = new RasStats();

        m_ConnectedRasHandle = lprasConn.hrasconn;
        RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);

        m_ConnectionName = lprasConn.szEntryName;

        //Work out our duration 
        int Hours = 0;
        int Minutes = 0;
        int Seconds = 0;

        //The RasStats duration is in milliseconds
        Hours = ((stats.dwConnectDuration /1000) /3600);
        Minutes = ((stats.dwConnectDuration /1000) /60) - 
                                              (Hours * 60);
        Seconds = ((stats.dwConnectDuration /1000)) - 
                           (Minutes * 60) - (Hours * 3600);

        m_duration = Hours  +  " hours "  + Minutes + 
                " minutes " + Seconds + " secs";

        //set the bytes transferred and received
        m_TX = stats.dwBytesXmited;
        m_RX = stats.dwBytesRcved;

        //we aren't connected
        m_connected = false;

    //Find the names of the connections we could dial
    int lpNames = 1;
    int entryNameSize=Marshal.SizeOf(typeof(RasEntryName));
    int lpSize=lpNames*entryNameSize;
    RasEntryName[] names=new RasEntryName[lpNames];
    for(int i=0;i<names.Length;i++)

    uint retval = RAS.RasEnumEntries(null,null,names,
                                 ref lpSize,out lpNames);

    m_ConnectionNames = new string[lpNames];

        for(int i=0;i<lpNames;i++)
            m_ConnectionNames[i] = names[i].szEntryName;

The code to connect to the Internet uses the InternetDial WinInet API:

public int Connect(string sConnection)
    int intConnection = 0;
    int retVal = RAS.InternetDial(IntPtr.Zero, sConnection,
            INTERNET_AUTO_DIAL_UNATTENDED,ref intConnection,0);
    return retVal;

The code to disconnect is very simple as well:

public void Disconnect()

The web application that uses the RASDisplay class looks like:

protected System.Web.UI.WebControls.Label lblName;
protected System.Web.UI.WebControls.Label lblDuration;
protected System.Web.UI.WebControls.Label lblTransmitted;
protected System.Web.UI.WebControls.DataGrid dgAllConnections;
protected System.Web.UI.HtmlControls.HtmlTable tblCurrentConnection;
protected System.Web.UI.WebControls.Button btnDisconnect;
protected System.Web.UI.WebControls.Label lblRecieved;

private void Page_Load(object sender, System.EventArgs e)

private void BindData()
        RASDisplay display = new RASDisplay();

        lblName.Text = display.ConnectionName;
        lblDuration.Text = display.Duration;
        lblRecieved.Text = display.BytesReceived.ToString();
        lblTransmitted.Text = display.BytesTransmitted.ToString();

            //show the table with stats
            tblCurrentConnection.Visible = false;
            //show the tables with available connections
            dgAllConnections.Visible = false;

        //bind the data
        dgAllConnections.DataSource = display.Connections;

    catch(Exception e){

protected void btnConnect_Click(object sender, EventArgs e)
    RASDisplay rasDisplay = new RASDisplay();

    //need to find the text in the first column of the datagrid
    Button btnSender = (Button) sender;
    DataGridItem ob =  (DataGridItem) btnSender.Parent.Parent;

    int ErrorVal = rasDisplay.Connect(ob.Cells[1].Text);

    if(ErrorVal != 0)
        //redirect to the same page, so the display 
        //is refreshed with stats

protected void btnDisConnect_Click(object sender, EventArgs e)
    RASDisplay rasDisplay = new RASDisplay();
    //redirect to the same page, so the display is 
    //refreshed with available connections

The web application has a DataGrid that displays all the connections on the machine, this DataGrid only gets displayed if the computer isn't connected to the Internet. Otherwise a table with the connection's statistics gets displayed. The web page has a meta refresh tag, to keep the statistics up to date.

Know issues

Currently the Connect method calls InternetDial, this method takes the name of the connection. If this connection doesn't have a password saved, the call will fail. In a future version I'd like to change the way the application connects to the Internet, maybe using RASDial and have the application store the username and password for the user (or require them to enter it ? )


  • Article Created: 30/06/2003
  • Updated 7/7/2003 - Just a bug fix, if the user had more than one dialup internet phonebook, they might have some problems.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Australia Australia
I've been programming for a few years now. I blog regularly at httpcode.

Comments and Discussions

QuestionConnect return 87 Pinmembersealion_012629-Jun-14 2:33 
QuestionAlready have Internet connection but how to use RAS with that please help PinmemberUmesh Deshmukh3522-Mar-12 2:09 
GeneralYou Deserve a 5. Good stuff PinmemberAriel Delgado4-Nov-10 22:14 
GeneralVery Informative PinmemberMohammad Rastkar24-Jun-08 4:52 
GeneralDial up connnection in c#/.net Pinmembersatheesh.yuva13-Apr-08 22:13 
QuestionGreat Job - Need help translating it to VB.NET PinmemberMember 44388863-Apr-08 12:11 
GeneralYou got 5 PinmemberDavid Bazan26-Feb-08 10:00 
GeneralCant disconnect neither get ANY exception PinmemberMuammar©16-Mar-07 3:30 
GeneralProblem in Getting Duration Pinmemberss_hellhound16-Jan-06 7:06 
GeneralI LOVE YOU Pinmembernaiemk21-Oct-05 21:02 
GeneralQuestion about Improvement of RASDisplay class PinmemberJonny Depp10-Aug-05 23:27 
GeneralError 691 Pinmembersgomcres9-Sep-04 0:06 
GeneralRe: Error 691 PinmemberAhmad Maatouki9-Jan-07 11:50 
GeneralNeed help! PinmemberRockman X725-Aug-04 0:42 
GeneralHook to dos window PinmemberFrank5976-Aug-04 13:35 
GeneralUse of same Pinmemberdaniel doran10-Jun-04 6:27 
GeneralRasEnumConnections Error Pinmemberbsargos31-Jan-04 10:44 
GeneralRe: RasEnumConnections Error PinmemberMember 267675518-Apr-08 3:07 
QuestionVery well but problem with win98? Pinsussarmink30-Jan-04 4:11 
GeneralGood job. Two questions : Pinmemberbsargos29-Jan-04 14:47 
GeneralNeed a small help.... Pinmemberarun_br8-Dec-03 1:17 
GeneralDial-UP connectio Pinmemberhzzz7-Dec-03 13:48 
GeneralRe: Dial-UP connectio Pinmemberbenoityip8-Dec-03 3:33 
Questiongot this error...any ideas??? Pinmemberavla26-Nov-03 0:18 
GeneralCRITICAL error when reusing object : no disconnect PinmemberUps10116-Oct-03 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 7 Jul 2003
Article Copyright 2003 by Dan_P
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid