using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace ConnectionDial
{
public class Ras
{
// Fields
private bool bConnected;
private ConnectionNotify ConnectNotify;
private const int DNLEN = 15;
private string EntryName;
private const int ERROR_BUFFER_TOO_SMALL = 0x25b;
private int hrasconn;
public const int MAX_PATH = 260;
public Timer NotifyTimer;
private const int PWLEN = 0x100;
private const string Ras_Authenticate = "正在验证用户名与密码.";
public const string Ras_Connected = "成功连接到";
public const string Ras_Connecting = "正在连接";
private const string Ras_DialUping = "正在拨...";
public const string Ras_Disconnected = "连接中断.";
private const string Ras_Dot = "...";
private const int RAS_MaxCallbackNumber = 0x80;
private const int RAS_MaxDeviceName = 0x80;
private const int RAS_MaxDeviceType = 0x10;
public const int RAS_MaxEntryName = 0x100;
private const int RAS_MaxPhoneNumber = 0x80;
private const string Ras_OpenPort = "正在打开端口...";
private const string Ras_PortOpend = "端口已经打开.";
private RASCONN[] Rasconn;
private const int RASCS_DONE = 0x2000;
private const int RASCS_PAUSED = 0x1000;
private const int UNLEN = 0x100;
// Methods
public Ras()
{
}
public Ras(ConnectionNotify ConnectionDelegate, double interval)
{
this.ConnectNotify = ConnectionDelegate;
this.NotifyTimer = new Timer(interval);
this.NotifyTimer.Elapsed += new ElapsedEventHandler(this.TimerEvent);
this.Rasconn = new RASCONN[1];
this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]);
this.NotifyTimer.Start();
this.bConnected = false;
}
public bool CreateEntry(int hWnd, out string strError)
{
int nErrorValue = RasCreatePhonebookEntry(hWnd, null);
if (nErrorValue == 0)
{
strError = null;
return true;
}
strError = this.GetErrorString(nErrorValue);
return false;
}
public bool DeleteEntry(string strEntryName, out string strError)
{
int nErrorValue = RasDeleteEntry(null, strEntryName);
if (nErrorValue == 0)
{
strError = null;
return true;
}
strError = this.GetErrorString(nErrorValue);
return false;
}
private static RasDialEvent lpvNotifier ;
public bool DialUp(string strEntryName, out string strError)
{
bool lpfPassword = false;
RASDIALPARAMS structure = new RASDIALPARAMS();
structure.dwSize = Marshal.SizeOf(structure);
structure.szEntryName = strEntryName;2010.04.13号修改。。防止垃圾回收处理
//RasDialEvent lpvNotifier = new RasDialEvent(this.RasDialFunc);(原来的)
// RasDialEvent lpvNotifier ;
lpvNotifier = new RasDialEvent(this.RasDialFunc);
///
int nErrorValue = RasGetEntryDialParams(null, ref structure, ref lpfPassword);
if (nErrorValue != 0)
{
strError = this.GetErrorString(nErrorValue);
return false;
}
this.ConnectNotify("正在连接" + structure.szEntryName + "...", 1);
this.EntryName = strEntryName;
this.hrasconn = 0;
nErrorValue = RasDial(0, null, ref structure, 0, lpvNotifier, ref this.hrasconn);
if (nErrorValue != 0)
{
strError = this.GetErrorString(nErrorValue);
this.ConnectNotify(strError, 3);
return false;
}
this.ConnectNotify("正在打开端口...", 1);
C#自动拨号类源代码
最新推荐文章于 2022-10-09 14:15:25 发布