C#实现ADSL自动拨号

开发 后端
这里介绍C# ADSL自动拨号,前提是在系统中已经有了一个宽带拨号连接,C# ADSL自动拨号调用代码如下。

C# ADSL自动拨号,前提是在系统中已经有了一个宽带拨号连接

C# ADSL自动拨号调用代码:

  1. RASDisplayras=newRASDisplay();  
  2. ras.Disconnect();//断线  
  3. ras.Connect("adsl");//拨号  
  4.  
  5.  
  6. usingSystem;  
  7. usingSystem.Runtime.InteropServices;  
  8. publicstructRASCONN  
  9. {  
  10. publicintdwSize;  
  11. publicIntPtrhrasconn;  
  12. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=257)]  
  13. publicstringszEntryName;  
  14. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=17)]  
  15. publicstringszDeviceType;  
  16. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=129)]  
  17. publicstringszDeviceName;  
  18. }  
  19.  
  20. [StructLayout(LayoutKind.Sequential,CharSetCharSet=CharSet.Auto)]  
  21. publicstructRasStats  
  22. {  
  23. publicintdwSize;  
  24. publicintdwBytesXmited;  
  25. publicintdwBytesRcved;  
  26. publicintdwFramesXmited;  
  27. publicintdwFramesRcved;  
  28. publicintdwCrcErr;  
  29. publicintdwTimeoutErr;  
  30. publicintdwAlignmentErr;  
  31. publicintdwHardwareOverrunErr;  
  32. publicintdwFramingErr;  
  33. publicintdwBufferOverrunErr;  
  34. publicintdwCompressionRatioIn;  
  35. publicintdwCompressionRatioOut;  
  36. publicintdwBps;  
  37. publicintdwConnectionDuration;  
  38. }  
  39. [StructLayout(LayoutKind.Sequential,CharSetCharSet=CharSet.Auto)]  
  40. publicstructRasEntryName  
  41. {  
  42. publicintdwSize;  
  43. //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.  
  44. RAS_MaxEntryName+1)]  
  45. publicstringszEntryName;  
  46. //#ifWINVER5  
  47. //publicintdwFlags;  
  48. //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]  
  49. //publicstringszPhonebookPath;  
  50. //#endif  
  51. }  
  52. publicclassRAS  
  53. {  
  54. [DllImport("Ra<ahrefahref="http://dev.21tx.com/corp/sap/"target="_blank">SAPa>i32.dll",  
  55. EntryPoint="RasEnumConnectionsA",  
  56. SetLastError=true)]  
  57.  
  58. internalstaticexternintRasEnumConnections  
  59. (  
  60. refRASCONNlprasconn,//buffertoreceiveconnectionsdata  
  61. refintlpcb,//sizeinbytesofbuffer  
  62. refintlpcConnections//numberofconnectionswrittentobuffer  
  63. );  
  64.  
  65.  
  66. [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)]  
  67. internalstaticexternuintRasGetConnectionStatistics(  
  68. IntPtrhRasConn,//handletotheconnection  
  69. [In,Out]RasStatslpStatistics//buffertoreceivestatistics  
  70. );  
  71. [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)]  
  72. publicexternstaticuintRasHangUp(  
  73. IntPtrhrasconn//handletotheRASconnectiontohangup  
  74. );  
  75.  
  76. [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)]  
  77. publicexternstaticuintRasEnumEntries(  
  78. stringreserved,//reserved,mustbeNULL  
  79. stringlpszPhonebook,//pointertofullpathand  
  80. //filenameofphone-bookfile  
  81. [In,Out]RasEntryName[]lprasentryname,//buffertoreceive  
  82. //phone-bookentries  
  83. refintlpcb,//sizeinbytesofbuffer  
  84. outintlpcEntries//numberofentrieswritten  
  85. //tobuffer  
  86. );  
  87.  
  88. [DllImport("wininet.dll",CharSetCharSet=CharSet.Auto)]  
  89. publicexternstaticintInternetDial(  
  90. IntPtrhwnd,  
  91. [In]stringlpszConnectoid,  
  92. uintdwFlags,  
  93. refintlpdwConnection,  
  94. uintdwReserved  
  95. );  
  96.  
  97. publicRAS()  
  98. {  
  99. }  
  100. }  
  101. publicenumDEL_CACHE_TYPE//要删除的类型。  
  102. {  
  103. File,//表示internet临时文件  
  104. Cookie//表示Cookie  
  105. };  
  106. publicclassRASDisplay  
  107. {  
  108. [DllImport("wininet.dll",CharSetCharSet=CharSet.Auto)]  
  109. publicstaticexternboolDeleteUrlCacheEntry(  
  110. DEL_CACHE_TYPEtype  
  111. );  
  112. privatestringm_duration;  
  113. privatestringm_ConnectionName;  
  114. privatestring[]m_ConnectionNames;  
  115. privatedoublem_TX;  
  116. privatedoublem_RX;  
  117. privateboolm_connected;  
  118. privateIntPtrm_ConnectedRasHandle;  
  119.  
  120. RasStatsstatus=newRasStats();  
  121. publicRASDisplay()  
  122. {  
  123. m_connected=true;  
  124.  
  125. RASlpras=newRAS();  
  126. RASCONNlprasConn=newRASCONN();  
  127.  
  128. lprasConn.dwSize=Marshal.SizeOf(typeof(RASCONN));  
  129. lprasConn.hrasconn=IntPtr.Zero;  
  130.  
  131. intlpcb=0;  
  132. intlpcConnections=0;  
  133. intnRet=0;  
  134. lpcb=Marshal.SizeOf(typeof(RASCONN));  
  135.  
  136.  
  137. nRet=RAS.RasEnumConnections(reflprasConn,reflpcb,ref  
  138. lpcConnections);  
  139. if(nRet!=0)  
  140. {  
  141. m_connected=false;  
  142. return;  
  143. }  
  144. if(lpcConnections>0)  
  145. {  
  146. //for(inti=0;i<lpcConnections;i++)  
  147. //{  
  148. RasStatsstats=newRasStats();  
  149. m_ConnectedRasHandle=lprasConn.hrasconn;  
  150. RAS.RasGetConnectionStatistics(lprasConn.hrasconn,stats);  
  151. m_ConnectionName=lprasConn.szEntryName;  
  152. intHours=0;  
  153. intMinutes=0;  
  154. intSeconds=0;  
  155. Hours=((stats.dwConnectionDuration/1000)/3600);  
  156. Minutes=((stats.dwConnectionDuration/1000)/60)-(Hours*60);  
  157. Seconds=((stats.dwConnectionDuration/1000))-(Minutes*60)  
  158. -(Hours*3600);  
  159. m_duration=Hours+"hours"+Minutes+"minutes"  
  160. +Seconds+"secs";  
  161. m_TX=stats.dwBytesXmited;  
  162. m_RX=stats.dwBytesRcved;  
  163. //}  
  164. }  
  165. else  
  166. {  
  167. m_connected=false;  
  168. }  
  169. intlpNames=1;  
  170. intentryNameSize=0;  
  171. intlpSize=0;  
  172. RasEntryName[]names=null;  
  173. entryNameSize=Marshal.SizeOf(typeof(RasEntryName));  
  174. lpSize=lpNames*entryNameSize;  
  175. names=newRasEntryName[lpNames];  
  176. names[0].dwSize=entryNameSize;  
  177. uintretval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames);  
  178. //ifwehavemorethanoneconnection,weneedtodoitagain  
  179. if(lpNames>1)  
  180. {  
  181. names=newRasEntryName[lpNames];  
  182. for(inti=0;i<names.Length;i++)  
  183. {  
  184. names[i].dwSize=entryNameSize;  
  185. }  
  186. retval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames);  
  187. }  
  188. m_ConnectionNames=newstring[names.Length];  
  189. if(lpNames>0)  
  190. {  
  191. for(inti=0;i<names.Length;i++)  
  192. {  
  193. m_ConnectionNames[i]=names[i].szEntryName;  
  194. }  
  195. }  
  196. }  
  197. publicstringDuration  
  198. {  
  199. get  
  200. {  
  201. returnm_connected?m_duration:"";  
  202. }  
  203. }  
  204.  
  205. publicstring[]Connections  
  206. {  
  207. get  
  208. {  
  209. returnm_ConnectionNames;  
  210. }  
  211. }  
  212.  
  213. publicdoubleBytesTransmitted  
  214. {  
  215. get  
  216. {  
  217. returnm_connected?m_TX:0;  
  218. }  
  219. }  
  220. publicdoubleBytesReceived  
  221. {  
  222. get  
  223. {  
  224. returnm_connected?m_RX:0;  
  225. }  
  226. }  
  227. publicstringConnectionName  
  228. {  
  229. get  
  230. {  
  231. returnm_connected?m_ConnectionName:"";  
  232. }  
  233. }  
  234. publicboolIsConnected  
  235. {  
  236. get  
  237. {  
  238. returnm_connected;  
  239. }  
  240. }  
  241. publicintConnect(stringConnection)  
  242. {  
  243. inttemp=0;  
  244. uintINTERNET_AUTO_DIAL_UNATTENDED=2;  
  245. intretVal=RAS.InternetDial(IntPtr.Zero,Connection,INTERNET_AUTO_DIAL_UNATTENDED,  
  246. reftemp,0);  
  247. returnretVal;  
  248. }  
  249. publicvoidDisconnect()  
  250. {  
  251. RAS.RasHangUp(m_ConnectedRasHandle);  
  252. }  

以上介绍C# ADSL自动拨号。

【编辑推荐】

  1. 浅析C# ArrayList
  2. C#对象初始化学习总结
  3. 使用C#正则表达式匹配相关字符串
  4. C#改写方法学习笔记
  5. 概述C#加框和消框
责任编辑:佚名 来源: 博客园
相关推荐

2009-12-28 11:04:29

ADSL拨号上网

2010-08-02 09:27:31

Vista ADSL

2010-08-02 10:10:02

Win7 ADSL自动

2009-06-26 11:43:00

2009-01-07 09:19:00

2009-06-25 09:49:00

Windows XPADSL拨号

2009-06-17 10:05:07

自动实现属性VBC#

2009-09-04 13:55:04

C#文档自动化

2009-08-25 16:16:27

C#调用Windows

2009-12-25 10:17:38

2009-11-25 09:36:23

2010-09-07 12:13:35

ADSL PPPOE拨

2010-07-28 17:14:34

ADSL拨号设置

2009-12-09 11:45:51

SUSELinuxADSL

2010-08-02 12:18:12

ADSL拨号错误代码

2010-08-05 10:24:35

路由器设置

2009-12-29 11:11:19

2010-01-08 15:21:06

Ubuntu ADSL

2009-12-30 10:07:15

2009-06-22 10:09:18

LinuxADSL拨号
点赞
收藏

51CTO技术栈公众号