VB C#断开拨号连接的完整类

======================================================================================== 
http://blog.csdn.net/jiangfeng128/archive/2006/10/12/1331876.aspx  
   
      C#断开拨号连接的完整类  
   
    using   System;  
  using   System.Collections.Generic;  
  using   System.Text;  
  using   System.Runtime.InteropServices;  
  using   System.Threading;  
  using   System.ComponentModel;  
   
  namespace   mainApp  
  {  
          class   RASHelper  
          {  
                  const   int   INTERNET_RAS_INSTALLED   =   0x10;  
   
                  [DllImport("WININET",   CharSet   =   CharSet.Auto)]  
                  public   static   extern   bool   InternetGetConnectedState(  
                          ref   int   lpdwFlags,  
                          int   dwReserved);  
   
                  const   int   MAX_PATH   =   260;  
                  const   int   RAS_MaxDeviceType   =   16;  
                  const   int   RAS_MaxPhoneNumber   =   128;  
                  const   int   RAS_MaxEntryName   =   256;  
                  const   int   RAS_MaxDeviceName   =   128;  
   
                  const   int   RAS_Connected   =   0x2000;  
   
                  [DllImport("RASAPI32",   SetLastError   =   true,   CharSet   =   CharSet.Auto)]  
                  public   static   extern   int   RasEnumConnections(  
                          [In,   Out]   RASCONN[]   lprasconn,  
                          ref   int   lpcb,  
                          ref   int   lpcConnections);  
   
                  [DllImport("RASAPI32",   SetLastError   =   true,   CharSet   =   CharSet.Auto)]  
                  public   static   extern   int   RasGetConnectStatus(  
                          IntPtr   hrasconn,  
                          ref   RASCONNSTATUS   lprasconnstatus);  
                  [DllImport("RASAPI32",   SetLastError   =   true,   CharSet   =   CharSet.Auto)]  
                  public   static   extern   int   RasHangUp(IntPtr   hrasconn);  
   
                  [StructLayout(LayoutKind.Sequential,   CharSet   =   CharSet.Auto)]  
                  public   struct   RASCONN  
                  {  
                          public   int   dwSize;  
                          public   IntPtr   hrasconn;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxEntryName   +   1)]  
                          public   string   szEntryName;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxDeviceType   +   1)]  
                          public   string   szDeviceType;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxDeviceName   +   1)]  
                          public   string   szDeviceName;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   MAX_PATH)]  
                          public   string   szPhonebook;  
                          public   int   dwSubEntry;  
                  }  
   
                  [StructLayout(LayoutKind.Sequential,   CharSet   =   CharSet.Auto)]  
                  public   struct   RASCONNSTATUS  
                  {  
                          public   int   dwSize;  
                          public   int   rasconnstate;  
                          public   int   dwError;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxDeviceType   +   1)]  
                          public   string   szDeviceType;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxDeviceName   +   1)]  
                          public   string   szDeviceName;  
                          [MarshalAs(UnmanagedType.ByValTStr,   SizeConst   =   RAS_MaxPhoneNumber   +   1)]  
                          public   string   szPhoneNumber;  
                  }  
                  ///   <summary>  
                  ///   关闭所有拨号网络链接  
                  ///   </summary>  
                  public   static   void   HangAllConnection()  
                  {  
                      int   flags   =   0;  
                          InternetGetConnectedState(ref   flags,   0);  
                          if   (!((flags   &   INTERNET_RAS_INSTALLED)   ==   INTERNET_RAS_INSTALLED))  
                                  throw   new   NotSupportedException();  
   
                          //create   array   of   structures   to   pass   to   API  
                          int   ret;  
                          int   conns   =   0;  
                          RASCONN[]   rarr   =   new   RASCONN[256];  
                          rarr.Initialize();  
                          rarr[0].dwSize   =   Marshal.SizeOf(typeof(RASCONN));  
                          int   lr   =   rarr[0].dwSize   *   rarr.Length;  
   
                          //call   RasEnumConnections   to   loop   all   RAS   connections  
                          ret   =   RasEnumConnections(rarr,   ref   lr,   ref   conns);  
                          if   (ret   !=   0)   throw   new   Win32Exception(ret);  
                          //loop   through   each   RASCONN   struct  
                          for   (int   i   =   0;   i   <   conns;   i++)  
                          {  
                                  //retrieve   RASCONN   struct  
                                  RASCONN   r   =   rarr[i];  
   
                                  //if   connection   bad,   handle   will   be   0  
                                  if   (r.hrasconn   ==   IntPtr.Zero)   continue;  
                                  RasHangUp(r.hrasconn);  
                       
                          }  
                  }  
          }  
  }

========================================================================================

ADSL自动拨号类,前提是在系统中已经有了一个宽带拨号连接  
  调用代码:  
  RASDisplay   ras   =   new   RASDisplay();  
  ras.Disconnect();//断线  
  ras.Connect("adsl");//拨号    
   
     
  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("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(  
                  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   //要删除的类型。  
  {  
          File,//表示internet临时文件  
          Cookie   //表示Cookie  
  };  
  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   *   60)   -   (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  
                  {  
                          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);  
          }  
  }


========================================================================================

========================================================================================
vb版  
  http://blog.csdn.net/modest/archive/2006/10/20/1342633.aspx  
   
      拨号、断网、枚举连接名称,判断是否在线、连接方式。  
   
  (声明:魏滔序原创,转贴请注明出处。)  
  Option   Explicit  
  '拨号/断网  
  Private   Declare   Function   InternetDial   Lib   "wininet.dll"   (ByVal   hwndParent   As   Long,   ByVal   lpszConnectoid   As   String,   ByVal   dwFlags   As   Long,   lpdwConnection   As   Long,   ByVal   dwReserved   As   Long)   As   Long  
  Private   Declare   Function   InternetHangUp   Lib   "wininet.dll"   (ByVal   dwConnection   As   Long,   ByVal   dwReserved   As   Long)   As   Long  
  Private   Declare   Function   InternetAutodial   Lib   "wininet.dll"   (ByVal   dwFlags   As   Long,   ByVal   dwReserved   As   Long)   As   Long  
  Private   Declare   Function   InternetAutodialHangup   Lib   "wininet.dll"   (ByVal   dwReserved   As   Long)   As   Long  
  Private   Const   INTERNET_DIALSTATE_DISCONNECTED   =   1  
  Private   Const   INTERNET_AUTODIAL_FORCE_ONLINE   =   1  
  Private   Const   INTERNET_AUTODIAL_FORCE_UNATTENDED   =   2  
  Private   Const   INTERNET_DIAL_UNATTENDED   =   &H8000  
  Private   Handle   As   Long  
   
  '网络状态  
  Private   Declare   Function   InternetGetConnectedStateEx   Lib   "wininet.dll"   Alias   "InternetGetConnectedStateExA"   (ByRef   lpdwFlags   As   Long,   ByVal   lpszConnectionName   As   String,   ByVal   dwNameLen   As   Long,   ByVal   dwReserved   As   Long)   As   Long  
  Private   Declare   Function   InternetGetConnectedState   Lib   "wininet.dll"   (ByRef   dwFlags   As   Long,   ByVal   dwReserved   As   Long)   As   Long  
  Private   Const   INTERNET_CONNECTION_MODEM   As   Long   =   &H1             '本系统使用调制解调器与因特网相连  
  Private   Const   INTERNET_CONNECTION_LAN   As   Long   =   &H2                 '本系统通过LAN与因特网相连  
  Private   Const   INTERNET_CONNECTION_PROXY   As   Long   =   &H4             '本系统使用proxy代理服务器与因特网相连  
  Private   Const   INTERNET_CONNECTION_MODEM_BUSY   As   Long   =   &H8   '未使用  
  Private   Const   INTERNET_RAS_INSTALLED   As   Long   =   &H10  
  Private   Const   INTERNET_CONNECTION_OFFLINE   As   Long   =   &H20  
  Private   Const   INTERNET_CONNECTION_CONFIGURED   As   Long   =   &H40  
   
  '枚举网络连接  
  Private   Const   RAS_MaxDeviceType   =   16  
  Private   Const   RAS95_MaxDeviceName   =   128  
  Private   Const   RAS95_MaxEntryName   =   256  
  Private   Type   RASCONN95  
          dwSize   As   Long  
          hRasConn   As   Long  
          szEntryName(RAS95_MaxEntryName)   As   Byte  
          szDeviceType(RAS_MaxDeviceType)   As   Byte  
          szDeviceName(RAS95_MaxDeviceName)   As   Byte  
  End   Type  
  Private   Type   RASENTRYNAME95  
          dwSize   As   Long  
          szEntryName(RAS95_MaxEntryName)   As   Byte  
  End   Type  
  Private   Declare   Function   RasEnumConnections   Lib   "rasapi32.dll"   Alias   "RasEnumConnectionsA"   (lprasconn   As   Any,   lpcb   As   Long,   lpcConnections   As   Long)   As   Long  
  Private   Declare   Function   RasEnumEntries   Lib   "rasapi32.dll"   Alias   "RasEnumEntriesA"   (ByVal   reserved   As   String,   ByVal   lpszPhonebook   As   String,   lprasentryname   As   Any,   lpcb   As   Long,   lpcEntries   As   Long)   As   Long  
  Private   Declare   Function   RasHangUp   Lib   "rasapi32.dll"   Alias   "RasHangUpA"   (ByVal   hRasConn   As   Long)   As   Long  
   
  '拨号  
  Public   Function   DialUp(LinkName   As   String)   As   Boolean  
          InternetDial   0,   LinkName,   INTERNET_AUTODIAL_FORCE_UNATTENDED,   Handle,   0  
          DialUp   =   (Handle   <>   0)  
  End   Function  
  '断网  
  Public   Sub   HangUp()  
          If   Handle   <>   0   Then  
                  InternetHangUp   Handle,   0  
                  Handle   =   0  
          End   If  
  End   Sub  
   
   
  '枚举网络连接  
  Public   Sub   EnumConnectName(Value()   As   String)  
          Dim   s   As   Long,   l   As   Long,   ln   As   Long,   a   As   String  
          ReDim   r(255)   As   RASENTRYNAME95  
   
          r(0).dwSize   =   264  
          s   =   256   *   r(0).dwSize  
          l   =   RasEnumEntries(vbNullString,   vbNullString,   r(0),   s,   ln)  
          ReDim   Value(ln   -   1)  
          For   l   =   0   To   ln   -   1  
                  a   =   StrConv(r(l).szEntryName(),   vbUnicode)  
                  Value(l)   =   Left$(a$,   InStr(a$,   Chr$(0))   -   1)  
          Next  
  End   Sub  
   
  '判断是否在线  
  Public   Function   Online()   As   Boolean  
          Online   =   InternetGetConnectedState(0&,   0&)  
  End   Function  
  '判断是否在线并返回连接方式  
  Public   Property   Get   OnlineOfLinkName(LinkName   As   String)   As   Boolean  
          LinkName   =   Space$(128)  
          OnlineOfLinkName   =   InternetGetConnectedStateEx(0,   LinkName,   128,   0&)  
  End   Property  
   
  '如果是通过LAN的连接,则返回True  
  Public   Function   IsNetConnectViaLAN()   As   Boolean  
          Dim   dwFlags   As   Long  
          Call   InternetGetConnectedState(dwFlags,   0&)  
          IsNetConnectViaLAN   =   dwFlags   And   INTERNET_CONNECTION_LAN  
  End   Function  
  '如果是通过调制解调器的连接,则返回True  
  Public   Function   IsNetConnectViaModem()   As   Boolean  
          Dim   dwFlags   As   Long  
          Call   InternetGetConnectedState(dwFlags,   0&)  
          IsNetConnectViaModem   =   dwFlags   And   INTERNET_CONNECTION_MODEM  
  End   Function  
  '如果是通过Proxy代理服务器的连接,则返回True  
  Public   Function   IsNetConnectViaProxy()   As   Boolean  
          Dim   dwFlags   As   Long  
          Call   InternetGetConnectedState(dwFlags,   0&)  
          IsNetConnectViaProxy   =   dwFlags   And   INTERNET_CONNECTION_PROXY  
  End   Function  
  '如果已安装了RAS,则返回True  
  Public   Function   IsNetRASInstalled()   As   Boolean  
          Dim   dwFlags   As   Long  
          Call   InternetGetConnectedState(dwFlags,   0&)  
          IsNetRASInstalled   =   dwFlags   And   INTERNET_RAS_INSTALLED  
  End   Function  
  '返回当前网络状态信息字符串  
  Public   Function   GetNetConnectString()   As   String  
          Dim   dwFlags   As   Long  
          Dim   msg   As   String  
          If   InternetGetConnectedState(dwFlags,   0&)   Then  
                  If   dwFlags   And   INTERNET_CONNECTION_CONFIGURED   Then  
                          msg   =   msg   &   "系统配置了网络连接"   &   vbCrLf  
                  End   If  
                  If   dwFlags   And   INTERNET_CONNECTION_LAN   Then  
                          msg   =   msg   &   "系统通过局域网与因特网相连接"  
                  End   If  
                  If   dwFlags   And   INTERNET_CONNECTION_PROXY   Then  
                          msg   =   msg   &   "并使用了Proxy代理服务"  
                  Else:   msg   =   msg   &   "."  
                  End   If  
                  If   dwFlags   And   INTERNET_CONNECTION_MODEM   Then  
                          msg   =   msg   &   "系统使用调制解调器与因特网相连接"  
                  End   If  
                  If   dwFlags   And   INTERNET_CONNECTION_OFFLINE   Then  
                          msg   =   msg   &   "系统当前处于离线状态"  
                  End   If  
                  If   dwFlags   And   INTERNET_CONNECTION_MODEM_BUSY   Then  
                          msg   =   msg   &   "系统的调制解调器未连接到因特网"  
                  End   If  
                  If   dwFlags   And   INTERNET_RAS_INSTALLED   Then  
                          msg   =   msg   &   "本系统安装了远程访问服务"  
                  End   If  
          Else  
                  msg   =   "当前未与因特网相连"  
          End   If  
          GetNetConnectString   =   msg  
  End   Function
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值