C#控制ADSL等拨号连接

调用代码:

  1.  RASDisplay ras = new RASDisplay();  
  2.             ras.Disconnect();//断线  
  3. ras.Connect("宽带连接");//拨号,adsl是宽带连接的名称   

控制类

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5.   
  6. namespace ADSL  
  7. {  
  8.     public struct RASCONN  
  9.     {  
  10.         public int dwSize;  
  11.         public IntPtr hrasconn;  
  12.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]  
  13.         public string szEntryName;  
  14.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]  
  15.         public string szDeviceType;  
  16.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]  
  17.         public string szDeviceName;  
  18.     }  
  19.   
  20.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  21.     public struct RasStats  
  22.     {  
  23.         public int dwSize;  
  24.         public int dwBytesXmited;  
  25.         public int dwBytesRcved;  
  26.         public int dwFramesXmited;  
  27.         public int dwFramesRcved;  
  28.         public int dwCrcErr;  
  29.         public int dwTimeoutErr;  
  30.         public int dwAlignmentErr;  
  31.         public int dwHardwareOverrunErr;  
  32.         public int dwFramingErr;  
  33.         public int dwBufferOverrunErr;  
  34.         public int dwCompressionRatioIn;  
  35.         public int dwCompressionRatioOut;  
  36.         public int dwBps;  
  37.         public int dwConnectionDuration;  
  38.     }  
  39.   
  40.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  41.     public struct RasEntryName  
  42.     {  
  43.         public int dwSize;  
  44.         //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName   +   1)]  
  45.         public string szEntryName;  
  46.         //#if   WINVER5  
  47.         //     public   int   dwFlags;  
  48.         //     [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]  
  49.         //     public   string   szPhonebookPath;  
  50.         //#endif  
  51.     }  
  52.     public class RAS  
  53.     {  
  54.   
  55.         [DllImport("Rasapi32.dll", EntryPoint = "RasEnumConnectionsA",  
  56.                   SetLastError = true)]  
  57.   
  58.         internal static extern int RasEnumConnections  
  59.                 (  
  60.                 ref   RASCONN lprasconn,   //   buffer   to   receive   connections   data  
  61.                 ref   int lpcb,   //   size   in   bytes   of   buffer  
  62.                 ref   int lpcConnections   //   number   of   connections   written   to   buffer  
  63.                 );  
  64.   
  65.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  66.         internal static extern uint RasGetConnectionStatistics(  
  67.                 IntPtr hRasConn,               //   handle   to   the   connection  
  68.                 [In, Out]RasStats lpStatistics     //   buffer   to   receive   statistics  
  69.                 );  
  70.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  71.         public extern static uint RasHangUp(  
  72.                 IntPtr hrasconn     //   handle   to   the   RAS   connection   to   hang   up  
  73.                 );  
  74.   
  75.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  76.         public extern static uint RasEnumEntries(  
  77.                 string reserved,                             //   reserved,   must   be   NULL  
  78.                 string lpszPhonebook,                   //   pointer   to   full   path   and  
  79.             //     file   name   of   phone-book   file  
  80.                 [In, Out]RasEntryName[] lprasentryname,   //   buffer   to   receive  
  81.             //     phone-book   entries  
  82.                 ref   int lpcb,                                     //   size   in   bytes   of   buffer  
  83.                 out   int lpcEntries                           //   number   of   entries   written  
  84.             //     to   buffer  
  85.                 );  
  86.   
  87.         [DllImport("wininet.dll", CharSet = CharSet.Auto)]  
  88.         public extern static int InternetDial(  
  89.                 IntPtr hwnd,  
  90.                 [In]string lpszConnectoid,  
  91.                 uint dwFlags,  
  92.                 ref   int lpdwConnection,  
  93.                 uint dwReserved  
  94.                 );  
  95.   
  96.         public RAS()  
  97.         {  
  98.   
  99.         }  
  100.   
  101.     }  
  102.     public enum DEL_CACHE_TYPE   //要删除的类型。  
  103.     {  
  104.         File,//表示internet临时文件  
  105.         Cookie   //表示Cookie  
  106.     };  
  107.     public class RASDisplay  
  108.     {  
  109.         [DllImport("wininet.dll", CharSet = CharSet.Auto)]  
  110.         public static extern bool DeleteUrlCacheEntry(  
  111.                 DEL_CACHE_TYPE type  
  112.                 );  
  113.         private string m_duration;  
  114.         private string m_ConnectionName;  
  115.         private string[] m_ConnectionNames;  
  116.         private double m_TX;  
  117.         private double m_RX;  
  118.         private bool m_connected;  
  119.         private IntPtr m_ConnectedRasHandle;  
  120.   
  121.         RasStats status = new RasStats();  
  122.         public RASDisplay()  
  123.         {  
  124.             m_connected = true;  
  125.   
  126.             RAS lpras = new RAS();  
  127.             RASCONN lprasConn = new RASCONN();  
  128.   
  129.             lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));  
  130.             lprasConn.hrasconn = IntPtr.Zero;  
  131.   
  132.             int lpcb = 0;  
  133.             int lpcConnections = 0;  
  134.             int nRet = 0;  
  135.             lpcb = Marshal.SizeOf(typeof(RASCONN));  
  136.   
  137.             nRet = RAS.RasEnumConnections(ref   lprasConn, ref   lpcb, ref  
  138.                         lpcConnections);  
  139.   
  140.             if (nRet != 0)  
  141.             {  
  142.                 m_connected = false;  
  143.                 return;  
  144.   
  145.             }  
  146.   
  147.             if (lpcConnections > 0)  
  148.             {  
  149.   
  150.                 //for   (int   i   =   0;   i   <   lpcConnections;   i++)   
  151.   
  152.                 //{  
  153.                 RasStats stats = new RasStats();  
  154.   
  155.                 m_ConnectedRasHandle = lprasConn.hrasconn;  
  156.                 RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);  
  157.   
  158.                 m_ConnectionName = lprasConn.szEntryName;  
  159.   
  160.                 int Hours = 0;  
  161.                 int Minutes = 0;  
  162.                 int Seconds = 0;  
  163.   
  164.                 Hours = ((stats.dwConnectionDuration / 1000) / 3600);  
  165.                 Minutes = ((stats.dwConnectionDuration / 1000) / 60) - (Hours * 60);  
  166.                 Seconds = ((stats.dwConnectionDuration / 1000)) - (Minutes * 60) - (Hours * 3600);  
  167.   
  168.                 m_duration = Hours + "   hours   " + Minutes + "   minutes   " + Seconds + "   secs ";  
  169.                 m_TX = stats.dwBytesXmited;  
  170.                 m_RX = stats.dwBytesRcved;  
  171.   
  172.                 //}   
  173.   
  174.             }  
  175.             else  
  176.             {  
  177.                 m_connected = false;  
  178.             }  
  179.   
  180.             int lpNames = 1;  
  181.             int entryNameSize = 0;  
  182.             int lpSize = 0;  
  183.             RasEntryName[] names = null;  
  184.   
  185.             entryNameSize = Marshal.SizeOf(typeof(RasEntryName));  
  186.             lpSize = lpNames * entryNameSize;  
  187.   
  188.             names = new RasEntryName[lpNames];  
  189.             names[0].dwSize = entryNameSize;  
  190.   
  191.             uint retval = RAS.RasEnumEntries(nullnull, names, ref   lpSize, out   lpNames);  
  192.   
  193.             //if   we   have   more   than   one   connection,   we   need   to   do   it   again  
  194.             if (lpNames > 1)  
  195.             {  
  196.                 names = new RasEntryName[lpNames];  
  197.                 for (int i = 0; i < names.Length; i++)  
  198.                 {  
  199.                     names[i].dwSize = entryNameSize;  
  200.                 }  
  201.   
  202.                 retval = RAS.RasEnumEntries(nullnull, names, ref   lpSize, out   lpNames);  
  203.   
  204.             }  
  205.             m_ConnectionNames = new string[names.Length];  
  206.   
  207.             if (lpNames > 0)  
  208.             {  
  209.                 for (int i = 0; i < names.Length; i++)  
  210.                 {  
  211.   
  212.                     m_ConnectionNames[i] = names[i].szEntryName;  
  213.   
  214.                 }  
  215.             }  
  216.         }  
  217.   
  218.         public string Duration  
  219.         {  
  220.             get  
  221.             {  
  222.                 return m_connected ? m_duration : " ";  
  223.             }  
  224.         }  
  225.   
  226.         public string[] Connections  
  227.         {  
  228.             get  
  229.             {  
  230.                 return m_ConnectionNames;  
  231.             }  
  232.         }  
  233.   
  234.         public double BytesTransmitted  
  235.         {  
  236.             get  
  237.             {  
  238.                 return m_connected ? m_TX : 0;  
  239.             }  
  240.         }  
  241.         public double BytesReceived  
  242.         {  
  243.             get  
  244.             {  
  245.                 return m_connected ? m_RX : 0;  
  246.   
  247.             }  
  248.         }  
  249.         public string ConnectionName  
  250.         {  
  251.             get  
  252.             {  
  253.                 return m_connected ? m_ConnectionName : " ";  
  254.             }  
  255.         }  
  256.         public bool IsConnected  
  257.         {  
  258.             get  
  259.             {  
  260.                 return m_connected;  
  261.             }  
  262.         }  
  263.   
  264.         public int Connect(string Connection)  
  265.         {  
  266.             int temp = 0;  
  267.             uint INTERNET_AUTO_DIAL_UNATTENDED = 2;  
  268.             int retVal = RAS.InternetDial(IntPtr.Zero, Connection, INTERNET_AUTO_DIAL_UNATTENDED, ref   temp, 0);  
  269.             return retVal;  
  270.         }  
  271.         public void Disconnect()  
  272.         {  
  273.             RAS.RasHangUp(m_ConnectedRasHandle);  
  274.         }  
  275.     }  
  276.   
  277. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值