用C#写的ADSL拨号程序的代码示例
程序员文章站
2022-06-21 08:12:34
adsl自动拨号类,前提是在系统中已经有了一个宽带拨号连接 调用代码: rasdisplay ras ...
<!--startfragment-->adsl自动拨号类,前提是在系统中已经有了一个宽带拨号连接
调用代码:
rasdisplay ras = new rasdisplay();
ras.disconnect();//断线
ras.connect("adsl");//拨号
using system;
using system.runtime.interopservices;
public struct rasconn
{
public int dwsize;
public intptr hrasconn;
[marshalas(unmanagedtype.byvaltstr, sizeconst=257)]
public string szentryname;
[marshalas(unmanagedtype.byvaltstr, sizeconst=17)]
public string szdevicetype;
[marshalas(unmanagedtype.byvaltstr, sizeconst=129)]
public string szdevicename;
}
[structlayout(layoutkind.sequential,charset=charset.auto)]
public struct rasstats
{
public int dwsize;
public int dwbytesxmited;
public int dwbytesrcved;
public int dwframesxmited;
public int dwframesrcved;
public int dwcrcerr;
public int dwtimeouterr;
public int dwalignmenterr;
public int dwhardwareoverrunerr;
public int dwframingerr;
public int dwbufferoverrunerr;
public int dwcompressionratioin;
public int dwcompressionratioout;
public int dwbps;
public int dwconnectionduration;
}
[structlayout(layoutkind.sequential,charset=charset.auto)]
public struct rasentryname
{
public int dwsize;
//[marshalas(unmanagedtype.byvaltstr,sizeconst=(int)rasfieldsizeconstants.ras_maxentryname + 1)]
public string szentryname;
//#if winver5
// public int dwflags;
// [marshalas(unmanagedtype.byvaltstr,sizeconst=260+1)]
// public string szphonebookpath;
//#endif
}
public class ras
{
[dllimport("ra<a href="http://dev.21tx.com/corp/sap/" target="_blank">sap</a>i32.dll", entrypoint="rasenumconnectionsa",
setlasterror=true)]
internal static extern int rasenumconnections
(
ref rasconn lprasconn, // buffer to receive connections data
ref int lpcb, // size in bytes of buffer
ref int lpcconnections // number of connections written to buffer
);
[dllimport("rasapi32.dll",charset=charset.auto)]
internal static extern uint rasgetconnectionstatistics(
intptr hrasconn, // handle to the connection
[in,out]rasstats lpstatistics // buffer to receive statistics
);
[dllimport("rasapi32.dll",charset=charset.auto)]
public extern static uint rashangup(
intptr hrasconn // handle to the ras connection to hang up
);
[dllimport("rasapi32.dll",charset=charset.auto)]
public extern static uint rasenumentries (
string reserved, // reserved, must be null
string lpszphonebook, // pointer to full path and
// file name of phone-book file
[in,out]rasentryname[] lprasentryname, // buffer to receive
// phone-book entries
ref int lpcb, // size in bytes of buffer
out int lpcentries // number of entries written
// to buffer
);
[dllimport("wininet.dll",charset=charset.auto)]
public extern static int internetdial(
intptr hwnd,
[in]string lpszconnectoid,
uint dwflags,
ref int lpdwconnection,
uint dwreserved
);
public ras()
{
}
}
public enum del_cache_type //要删除的类型。
{
file,//表示internet临时文件
cookie //表示cookie
};
public class rasdisplay
{
[dllimport("wininet.dll",charset=charset.auto)]
public static extern bool deleteurlcacheentry(
del_cache_type type
);
private string m_duration;
private string m_connectionname;
private string[] m_connectionnames;
private double m_tx;
private double m_rx;
private bool m_connected;
private intptr m_connectedrashandle;
rasstats status = new rasstats();
public rasdisplay()
{
m_connected = true;
ras lpras = new ras();
rasconn lprasconn = new rasconn();
lprasconn.dwsize = marshal.sizeof(typeof(rasconn));
lprasconn.hrasconn = intptr.zero;
int lpcb = 0;
int lpcconnections = 0;
int nret = 0;
lpcb = marshal.sizeof(typeof(rasconn));
nret = ras.rasenumconnections(ref lprasconn, ref lpcb, ref
lpcconnections);
if(nret != 0)
{
m_connected = false;
return;
}
if(lpcconnections > 0)
{
//for (int i = 0; i < lpcconnections; i++)
//{
rasstats stats = new rasstats();
m_connectedrashandle = lprasconn.hrasconn;
ras.rasgetconnectionstatistics(lprasconn.hrasconn, stats);
m_connectionname = lprasconn.szentryname;
int hours = 0;
int minutes = 0;
int seconds = 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;
}
int lpnames = 1;
int entrynamesize = 0;
int lpsize = 0;
rasentryname[] names = null;
entrynamesize=marshal.sizeof(typeof(rasentryname));
lpsize=lpnames*entrynamesize;
names=new rasentryname[lpnames];
names[0].dwsize=entrynamesize;
uint retval = ras.rasenumentries(null,null,names,ref lpsize,out lpnames);
//if we have more than one connection, we need to do it again
if(lpnames > 1)
{
names=new rasentryname[lpnames];
for(int i=0;i<names.length;i++)
{
names[i].dwsize=entrynamesize;
}
retval = ras.rasenumentries(null,null,names,ref lpsize,out lpnames);
}
m_connectionnames = new string[names.length];
if(lpnames>0)
{
for(int i=0;i<names.length;i++)
{
m_connectionnames[i] = names[i].szentryname;
}
}
}
public string duration
{
get
{
return m_connected ? m_duration : "";
}
}
public string[] connections
{
get
{
return m_connectionnames;
}
}
public double bytestransmitted
{
get
{
return m_connected ? m_tx : 0;
}
}
public double bytesreceived
{
get
{
return m_connected ? m_rx : 0;
}
}
public string connectionname
{
get
{
return m_connected ? m_connectionname : "";
}
}
public bool isconnected
{
get
{
return m_connected;
}
}
public int connect(string connection)
{
int temp = 0;
uint internet_auto_dial_unattended = 2;
int retval = ras.internetdial(intptr.zero,connection,internet_auto_dial_unattended,ref temp,0);
return retval;
}
public void disconnect()
{
ras.rashangup(m_connectedrashandle);
}
}
调用代码:
rasdisplay ras = new rasdisplay();
ras.disconnect();//断线
ras.connect("adsl");//拨号
复制代码 代码如下:
using system;
using system.runtime.interopservices;
public struct rasconn
{
public int dwsize;
public intptr hrasconn;
[marshalas(unmanagedtype.byvaltstr, sizeconst=257)]
public string szentryname;
[marshalas(unmanagedtype.byvaltstr, sizeconst=17)]
public string szdevicetype;
[marshalas(unmanagedtype.byvaltstr, sizeconst=129)]
public string szdevicename;
}
[structlayout(layoutkind.sequential,charset=charset.auto)]
public struct rasstats
{
public int dwsize;
public int dwbytesxmited;
public int dwbytesrcved;
public int dwframesxmited;
public int dwframesrcved;
public int dwcrcerr;
public int dwtimeouterr;
public int dwalignmenterr;
public int dwhardwareoverrunerr;
public int dwframingerr;
public int dwbufferoverrunerr;
public int dwcompressionratioin;
public int dwcompressionratioout;
public int dwbps;
public int dwconnectionduration;
}
[structlayout(layoutkind.sequential,charset=charset.auto)]
public struct rasentryname
{
public int dwsize;
//[marshalas(unmanagedtype.byvaltstr,sizeconst=(int)rasfieldsizeconstants.ras_maxentryname + 1)]
public string szentryname;
//#if winver5
// public int dwflags;
// [marshalas(unmanagedtype.byvaltstr,sizeconst=260+1)]
// public string szphonebookpath;
//#endif
}
public class ras
{
[dllimport("ra<a href="http://dev.21tx.com/corp/sap/" target="_blank">sap</a>i32.dll", entrypoint="rasenumconnectionsa",
setlasterror=true)]
internal static extern int rasenumconnections
(
ref rasconn lprasconn, // buffer to receive connections data
ref int lpcb, // size in bytes of buffer
ref int lpcconnections // number of connections written to buffer
);
[dllimport("rasapi32.dll",charset=charset.auto)]
internal static extern uint rasgetconnectionstatistics(
intptr hrasconn, // handle to the connection
[in,out]rasstats lpstatistics // buffer to receive statistics
);
[dllimport("rasapi32.dll",charset=charset.auto)]
public extern static uint rashangup(
intptr hrasconn // handle to the ras connection to hang up
);
[dllimport("rasapi32.dll",charset=charset.auto)]
public extern static uint rasenumentries (
string reserved, // reserved, must be null
string lpszphonebook, // pointer to full path and
// file name of phone-book file
[in,out]rasentryname[] lprasentryname, // buffer to receive
// phone-book entries
ref int lpcb, // size in bytes of buffer
out int lpcentries // number of entries written
// to buffer
);
[dllimport("wininet.dll",charset=charset.auto)]
public extern static int internetdial(
intptr hwnd,
[in]string lpszconnectoid,
uint dwflags,
ref int lpdwconnection,
uint dwreserved
);
public ras()
{
}
}
public enum del_cache_type //要删除的类型。
{
file,//表示internet临时文件
cookie //表示cookie
};
public class rasdisplay
{
[dllimport("wininet.dll",charset=charset.auto)]
public static extern bool deleteurlcacheentry(
del_cache_type type
);
private string m_duration;
private string m_connectionname;
private string[] m_connectionnames;
private double m_tx;
private double m_rx;
private bool m_connected;
private intptr m_connectedrashandle;
rasstats status = new rasstats();
public rasdisplay()
{
m_connected = true;
ras lpras = new ras();
rasconn lprasconn = new rasconn();
lprasconn.dwsize = marshal.sizeof(typeof(rasconn));
lprasconn.hrasconn = intptr.zero;
int lpcb = 0;
int lpcconnections = 0;
int nret = 0;
lpcb = marshal.sizeof(typeof(rasconn));
nret = ras.rasenumconnections(ref lprasconn, ref lpcb, ref
lpcconnections);
if(nret != 0)
{
m_connected = false;
return;
}
if(lpcconnections > 0)
{
//for (int i = 0; i < lpcconnections; i++)
//{
rasstats stats = new rasstats();
m_connectedrashandle = lprasconn.hrasconn;
ras.rasgetconnectionstatistics(lprasconn.hrasconn, stats);
m_connectionname = lprasconn.szentryname;
int hours = 0;
int minutes = 0;
int seconds = 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;
}
int lpnames = 1;
int entrynamesize = 0;
int lpsize = 0;
rasentryname[] names = null;
entrynamesize=marshal.sizeof(typeof(rasentryname));
lpsize=lpnames*entrynamesize;
names=new rasentryname[lpnames];
names[0].dwsize=entrynamesize;
uint retval = ras.rasenumentries(null,null,names,ref lpsize,out lpnames);
//if we have more than one connection, we need to do it again
if(lpnames > 1)
{
names=new rasentryname[lpnames];
for(int i=0;i<names.length;i++)
{
names[i].dwsize=entrynamesize;
}
retval = ras.rasenumentries(null,null,names,ref lpsize,out lpnames);
}
m_connectionnames = new string[names.length];
if(lpnames>0)
{
for(int i=0;i<names.length;i++)
{
m_connectionnames[i] = names[i].szentryname;
}
}
}
public string duration
{
get
{
return m_connected ? m_duration : "";
}
}
public string[] connections
{
get
{
return m_connectionnames;
}
}
public double bytestransmitted
{
get
{
return m_connected ? m_tx : 0;
}
}
public double bytesreceived
{
get
{
return m_connected ? m_rx : 0;
}
}
public string connectionname
{
get
{
return m_connected ? m_connectionname : "";
}
}
public bool isconnected
{
get
{
return m_connected;
}
}
public int connect(string connection)
{
int temp = 0;
uint internet_auto_dial_unattended = 2;
int retval = ras.internetdial(intptr.zero,connection,internet_auto_dial_unattended,ref temp,0);
return retval;
}
public void disconnect()
{
ras.rashangup(m_connectedrashandle);
}
}
上一篇: java实现一次性压缩多个文件到zip中的方法示例
下一篇: vue.js与后台数据交互的实例讲解