<>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,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("Ra<ahref="http://dev.21tx.com/corp/sap/"target="_blank">SAP</a>i32.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;
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.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);
}
}