`
java-mans
  • 浏览: 11432380 次
文章分类
社区版块
存档分类
最新评论

C#自动拨号类源代码

 
阅读更多
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);
            strError = null;
            return true;
        }

        public bool EditEntry(int hWnd, string strEntryName, out string strError)
        {
            int nErrorValue = RasEditPhonebookEntry(hWnd, null, strEntryName);
            if (nErrorValue == 0)
            {
                strError = null;
                return true;
            }
            strError = this.GetErrorString(nErrorValue);
            return false;
        }

        public bool GetDefaultEntry(out string strEntry, out string strError)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\RAS AutoDial\Default");
            if (key != null)
            {
                string str = (string)key.GetValue("DefaultInternet");
                if ((str != null) && (str.Length > 0))
                {
                    strEntry = str;
                    strError = null;
                    return true;
                }
            }
            strEntry = null;
            strError = "注册表访问失败.";
            return false;
        }

        public bool GetEntries(out string[] strEntryName, out string strError)
        {
            RASENTRYNAME[] lprasentryname = new RASENTRYNAME[1];
            lprasentryname[0].dwSize = Marshal.SizeOf(lprasentryname[0]);
            int lpcb = 0;
            int lpcEntries = 0;
            int nErrorValue = RasEnumEntries(null, null, lprasentryname, ref lpcb, ref lpcEntries);
            switch (nErrorValue)
            {
                case 0:
                    break;

                case 0x25b:
                    lprasentryname = new RASENTRYNAME[lpcEntries];
                    lprasentryname[0].dwSize = Marshal.SizeOf(lprasentryname[0]);
                    break;

                default:
                    strError = this.GetErrorString(nErrorValue);
                    strEntryName = null;
                    return false;
            }
            nErrorValue = RasEnumEntries(null, null, lprasentryname, ref lpcb, ref lpcEntries);
            if (nErrorValue != 0)
            {
                strError = this.GetErrorString(nErrorValue);
                strEntryName = null;
                return false;
            }
            strEntryName = new string[lpcEntries];
            for (int i = 0; i < lpcEntries; i++)
            {
                strEntryName[i] = lprasentryname[i].szEntryName;
            }
            strError = null;
            return true;
        }

        public bool GetEntryParams(string strEntryName, out string strPhoneNumber, out string strUserName, out string strPassword, out bool bRememberPassword, out string strError)
        {
            bool lpfPassword = false;
            RASDIALPARAMS structure = new RASDIALPARAMS();
            structure.dwSize = Marshal.SizeOf(structure);
            structure.szEntryName = strEntryName;
            int nErrorValue = RasGetEntryDialParams(null, ref structure, ref lpfPassword);
            if (nErrorValue != 0)
            {
                strError = this.GetErrorString(nErrorValue);
                strPhoneNumber = null;
                strUserName = null;
                strPassword = null;
                bRememberPassword = false;
                strError = null;
                return false;
            }
            strPhoneNumber = structure.szPhoneNumber;
            strUserName = structure.szUserName;
            if (lpfPassword)
            {
                strPassword = structure.szPassword;
            }
            else
            {
                strPassword = null;
            }
            bRememberPassword = lpfPassword;
            strError = null;
            return true;
        }

        internal string GetErrorString(int nErrorValue)
        {
            if ((nErrorValue >= 600) && (nErrorValue < 0x2f2))
            {
                int cBufSize = 0x100;
                string lpszErrorString = new string(new char[cBufSize]);
                if (RasGetErrorString(nErrorValue, lpszErrorString, cBufSize) != 0)
                {
                    lpszErrorString = null;
                }
                if ((lpszErrorString != null) && (lpszErrorString.Length > 0))
                {
                    return lpszErrorString;
                }
            }
            //return null;  河口三矿服务器发生错误,未将对象引用设置到对象的实例。20100517修改成返回其他值
            return "unknown";
        }

        public bool HangUp(out string strError)
        {
            this.bConnected = false;
            if (this.hrasconn != 0)
            {
                int nErrorValue = RasHangUp(this.hrasconn);
                if (nErrorValue != 0)
                {
                    strError = this.GetErrorString(nErrorValue);
                    this.ConnectNotify(strError, 0);
                    return false;
                }
            }
            foreach (RASCONN rasconn in this.Rasconn)
            {
                if (rasconn.hrasconn != 0)
                {
                    int num2 = RasHangUp(rasconn.hrasconn);
                    if (num2 != 0)
                    {
                        strError = this.GetErrorString(num2);
                        this.ConnectNotify(strError, 0);
                        return false;
                    }
                }
            }
            strError = null;
            this.ConnectNotify("连接中断.", 0);
            return true;
        }

        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasCreatePhonebookEntry(int hwnd, string lpszPhonebook);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasDeleteEntry(string lpszPhonebook, string lpszEntry);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasDial(int lpRasDialExtensions, string lpszPhonebook, ref RASDIALPARAMS lpRasDialParams, int dwNotifierType, RasDialEvent lpvNotifier, ref int lphRasConn);
        private void RasDialFunc(uint unMsg, RASCONNSTATE rasconnstate, int dwError)
        {
            if (dwError != 0)
            {
                this.ConnectNotify(this.GetErrorString(dwError), 3);
                this.bConnected = false;
                if (this.hrasconn != 0)
                {
                    int nErrorValue = RasHangUp(this.hrasconn);
                    if (nErrorValue == 0)
                    {
                        this.ConnectNotify("连接中断.", 0);
                    }
                    else
                    {
                        this.ConnectNotify(this.GetErrorString(nErrorValue), 0);
                    }
                }
            }
            else
            {
                if (rasconnstate == RASCONNSTATE.RASCS_PortOpened)
                {
                    this.ConnectNotify("端口已经打开.", 1);
                }
                if (rasconnstate == RASCONNSTATE.RASCS_ConnectDevice)
                {
                    this.ConnectNotify("正在拨...", 1);
                }
                if (rasconnstate == RASCONNSTATE.RASCS_Authenticate)
                {
                    this.ConnectNotify("正在验证用户名与密码.", 1);
                }
                if (rasconnstate == RASCONNSTATE.RASCS_Connected)
                {
                    this.bConnected = true;
                    this.ConnectNotify("成功连接到" + this.EntryName + '.', 2);
                }
            }
        }

        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasEditPhonebookEntry(int hwnd, string lpszPhonebook, string lpszEntryName);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasEnumConnections([In, Out] RASCONN[] lprasconn, ref int lpcb, ref int lpcConnections);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasEnumEntries(string reserved, string lpszPhonebook, [In, Out] RASENTRYNAME[] lprasentryname, ref int lpcb, ref int lpcEntries);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasGetConnectStatus(int hrasconn, ref RASCONNSTATUS lprasconnstatus);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasGetEntryDialParams(string lpszPhonebook, ref RASDIALPARAMS lprasdialparams, ref bool lpfPassword);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasGetErrorString(int uErrorValue, string lpszErrorString, int cBufSize);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasHangUp(int hrasconn);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasRenameEntry(string lpszPhonebook, string lpszOldEntry, string lpszNewEntry);
        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        private static extern int RasSetEntryDialParams(string lpszPhonebook, ref RASDIALPARAMS lprasdialparams, bool fRemovePassword);
        public bool RenameEntry(string strOldEntry, string strNewEntry, out string strError)
        {
            int nErrorValue = RasRenameEntry(null, strOldEntry, strNewEntry);
            if (nErrorValue == 0)
            {
                strError = null;
                return true;
            }
            strError = this.GetErrorString(nErrorValue);
            return false;
        }

        public void SetDefaultEntry(string strEntry)
        {
            if ((strEntry != null) && (strEntry.Length > 0))
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\RAS AutoDial\Default", true);
                if (key == null)
                {
                    key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\RAS AutoDial\Default");
                }
                key.SetValue("DefaultInternet", strEntry);
            }
        }

        public bool SetEntryParams(string strEntryName, string strPhoneNumber, string strUserName, string strPassword, bool bRememberPassword, out string strError)
        {
            RASDIALPARAMS structure = new RASDIALPARAMS();
            structure.dwSize = Marshal.SizeOf(structure);
            structure.szEntryName = strEntryName;
            structure.szPhoneNumber = strPhoneNumber;
            structure.szUserName = strUserName;
            structure.szPassword = strPassword;
            int nErrorValue = RasSetEntryDialParams(null, ref structure, !bRememberPassword);
            if (nErrorValue != 0)
            {
                strError = this.GetErrorString(nErrorValue);
                return false;
            }
            strError = null;
            return true;
        }

        private void TimerEvent(object sender, ElapsedEventArgs e)
        {
            RASCONNSTATUS structure = new RASCONNSTATUS();
            int lpcb = 0;
            int lpcConnections = 0;
            structure.dwSize = Marshal.SizeOf(structure);
            int nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections);
            switch (nErrorValue)
            {
                case 0:
                    break;

                case 0x25b:
                    this.Rasconn = new RASCONN[lpcConnections];
                    lpcb = this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]);
                    nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections);
                    break;

                default:
                    this.ConnectNotify(this.GetErrorString(nErrorValue), 3);
                    return;
            }
            if (nErrorValue != 0)
            {
                this.ConnectNotify(this.GetErrorString(nErrorValue), 3);
            }
            else if ((lpcConnections < 1) && this.bConnected)
            {
                this.bConnected = false;
                this.ConnectNotify("连接中断.", 0);
            }
            else
            {
                for (int i = 0; i < lpcConnections; i++)
                {
                    nErrorValue = RasGetConnectStatus(this.Rasconn[i].hrasconn, ref structure);
                    if (nErrorValue != 0)
                    {
                        this.ConnectNotify(this.GetErrorString(nErrorValue), 3);
                        return;
                    }
                    if ((structure.rasconnstate == RASCONNSTATE.RASCS_Connected) && !this.bConnected)
                    {
                        this.bConnected = true;
                        this.ConnectNotify("成功连接到" + this.Rasconn[i].szEntryName + '.', 2);
                    }
                    if ((structure.rasconnstate == RASCONNSTATE.RASCS_Disconnected) && this.bConnected)
                    {
                        this.bConnected = false;
                        this.ConnectNotify("连接中断.", 0);
                    }
                }
            }
        }

        // Nested Types
      
        public delegate void ConnectionNotify(string strNotify, int bConnect);
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct RASCONN
        {
            internal int dwSize;
            internal int hrasconn;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]
            internal string szEntryName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x11)]
            internal string szDeviceType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]
            internal string szDeviceName;
        }

        private enum RASCONNSTATE
        {
            RASCS_AllDevicesConnected = 4,
            RASCS_AuthAck = 12,
            RASCS_AuthCallback = 8,
            RASCS_AuthChangePassword = 9,
            RASCS_Authenticate = 5,
            RASCS_Authenticated = 14,
            RASCS_AuthLinkSpeed = 11,
            RASCS_AuthNotify = 6,
            RASCS_AuthProject = 10,
            RASCS_AuthRetry = 7,
            RASCS_CallbackSetByCaller = 0x1002,
            RASCS_ConnectDevice = 2,
            RASCS_Connected = 0x2000,
            RASCS_DeviceConnected = 3,
            RASCS_Disconnected = 0x2001,
            RASCS_Interactive = 0x1000,
            RASCS_OpenPort = 0,
            RASCS_PasswordExpired = 0x1003,
            RASCS_PortOpened = 1,
            RASCS_PrepareForCallback = 15,
            RASCS_Projected = 0x12,
            RASCS_ReAuthenticate = 13,
            RASCS_RetryAuthentication = 0x1001,
            RASCS_SubEntryConnected = 0x13,
            RASCS_SubEntryDisconnected = 20,
            RASCS_WaitForCallback = 0x11,
            RASCS_WaitForModemReset = 0x10
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct RASCONNSTATUS
        {
            internal int dwSize;
            internal Ras.RASCONNSTATE rasconnstate;
            internal int dwError;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x11)]
            internal string szDeviceType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]
            internal string szDeviceName;
        }

        //private delegate void RasDialEvent(uint unMsg, Ras.RASCONNSTATE rasconnstate, int dwError);
        private delegate void RasDialEvent(uint unMsg, Ras.RASCONNSTATE rasconnstate, int dwError);
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct RASDIALPARAMS
        {
            internal int dwSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]
            internal string szEntryName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]
            internal string szPhoneNumber;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]
            internal string szCallbackNumber;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]
            internal string szUserName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]
            internal string szPassword;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x10)]
            internal string szDomain;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct RASENTRYNAME
        {
            internal int dwSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]
            internal string szEntryName;
        }
    }
}


分享到:
评论

相关推荐

    C#自动拨号【ADSL】【源代码】

    C#自动拨号【ADSL】【源代码】,不错的源码

    wince for c# 拨号源代码

    wince下c#拨号源代码,使用wince API直接拨号,挂断,检测连接。用于ANY cpu 。适合wince/mobile下用c#开发的用户。

    杭州三汇语音卡电话拨号程序源代码(c#)

    用户拨打电话时系统自动播放已设定好的欢迎词。 客户通过按键选择,从而可以听到预先设置好的语音信息。

    东进语音卡拨号程序源代码(c#)

     客户在拨打企业电话客服时,系统会自动接收、识别并记录来电的主叫号码并显示相应的客户信息。  客户电话打入时系统自动播放已设定好的欢迎词。  用户在拨打企业电话客服后,系统会进行自动语音引导,客户...

    C#adsl重拨换IP源代码

    调用示例 string strError = ""; new RasApi().HangUp(XmlHelper.AdslName, out strError); bool fh = new RasApi().DialUp(XmlHelper.AdslName, XmlHelper.AdslUserName, XmlHelper.AdslPassWord, out strError);...

    adsl拨号器的C#源代码

    1: class AutoDialer 2: { 3: public void Connect(string connectionName, string user, string pass) 4: { 5: string arg = string.Format("rasdial \"{0}\" {1} {2}", connectionName, user, pass);...

    酷派N900手机:实现自动拨号、电话、短信、日历、拍照、打印等功能(含源代码)适用于Mobile手机。

    酷派N900手机:实现自动拨号、电话、短信、日历、拍照、打印等功能(含源代码)适用于Mobile手机。开发环境:c#+vs2005+wince5.0/mobile6.0SDK,直接打开工程就可以编译,也可以通过数据线直接部署到手机上。 现在...

    3G拨号 Wince

    3G拨号 Wince 源代码,适用于中兴的模块MG3732,MC8630

    C#启动电话拨号程序

    摘要:C#源码,网络相关,电话拨号程序 C#启动电话拨号程序,一个简单的电话拨号小程序,源码适合新手参考。

    C#写的网卡定时开关软件

     软件使用.net2.0开发,开放源代码,您可以根据需要做进一步的开发。 功能:  1、定时控制指定网卡的禁用与启用  2、支持开机启动  3、最小化时自动缩至托盘 使用心得:  我们公司有三个办公区,公司领导要求对...

    vc++ 应用源码包_6

    Visual.C++编程技巧精选500例源代码 内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与...

    vc++ 应用源码包_5

    Visual.C++编程技巧精选500例源代码 内含各种例子(vc下各种控件的使用方法、标题栏与菜单栏、工具栏与状态栏、图标与光标、程序窗口、程序控制、进程与线程、字符串、文件读写操作、文件与文件夹属性操作、文件与...

    ADSL拨号程序C#源码实例

    摘要:C#源码,网络相关,拨号程序,ADSL 基于C#的ADSL拨号程序,自己定义一个拨号名称,输入已知的ADSL帐户名和密码,就可以尝试进行拨号,程序相对不太复杂,为了顺利编译项目文件,请在VS2008下运行这些文件。

    C#源码大集合 02(共3卷)

    └ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程互斥 ├─chap09-多线程 ├─D00-多线程 ├─Windows多线程编程技术与实例 ... ... ├─多线程,多接收...

    vc++ 开发实例源码包

    《远程控制编程技术》源代码 内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法...

    Xamrin Android开发实战 基础下篇 源代码

    Xamrin Android开发实战 基础下篇 源代码 下册目录 第7章 选择器 1 7.1 时间选择器 1 7.2 日期选择器 3 7.3 自定义选择器 8 7.3.1 显示自定义选择器 8 7.3.2 填充自定义选择器 10 第8章 手势 15 8.1 轻拍 15 8.2 ...

    利用Wininet实现建立Internet拨号连接和断开连接(2KB)...

    利用Wininet实现建立Internet拨号连接和断开连接(2KB)

    C#源码大集合 03(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent &lt;br&gt;└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

    GPRS源码工程

    该工程项目包含了GPRS拨号,状态监测,断开连接等等功能,可以直接使用

    C#源码大集合 01(共3卷)

    ├─第53讲 文件系统监视器 │ └─第55讲 聊天室扩展之三——使用MS Agent &lt;br&gt;└ C#源代码第三部分多线程 ├─1-多线程 ├─2-多线程 ├─3-多线程互斥 ├─4-多线程互斥 ├─5-多线程...

Global site tag (gtag.js) - Google Analytics