C#实现ADSL自动拨号

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

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

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

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

新闻名称:C#实现ADSL自动拨号
新闻来源:http://www.shufengxianlan.com/qtweb/news0/437450.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联