C # Implementation of ADSL automatic disconnection and dialing method (for dial-up users) _c# tutorial

Source: Internet
Author: User
Tags reserved

Encapsulate class:

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 =)] 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 ("Rasapi32.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 (intpt
  R 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 O

  F buffer out int lpcentries//number of entries written//to buffer); [DllImport ("Wininet.dll", CharSet = CharSet.Auto)] public extern static int InternetDial (INTPTR hwnd, [In]strin

  G lpszconnectoid, uint dwflags, ref int lpdwconnection, uint dwreserved);
Public RAS () {}} public enum Del_cache_type//type to delete. {file,//represents temporary Internet file cookies//Representations Cookies} public class Rasdisplay {[DllImport ("Wininet.dll", CharSet = charset.a UTO)] 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

      s = 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)/A)-(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, 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 {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 {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); }
}

Call Method:

Rasdisplay ras = new Rasdisplay ();
Ras. Disconnect ();//Disconnect
RAS. Connect ("ADSL");/redial

Note: If you do not want the confirmation dialog box to appear when you disconnect, follow the steps below to set it:

1, right click on the "Network Neighborhood"-attributes;

2, in the next "Broadband Connection" window, right-click the broadband connection, click "Properties";

3, in the subsequent Properties dialog box, click Options;

4, the following "prompt name, password and certificate" before the check mark removed, point "OK" exit;

The above C # implementation of ADSL automatic disconnection and dial-up method (applicable to dial-up users) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.