code example for ADSL dialer written in C # _c# tutorial

Source: Internet
Author: User
Tags reserved
<!--STARTFRAGMENT--&GT;ADSL Automatic dialing class, provided that a broadband dial-up connection is already in the system
Calling code:
Rasdisplay ras = new Rasdisplay ();
Ras. Disconnect ()//disconnection
Ras. Connect ("ADSL");/dial


Copy Code code as follows:

Using System;
Using System.Runtime.InteropServices;
public struct Rasconn
{
public int dwsize;
Public IntPtr Hrasconn;
[MarshalAs (UnmanagedType.ByValTStr, sizeconst=257)]
public string Szentryname;
[MarshalAs (UnmanagedType.ByValTStr, sizeconst=17)]
public string Szdevicetype;
[MarshalAs (UnmanagedType.ByValTStr, sizeconst=129)]
public string Szdevicename;
}

[StructLayout (Layoutkind.sequential,charset=charset.auto)]
public struct Rasstats
{
public int dwsize;
public int dwbytesxmited;
public int dwbytesrcved;
public int dwframesxmited;
public int dwframesrcved;
public int dwcrcerr;
public int dwtimeouterr;
public int dwalignmenterr;
public int dwhardwareoverrunerr;
public int dwframingerr;
public int dwbufferoverrunerr;
public int dwcompressionratioin;
public int dwcompressionratioout;
public int dwbps;
public int dwconnectionduration;
}

[StructLayout (Layoutkind.sequential,charset=charset.auto)]
public struct Rasentryname
{
public int dwsize;
[MarshalAs (unmanagedtype.byvaltstr,sizeconst= (int) Rasfieldsizeconstants.ras_maxentryname + 1)]
public string Szentryname;
#if WINVER5
public int dwflags;
[MarshalAs (Unmanagedtype.byvaltstr,sizeconst=260+1)]
public string Szphonebookpath;
#endif
}
public class RAS

{


[DllImport ("Ra<a href=" http://dev.21tx.com/corp/sap/"target=" _blank ">sap</a>i32.dll", entrypoint= " Rasenumconnectionsa ",
Setlasterror=true)]

internal static extern int Rasenumconnections
(
Ref Rasconn Lprasconn,//buffer to receive connections data
ref int LPCB,//size in bytes of buffer
ref int Lpcconnections//number of connections written to buffer
);


[DllImport ("Rasapi32.dll", CharSet=CharSet.Auto)]
Internal static extern uint Rasgetconnectionstatistics (
IntPtr Hrasconn,//handle to the connection
[In,out] Rasstats lpstatistics//buffer to receive statistics
);
[DllImport ("Rasapi32.dll", CharSet=CharSet.Auto)]
public extern static UINT Rashangup (
INTPTR Hrasconn//handle to the RAS connection to hang up
);

[DllImport ("Rasapi32.dll", CharSet=CharSet.Auto)]
public extern static UINT Rasenumentries (
String reserved,//reserved, must be NULL
String lpszphonebook,//pointer to full path and
File name of phone-book file
[In,out] Rasentryname[] lprasentryname,//buffer to receive
Phone-book Entries
ref int LPCB,//size in bytes of buffer
out int lpcentries//number of entries written
To buffer
);

[DllImport ("Wininet.dll", CharSet=CharSet.Auto)]
public extern static int InternetDial (
INTPTR hwnd,
[In]string Lpszconnectoid,
UINT dwflags,
ref int Lpdwconnection,
UINT dwreserved
);

Public RAS ()
{

}


}
public enum Del_cache_type//type to delete.
{
file,//represents temporary Internet files
Cookies//Representations Cookies
};
public class Rasdisplay
{
[DllImport ("Wininet.dll", CharSet=CharSet.Auto)]
public static extern bool Deleteurlcacheentry (
Del_cache_type TYPE
);
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;

Rasstats status = new Rasstats ();
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;
Return

}

if (lpcconnections > 0)
{


for (int i = 0; i < lpcconnections; i++)

//{
Rasstats stats = new Rasstats ();

M_connectedrashandle = Lprasconn.hrasconn;
Ras. Rasgetconnectionstatistics (lprasconn.hrasconn, stats);


M_connectionname = Lprasconn.szentryname;

int Hours = 0;
int Minutes = 0;
int Seconds = 0;

Hours = ((stats.dwconnectionduration/1000)/3600);
Minutes = ((stats.dwconnectionduration/1000)/60)-(Hours * 60);
Seconds = ((stats.dwconnectionduration/1000))-(Minutes *)-(Hours * 3600);


M_duration = Hours + "Hours" + Minutes + "Minutes" + Seconds + "secs";
M_tx = stats.dwbytesxmited;
M_rx = stats.dwbytesrcved;


//}


}
Else
{
m_connected = false;
}


int lpnames = 1;
int entrynamesize = 0;
int lpsize = 0;
rasentryname[] names = null;

Entrynamesize=marshal.sizeof (typeof (Rasentryname));
Lpsize=lpnames*entrynamesize;

Names=new Rasentryname[lpnames];
Names[0].dwsize=entrynamesize;

UINT retval = RAS. Rasenumentries (Null,null,names,ref lpsize,out lpnames);

If we have more than one connection and we need to do it again
if (Lpnames > 1)
{
Names=new Rasentryname[lpnames];
for (int i=0;i<names. length;i++)
{
Names[i].dwsize=entrynamesize;
}

retval = RAS. Rasenumentries (Null,null,names,ref lpsize,out lpnames);

}
M_connectionnames = new String[names. Length];


if (lpnames>0)
{
for (int i=0;i<names. length;i++)
{

M_connectionnames[i] = Names[i].szentryname;

}
}
}

public string Duration
{
Get
{
Return m_connected? M_duration: "";
}
}

Public string[] Connections
{
Get
{
return m_connectionnames;
}
}

Public double bytestransmitted
{
Get
{
Return m_connected? m_tx:0;
}
}
Public double bytesreceived
{
Get
{
Return m_connected? m_rx:0;

}
}
public string ConnectionName
{
Get
{
Return m_connected? M_connectionname: "";
}
}
public bool IsConnected
{
Get
{
return m_connected;
}
}

public int Connect (string Connection)
{
int temp = 0;
UINT internet_auto_dial_unattended = 2;
int retVal = RAS. InternetDial (Intptr.zero,connection,internet_auto_dial_unattended,ref temp,0);
return retVal;
}
public void Disconnect ()
{
Ras. Rashangup (M_connectedrashandle);
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.