C#断开拨号连接的完整类

时间:2010年04月12日 点击:198
C#断开拨号连接的完整类

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;

namespace mainApp
{
class RASHelper
{
const int INTERNET_RAS_INSTALLED = 0x10;

[DllImport(
"WININET", CharSet = CharSet.Auto)]
public static extern bool InternetGetConnectedState(
ref int lpdwFlags,
int dwReserved);

const int MAX_PATH = 260;
const int RAS_MaxDeviceType = 16;
const int RAS_MaxPhoneNumber = 128;
const int RAS_MaxEntryName = 256;
const int RAS_MaxDeviceName = 128;

const int RAS_Connected = 0x2000;

[DllImport(
"RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasEnumConnections(
[In, Out] RASCONN[] lprasconn,
ref int lpcb,
ref int lpcConnections);

[DllImport(
"RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasGetConnectStatus(
IntPtr hrasconn,
ref RASCONNSTATUS lprasconnstatus);
[DllImport(
"RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasHangUp(IntPtr hrasconn);

[StructLayout(LayoutKind.Sequential, CharSet
= CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxEntryName + 1)]
public string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxDeviceType + 1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxDeviceName + 1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= MAX_PATH)]
public string szPhonebook;
public int dwSubEntry;
}


[StructLayout(LayoutKind.Sequential, CharSet
= CharSet.Auto)]
public struct RASCONNSTATUS
{
public int dwSize;
public int rasconnstate;
public int dwError;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxDeviceType + 1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxDeviceName + 1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
= RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber;
}

/// <summary>
/// 关闭所有拨号网络链接
/// </summary>

public static void HangAllConnection()
{
int flags = 0;
InternetGetConnectedState(
ref flags, 0);
if (!((flags & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED))
throw new NotSupportedException();

//create array of structures to pass to API
int ret;
int conns = 0;
RASCONN[] rarr
= new RASCONN[256];
rarr.Initialize();
rarr[
0].dwSize = Marshal.SizeOf(typeof(RASCONN));
int lr = rarr[0].dwSize * rarr.Length;

//call RasEnumConnections to loop all RAS connections
ret = RasEnumConnections(rarr, ref lr, ref conns);
if (ret != 0) throw new Win32Exception(ret);
//loop through each RASCONN struct
for (int i = 0; i < conns; i++)
{
//retrieve RASCONN struct
RASCONN r = rarr[i];

//if connection bad, handle will be 0
if (r.hrasconn == IntPtr.Zero) continue;
RasHangUp(r.hrasconn);

}

}

}

}
更多DotNet好文章www.zdexe.com

赞助商链接

热门内容

相关内容

联系我们

联系方式