📄 ras.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace Chapter9
{
/// <summary>
/// Represents an individual dialup connection.
/// </summary>
public class RasConnection
{
//connection handle
private IntPtr handle;
private string name;
internal RasConnection(IntPtr handle, string name)
{
this.handle = handle;
this.name = name;
}
/// <summary>
/// Return the connection name
/// </summary>
public string Name
{
get
{
return name;
}
}
/// <summary>
/// Hangup the connection
/// </summary>
public void HangUp()
{
if (this.handle != IntPtr.Zero)
{
int result = RasHangUp(handle);
handle = IntPtr.Zero;
}
}
[DllImport("coredll", SetLastError = true)]
private static extern int RasHangUp(IntPtr Session);
public override string ToString()
{
//return name if present
if (!string.IsNullOrEmpty(name))
{
return name;
}
return handle.ToInt32().ToString("X");
}
}
public static class Ras
{
#region Connections
public static RasConnection[] Connections
{
get
{
int len = 20 * Marshal.SizeOf(typeof(RASCONN));
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.WriteInt32(ptr, Marshal.SizeOf(typeof(RASCONN)));
int cConnections;
int result = RasEnumConnections(ptr, ref len, out cConnections);
RasConnection[] connections = new RasConnection[cConnections];
for (int thisConnection = 0; thisConnection < cConnections; thisConnection++)
{
RASCONN c = (RASCONN)Marshal.PtrToStructure((IntPtr)(ptr.ToInt32() + (Marshal.SizeOf(typeof(RASCONN))*thisConnection)),typeof(RASCONN));
connections[thisConnection] = new RasConnection(c.hrasconn, c.szEntryName);
}
Marshal.FreeHGlobal(ptr);
return connections;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string szEntryName;
}
[DllImport("coredll", SetLastError = true)]
internal static extern int RasEnumConnections(IntPtr lprasconn, ref int lpcb, out int lpcConnections);
#endregion
#region Entries
public static string[] Entries
{
get
{
int cEntries;
int len = Marshal.SizeOf(typeof(RASENTRYNAME));
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.WriteInt32(ptr, len);
int result = RasEnumEntries(null, null, ptr, ref len, out cEntries);
if (result == 0x0000025B)
{
ptr = Marshal.ReAllocHGlobal(ptr, (IntPtr)len);
Marshal.WriteInt32(ptr, Marshal.SizeOf(typeof(RASENTRYNAME)));
result = RasEnumEntries(null, null, ptr, ref len, out cEntries);
}
string[] names = new string[cEntries];
for (int iEntry = 0; iEntry < cEntries; iEntry++)
{
IntPtr p = (IntPtr)(ptr.ToInt32() + (Marshal.SizeOf(typeof(RASENTRYNAME)) * iEntry));
RASENTRYNAME ren = (RASENTRYNAME)Marshal.PtrToStructure(p, typeof(RASENTRYNAME));
names[iEntry] = ren.szEntryName;
}
Marshal.FreeHGlobal(ptr);
return names;
}
}
internal struct RASENTRYNAME
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string szEntryName;
}
[DllImport("coredll", SetLastError = true)]
internal static extern int RasEnumEntries(string Reserved,string lpszPhoneBookPath, IntPtr lprasentryname, ref int lpcb, out int lpcEntries);
#endregion
#region Dial
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal struct RASDIALPARAMS
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=129)]
public string szPhoneNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=49)]
public string szCallbackNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=257)]
public string szUserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=257)]
public string szPassword;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
public string szDomain;
}
[DllImport("coredll", SetLastError = true)]
private static extern int RasDial(IntPtr dialExtensions, string phoneBookPath, ref RASDIALPARAMS rasDialParam, int NotifierType, IntPtr notifier, out IntPtr pRasConn);
public static RasConnection Dial(string name, string username, string password, string domain)
{
IntPtr handle;
RASDIALPARAMS rdp = new RASDIALPARAMS();
rdp.dwSize = Marshal.SizeOf(rdp);
rdp.szEntryName = name;
rdp.szDomain = "*";
rdp.szUserName = username;
rdp.szPassword = password;
rdp.szDomain = domain;
int result = RasDial(IntPtr.Zero, null, ref rdp, 0, IntPtr.Zero, out handle);
if (result != 0)
{
throw new System.ComponentModel.Win32Exception(result, "Error establishing connection");
}
return new RasConnection(handle, name);
}
#endregion
#region Delete Entry
[DllImport("coredll", SetLastError = true)]
internal static extern int RasDeleteEntry(string lpszPhonebook, string lpszEntry);
public static void DeleteEntry(string entry)
{
int result = RasDeleteEntry(null, entry);
}
#endregion
public static RasDevice[] Devices
{
get
{
int len = 20 * Marshal.SizeOf(typeof(RASDEVINFO));
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.WriteInt32(ptr, Marshal.SizeOf(typeof(RASDEVINFO)));
int count;
int result = RasEnumDevices(ptr, ref len, out count);
RasDevice[] devices = new RasDevice[count];
for (int i = 0; i < count; i++)
{
RASDEVINFO rdi = (RASDEVINFO)Marshal.PtrToStructure((IntPtr)(ptr.ToInt32() + (i * Marshal.SizeOf(typeof(RASDEVINFO)))), typeof(RASDEVINFO));
devices[i] = new RasDevice(rdi.szDeviceType, rdi.szDeviceName);
}
Marshal.FreeHGlobal(ptr);
return devices;
}
}
[DllImport("coredll", SetLastError = true)]
internal static extern int RasEnumDevices(IntPtr lpRasDevinfo, ref int lpcb, out int lpcDevices);
internal struct RASDEVINFO
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=17)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=129)]
public string szDeviceName;
}
}
public class RasDevice
{
private string deviceType;
private string deviceName;
internal RasDevice(string deviceType, string deviceName)
{
this.deviceType = deviceType;
this.deviceName = deviceName;
}
public string DeviceType
{
get
{
return deviceType;
}
}
public string DeviceName
{
get
{
return deviceName;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -