C#拨号类一

using System;
using System.Runtime.InteropServices;

namespace Rainfall.Base.CNetWork
{
    public class CRasDial
    {
        public const int RAS_MaxEntryName = 256;
        public const int RAS_MaxPhoneNumber = 128;
        public const int UNLEN = 256;
        public const int PWLEN = 256;
        public const int DNLEN = 15;
        public const int MAX_PATH = 260;
        public const int RAS_MaxDeviceType = 16;
        public const int RAS_MaxCallbackNumber = RAS_MaxPhoneNumber;

        public delegate void Callback(uint unMsg, int rasconnstate, int dwError);

        [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)]
        public struct RASDIALPARAMS
        {
            public int dwSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
            public string szEntryName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPhoneNumber + 1)]
            public string szPhoneNumber;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxCallbackNumber + 1)]
            public string szCallbackNumber;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = UNLEN + 1)]
            public string szUserName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PWLEN + 1)]
            public string szPassword;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DNLEN + 1)]
            public string szDomain;
            public int dwSubEntry;
            public int dwCallbackId;
        }

        [DllImport("rasapi32.dll ", CharSet = CharSet.Auto)]
        public static extern int RasDial(int lpRasDialExtensions, string lpszPhonebook, ref   RASDIALPARAMS lprasdialparams, int dwNotifierType, Callback lpvNotifier, ref   int lphRasConn);

        [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
        public extern static int RasHangUp(int hrasconn); // handle to the RAS connection to hang up );

        private RASDIALPARAMS RasDialParams;
        private int rasSession;

        public CRasDial()
        {
            rasSession = 0;
            RasDialParams = new RASDIALPARAMS();
            RasDialParams.dwSize = Marshal.SizeOf(RasDialParams);
        }

        #region   Properties
        public string UserName
        {
            get
            {
                return RasDialParams.szUserName;
            }
            set
            {
                RasDialParams.szUserName = value;
            }
        }

        public string Password
        {
            get
            {
                return RasDialParams.szPassword;
            }
            set
            {
                RasDialParams.szPassword = value;
            }
        }

        public string EntryName
        {
            get
            {
                return RasDialParams.szEntryName;
            }
            set
            {
                RasDialParams.szEntryName = value;
            }
        }
        #endregion

        public int DialUp()
        {
            RasDialParams.szEntryName += "\0 ";
            RasDialParams.szUserName += "\0 ";
            RasDialParams.szPassword += "\0 ";
            int result = RasDial(0nullref   RasDialParams, 0nullref rasSession);
            return result;
        }
        public void HangUp()
        {
            if (rasSession != 0)
            {
                int lStatus = RasHangUp(rasSession);
                if (lStatus != 0)
                {
                  //  throw new ApplicationException("RasHangUp(" + rasSession + ") failed with error code: " + lStatus);
                }
                rasSession = 0;
            }
            else
            {
               // OnDisconnected();
            }
        }

        public int DisConnect()
        {
          //  int result = RasHangUp(rasSession);
            return result;
        }
    } 
}
posted @ 2012-01-13 09:35  base  阅读(814)  评论(0编辑  收藏  举报