示例#1
0
文件: RasApi.cs 项目: cnucky/huan
        public static string GetIP(string strEntryName)
        {
            RASCONN[] Rasconn = new RASCONN[1];
            Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]);
            RASCONNSTATUS structure      = new RASCONNSTATUS();
            int           lpcb           = 0;
            int           lpcConnections = 0;

            structure.dwSize = Marshal.SizeOf(structure);
            int nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections);

            switch (nErrorValue)
            {
            case 0:
                break;

            case 0x25b:
                Rasconn     = new RASCONN[lpcConnections];
                lpcb        = Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]);
                nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections);
                break;
                //default:
                //ConnectNotify(this.GetErrorString(nErrorValue), 3);
                //return;
            }
            int lphrsaConn = 0;

            foreach (RASCONN rasconn in Rasconn.Where(rasconn => rasconn.szEntryName == strEntryName))
            {
                if (rasconn.hrasconn != 0)
                {
                    lphrsaConn = rasconn.hrasconn;
                }
            }
            if (lphrsaConn == 0)
            {
                return("");
            }
            RASPPPIP structProjection = new RASPPPIP();
            uint     uiProjSize       = (uint)structProjection.dwSize;

            try {
                uint errorCode = RasGetProjectionInfo(lphrsaConn, RASPROJECTION.RASP_PppIp, structProjection, ref uiProjSize);
                return(structProjection.szIpAddress);
            } catch {
                return("");
            }
        }
示例#2
0
文件: RasApi.cs 项目: cnucky/huan
        /*
         * m_connected = true;
         *
         *  RAS lpras = new RAS();
         *
         *  RASCONN lprasConn = new RASCONN();
         *
         *  lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
         *  lprasConn.hrasconn = IntPtr.Zero;
         *
         *  int lpcb = 0;
         *  int lpcConnections = 0;
         *  int nRet = 0;
         *  lpcb = Marshal.SizeOf(typeof(RASCONN));
         *
         *
         *  nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, ref lpcConnections);
         *
         *
         *  if (nRet != 0)
         *  {
         *      m_connected = false;
         *      return;
         *
         *  }
         *
         *  if (lpcConnections > 0)
         *  {
         *
         *
         *      //for (int i = 0; i < lpcConnections; i++)
         *
         *      //{
         *      RasStats stats = new RasStats();
         *
         *      m_ConnectedRasHandle = lprasConn.hrasconn;
         *      RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);
         *
         *
         *      m_ConnectionName = lprasConn.szEntryName;
         *
         *      int Hours = 0;
         *      int Minutes = 0;
         *      int Seconds = 0;
         *
         *      Hours = ((stats.dwConnectionDuration / 1000) / 3600);
         *      Minutes = ((stats.dwConnectionDuration / 1000) / 60) - (Hours * 60);
         *      Seconds = ((stats.dwConnectionDuration / 1000)) - (Minutes * 60) - (Hours * 3600);
         *
         *
         *      m_duration = Hours + " hours " + Minutes + " minutes " + Seconds + " secs";
         *      m_TX = stats.dwBytesXmited;
         *      m_RX = stats.dwBytesRcved;
         *
         *
         *      //}
         *
         *
         *  }
         *  else
         *  {
         *      m_connected = false;
         *
         *  }
         */

        public bool HangUp(string strEntryName, out string strError)
        {
            strError = "";
            RASCONN[] Rasconn = new RASCONN[1];
            Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]);
            RASCONNSTATUS structure      = new RASCONNSTATUS();
            int           lpcb           = 0;
            int           lpcConnections = 0;

            structure.dwSize = Marshal.SizeOf(structure);
            lpcb             = 0x500;
            int nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections);

            switch (nErrorValue)
            {
            case 0:
                break;

            case 0x25b:
                Rasconn     = new RASCONN[lpcConnections];
                lpcb        = Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]);
                nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections);
                break;
                //default:
                //ConnectNotify(this.GetErrorString(nErrorValue), 3);
                //return;
            }
            foreach (RASCONN rasconn in Rasconn.Where(rasconn => rasconn.szEntryName == strEntryName))
            {
                if (rasconn.hrasconn != 0)
                {
                    int num2 = RasHangUp(rasconn.hrasconn);
                    if (num2 != 0)
                    {
                        //strError = this.GetErrorString(num2);
                        //this.ConnectNotify(strError, 0);
                        return(false);
                    }
                }
            }
            strError = null;
            return(true);
        }