以前写的C#代码,下面是拨号和断开的函数【在Vista/XP系统中,网通拨号测试通过,不需要安装网通的拨号客户端】:
private static Mutex mutex = new Mutex();
private Process dailer = new Process();
int Desc;
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
//Creating a function that uses the API function...
//if out parameter returns 18 then fail,if 81 then success
public void IsConnectedToInternet()
{
InternetGetConnectedState(out Desc, 0);
}
private void StopDailer()
{
while (Desc == 81)
{
lock (dailer)
{
if (!IsAlive("rundll32"))
{
mutex.WaitOne();
dailer.StartInfo.FileName = "rundll32.exe";
dailer.StartInfo.Arguments = "iedkcs32.dll CloseRASConnections";
dailer.Start();
//Thread.Sleep(1000);
mutex.ReleaseMutex();
}
}
IsConnectedToInternet();
}
dailer.Close();
}
private void StartDailer()
{
while (Desc != 81)
{
lock (dailer)
{
if (!IsAlive("rasdial"))
{
mutex.WaitOne();
dailer.StartInfo.FileName = "rasdial.exe";
//txtDail.Text宽带拨号的名称、txtName.Text宽带用户名【注意:是加密过的,不是你的原始用户名】、txtPWD.Text宽带用户密码
dailer.StartInfo.Arguments = txtDail.Text.Trim() + " " + txtName.Text.Trim() + " " + txtPWD.Text.Trim();
dailer.Start();
mutex.ReleaseMutex();
}
//Thread.Sleep(1000);
}
IsConnectedToInternet();
}
dailer.Close();
}
private bool IsAlive(string name)
{
Process[] ps = Process.GetProcessesByName(name);
if (ps.Length > 0)
{
return true;
}
else
{
return false;
}
}