C#中怎么实现ADSL自动拨号
短信预约 -IT技能 免费直播动态提醒
C#中怎么实现ADSL自动拨号,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
C# ADSL自动拨号,前提是在系统中已经有了一个宽带拨号连接
C# ADSL自动拨号调用代码:
RASDisplayras=newRASDisplay(); ras.Disconnect();//断线 ras.Connect("adsl");//拨号 usingSystem; usingSystem.Runtime.InteropServices; publicstructRASCONN { publicintdwSize; publicIntPtrhrasconn; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=257)] publicstringszEntryName; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=17)] publicstringszDeviceType; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=129)] publicstringszDeviceName; } [StructLayout(LayoutKind.Sequential,CharSetCharSet=CharSet.Auto)] publicstructRasStats { publicintdwSize; publicintdwBytesXmited; publicintdwByteclass="lazy" data-srcved; publicintdwFramesXmited; publicintdwFrameclass="lazy" data-srcved; publicintdwCrcErr; publicintdwTimeoutErr; publicintdwAlignmentErr; publicintdwHardwareOverrunErr; publicintdwFramingErr; publicintdwBufferOverrunErr; publicintdwCompressionRatioIn; publicintdwCompressionRatioOut; publicintdwBps; publicintdwConnectionDuration; } [StructLayout(LayoutKind.Sequential,CharSetCharSet=CharSet.Auto)] publicstructRasEntryName { publicintdwSize; //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants. RAS_MaxEntryName+1)] publicstringszEntryName; //#ifWINVER5 //publicintdwFlags; //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)] //publicstringszPhonebookPath; //#endif } publicclassRAS { [DllImport("Ra<ahrefahref="http://dev.21tx.com/corp/sap/"target="_blank">SAPa>i32.dll", EntryPoint="RasEnumConnectionsA", SetLastError=true)] internalstaticexternintRasEnumConnections ( refRASCONNlprasconn,//buffertoreceiveconnectionsdata refintlpcb,//sizeinbytesofbuffer refintlpcConnections//numberofconnectionswrittentobuffer ); [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)] internalstaticexternuintRasGetConnectionStatistics( IntPtrhRasConn,//handletotheconnection [In,Out]RasStatslpStatistics//buffertoreceivestatistics ); [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)] publicexternstaticuintRasHangUp( IntPtrhrasconn//handletotheRASconnectiontohangup ); [DllImport("rasapi32.dll",CharSetCharSet=CharSet.Auto)] publicexternstaticuintRasEnumEntries( stringreserved,//reserved,mustbeNULL stringlpszPhonebook,//pointertofullpathand //filenameofphone-bookfile [In,Out]RasEntryName[]lprasentryname,//buffertoreceive //phone-bookentries refintlpcb,//sizeinbytesofbuffer outintlpcEntries//numberofentrieswritten //tobuffer ); [DllImport("wininet.dll",CharSetCharSet=CharSet.Auto)] publicexternstaticintInternetDial( IntPtrhwnd, [In]stringlpszConnectoid, uintdwFlags, refintlpdwConnection, uintdwReserved ); publicRAS() { } } publicenumDEL_CACHE_TYPE//要删除的类型。 { File,//表示internet临时文件 Cookie//表示Cookie }; publicclassRASDisplay { [DllImport("wininet.dll",CharSetCharSet=CharSet.Auto)] publicstaticexternboolDeleteUrlCacheEntry( DEL_CACHE_TYPEtype ); privatestringm_duration; privatestringm_ConnectionName; privatestring[]m_ConnectionNames; privatedoublem_TX; privatedoublem_RX; privateboolm_connected; privateIntPtrm_ConnectedRasHandle; RasStatsstatus=newRasStats(); publicRASDisplay() { m_connected=true; RASlpras=newRAS(); RASCONNlprasConn=newRASCONN(); lprasConn.dwSize=Marshal.SizeOf(typeof(RASCONN)); lprasConn.hrasconn=IntPtr.Zero; intlpcb=0; intlpcConnections=0; intnRet=0; lpcb=Marshal.SizeOf(typeof(RASCONN)); nRet=RAS.RasEnumConnections(reflprasConn,reflpcb,ref lpcConnections); if(nRet!=0) { m_connected=false; return; } if(lpcConnections>0) { //for(inti=0;i<lpcConnections;i++) //{ RasStatsstats=newRasStats(); m_ConnectedRasHandle=lprasConn.hrasconn; RAS.RasGetConnectionStatistics(lprasConn.hrasconn,stats); m_ConnectionName=lprasConn.szEntryName; intHours=0; intMinutes=0; intSeconds=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.dwByteclass="lazy" data-srcved; //} } else { m_connected=false; } intlpNames=1; intentryNameSize=0; intlpSize=0; RasEntryName[]names=null; entryNameSize=Marshal.SizeOf(typeof(RasEntryName)); lpSize=lpNames*entryNameSize; names=newRasEntryName[lpNames]; names[0].dwSize=entryNameSize; uintretval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames); //ifwehavemorethanoneconnection,weneedtodoitagain if(lpNames>1) { names=newRasEntryName[lpNames]; for(inti=0;i<names.Length;i++) { names[i].dwSize=entryNameSize; } retval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames); } m_ConnectionNames=newstring[names.Length]; if(lpNames>0) { for(inti=0;i<names.Length;i++) { m_ConnectionNames[i]=names[i].szEntryName; } } } publicstringDuration { get { returnm_connected?m_duration:""; } } publicstring[]Connections { get { returnm_ConnectionNames; } } publicdoubleBytesTransmitted { get { returnm_connected?m_TX:0; } } publicdoubleBytesReceived { get { returnm_connected?m_RX:0; } } publicstringConnectionName { get { returnm_connected?m_ConnectionName:""; } } publicboolIsConnected { get { returnm_connected; } } publicintConnect(stringConnection) { inttemp=0; uintINTERNET_AUTO_DIAL_UNATTENDED=2; intretVal=RAS.InternetDial(IntPtr.Zero,Connection,INTERNET_AUTO_DIAL_UNATTENDED, reftemp,0); returnretVal; } publicvoidDisconnect() { RAS.RasHangUp(m_ConnectedRasHandle); } }
关于C#中怎么实现ADSL自动拨号问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341