【转】使用C#实现ADSL自动拨号
原帖地址:http://www.cnblogs.com/guozk/archive/2010/04/07/1705951.html
在网络编程中,有时候会需要重新拨号建立网络连接(如Ad点击软件通过重新拨号形成有效点击) ,下面介绍两种程序中拨号的方法.
1、最简单的方法:使用RasDial命令
RasDial是Windows自带的命令,使用非常简单。实际使用是可将下面代码保存为一个批处理文件,然后直接运行或在程序里进行调用。
rasdial.exe /disconnect '断开连接
del "C:\Documents and Settings\hyuqin\Cookies\*.*" /Q '清除Cookie
rasdial.exe 连接名 连接账户 连接密码 '重新拨号
2、封装为类,灵活调用
上面提到的方法虽然很简单,但并非标准的实现方法,使用时会受到一些限制,此时最好的办法就是将实现代码封装为类库。下面这个类库是网上一位朋友提供的,直接调用就行.
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("Rasapi32.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");//重新拨号
哦,我晕。原来这样子就可以了。。
就不用弹框框了。。无语啊
1、右击“网上邻居”--属性;
2、在随后出现的“宽带连接”窗口中,右击宽带连接,单击“属性”;
3、在随后出现的属性对话框中,单击“选项”;
4、把随后出现的“提示名称、密码和证书等”前面的对号去掉,点“确定”退出;
【转】使用C#实现ADSL自动拨号的更多相关文章
- 利用.pbk来实现ADSL开机自动拨号
当你新建拨号连接或者VPN连接之后在你的电脑里会创建一个.pbk的文件 这个.pbk的文件可以说是一个集合,将你电脑的所有连接都保存在一起. 同时你还可以将此连接复制起来传给其他人. 系统默认的.pb ...
- 如何实现ADSL宽带用户开机自动拨号与定时拨号
在宽带拨号网络的环境下,要通过手动拨号认证才能上网.下面给大家介绍怎么设置开机自动拨号上网以及定时拨号上网. 这也是为一个叫CHY的2B准备的技术套餐,不需要用到网上说的自动拨号软件,只要在主机上设置 ...
- C# 实现ADSL自动断网和拨号(适用于拨号用户)
using System;using System.Runtime.InteropServices; public struct RASCONN{ public int dwSize; p ...
- 深圳电信光纤用户必备:简单破解中兴ZXA10 F460光电猫,实现WIFI和自动拨号功能
最近搬家,ADSL转成光纤宽带,下载速度非常给力.原来的ADSL路由器派不上用场,电信的安装人员也给开通了wifi功能,只是wifi无法上网,而且拨号一定需要用电脑连网线进行拨号.以前是直接把拨号账户 ...
- PC-ADSL开机自动拨号方法
方法一:把adsl拨号的快捷方式放到“开始”菜单中“所有程序”中的“启动”中. 再到“控制面板”中的“网络连接”中找到你用的拨号连接,鼠标右键点击选择“属性”.然后,在窗口上部选择“选项”,把“拨号选 ...
- WinCE下GPRS自动拨号软件(GPRS AutoDial)
之前在WinCE下调试USB的3G Modem时,写过一个拨号助手RASManager,基本能用.后来车机卖到俄罗斯去,客户老M提供了一个更好的GPRS自动拨号软件GPRS AutoDial,功能完善 ...
- html:唤起手机qq开始对话 & 自动拨号
<a href="mqqwpa://im/chat?chat_type=wpa&uin=[qq号]&version=1">XXX</a> 另 ...
- win10以上系统设定PPTP自动拨号
:bohaorasdial adsl 123 123if not %errorlevel% == 0 goto :bohaoexit rasdial adsl 123 123 rasdial是开始拨号 ...
- 利用Py-Socket模块做的一个不登陆windows服务器自动实现替换或者调用自动拨号功能
xu言: 最近,有个朋友让我帮忙“搞点事情”,然后正好在学习socket模块,这个模块666啊,基本上可以实现远程服务器cmd shell的大部分功能.好,话不多说,直接上码~ 由于很多电信运营商都会 ...
随机推荐
- Xdebug的使用
1.http://www.cnblogs.com/mo-beifeng/articles/2446142.html 2.http://www.cnblogs.com/ximu/articles/200 ...
- Android学习笔记(2)
今天我继续看Mars老师的Android开发视频教程,看到一个“深入LinearLayout”的时候,发现一个比较好玩的技巧. 控件的layout_weight属性,他是父控件剩余空间的比例. 如果把 ...
- 【Android】利用服务Service创建标题栏通知
创建标题栏通知的核心代码 public void CreateInform() { //定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等) Inten ...
- Unity NGUI 描点控件的位移动画
要让一个描点的控件动画移动到一个Position,能够用TweenPosition.可是这个仅仅能用在Position是固定的情况下.并且不能依据分辨率适配来进行移动. 以NGUI自带的 ...
- 架构师书单 2nd Edition--转载
作者:江南白衣,原文出处: http://blog.csdn.net/calvinxiu/archive/2007/03/06/1522032.aspx,转载请保留. 为了2007年的目标,列了下面待 ...
- oracle时间格式转换问题 ORA-01810: format code appears twice--转
今天在做报表查询的时候Oracle报错: 信息为 ORA-01810: format code appears twice 原因:由于想java一样转化时间格式,但是Oracle中是不区分大小写的,所 ...
- const int * pi/int * const pi的区别
前面有一篇文章:数组名就是常量指针 参考文章:http://blog.pfan.cn/whyhappy/5164.html const int * pi .int const * pi与int * ...
- 双系统下,Windows如何正确删除Linux系统
一般电脑装了双系统,特别是Windows加Linux的电脑,不可以在Windows中直接删了linux,因为一般安装linux的时候,grub都写进了mbr,直接删了Windows就进不了了,除非原来 ...
- BootStrap2学习日记10---单选框和复选框
<label>选择你的性别</label> <label class="radio"> <input type="radio&q ...
- Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代
继续并发,上篇博客对于ScheduledThreadPoolExecutor没有进行介绍,说过会和Timer一直单独写一篇Blog. 1.Timer管理延时任务的缺陷 a.以前在项目中也经常使用定时器 ...