您现在的位置是:首页 > .NET

.NET

C#实现ADSL自动拨号

2020-11-12 09:09:28 .NET admin
因为每次开机都需要点击宽带连接手动拨号,很是麻烦,所以从网上找到一段自动拨号的代码,创建一个控制台程序,使其自动拨号。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.
因为每次开机都需要点击宽带连接手动拨号,很是麻烦,所以从网上找到一段自动拨号的代码,创建一个控制台程序,使其自动拨号。代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Runtime.InteropServices;

namespaceADSL自动拨号
{
classProgram
{
staticvoidMain(string[]args)
{
RASDisplayras=newRASDisplay();
ras.Disconnect();//断开连接
ras.Connect("ADSL");//重新拨号
}
}

publicstructRASCONN
{
publicintdwSize;
publicIntPtrhrasconn;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=257)]
publicstringszEntryName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=17)]
publicstringszDeviceType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=129)]
publicstringszDeviceName;
}

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
publicstructRasStats
{
publicintdwSize;
publicintdwBytesXmited;
publicintdwBytesRcved;
publicintdwFramesXmited;
publicintdwFramesRcved;
publicintdwCrcErr;
publicintdwTimeoutErr;
publicintdwAlignmentErr;
publicintdwHardwareOverrunErr;
publicintdwFramingErr;
publicintdwBufferOverrunErr;
publicintdwCompressionRatioIn;
publicintdwCompressionRatioOut;
publicintdwBps;
publicintdwConnectionDuration;
}

[StructLayout(LayoutKind.Sequential,CharSet=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("Rasapi32.dll",EntryPoint="RasEnumConnectionsA",
SetLastError=true)]

internalstaticexternintRasEnumConnections
(
refRASCONNlprasconn,//buffertoreceiveconnectionsdata
refintlpcb,//sizeinbytesofbuffer
refintlpcConnections//numberofconnectionswrittentobuffer
);


[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
internalstaticexternuintRasGetConnectionSTATISTICS(
IntPtrhRasConn,//handletotheconnection
[In,Out]RasStatslpSTATISTICS//buffertoreceiveSTATISTICS
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
publicexternstaticuintRasHangUp(
IntPtrhrasconn//handletotheRASconnectiontohangup
);

[DllImport("rasapi32.dll",CharSet=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",CharSet=CharSet.Auto)]
publicexternstaticintInternetDial(
IntPtrhwnd,
[In]stringlpszConnectoid,
uintdwFlags,
refintlpdwConnection,
uintdwReserved
);

publicRAS()
{
}
}
publicenumDEL_CACHE_TYPE//要删除的类型。
{
File,//表示internet临时文件
Cookie//表示Cookie
}

publicclassRASDisplay
{
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
publicstaticexternboolDeleteUrlCacheEntry(
DEL_CACHE_TYPEtype
);
privatestringm_duration;
privatestringm_ConnectionName;
privatestring[]m_ConnectionNames;
privatedoublem_TX;
privatedoublem_RX;
privateboolm_connected;
privateIntPtrm_ConnectedRasHandle;

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.dwBytesRcved;
//}
}
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);
}
}
}