Esempio n. 1
0
        public static bool disconnectVPN(string vpnName)
        {
            int cb = 0, connectionCount;

            if (RasEnumConnections(null, ref cb, out connectionCount) == ERROR_BUFFER_TOO_SMALL)
            {
                if (connectionCount == 0)
                {
                    return(false);
                }
                RASCONN[] buffer = new RASCONN[connectionCount];
                buffer[0].dwSize = Marshal.SizeOf(typeof(RASCONN));


                if (RasEnumConnections(buffer, ref cb, out connectionCount) == ERROR_SUCCESS)
                {
                    foreach (RASCONN rasConn in buffer)
                    {
                        if (rasConn.szEntryName == vpnName)
                        {
                            if (RasHangUp(rasConn.hrasconn) == 0)
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            return(false);
        }
        public bool IsConnected()
        {
            RASCONN[] rasStructs = GetRasConnections();

            RASCONNSTATUS rasConnStatus = new RASCONNSTATUS();

            rasConnStatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS));

            for (int i = 0; i < rasConnectionsAmount; ++i)
            {
                // Pobranie pojedynczej struktury
                RASCONN rStruct = rasStructs[i];

                int statusResult = RasGetConnectStatus(rStruct.hrasconn, ref rasConnStatus);
                if (statusResult != 0)
                {
                    throw new Win32Exception(statusResult);
                }
                if (rasConnStatus.rasconnstate == RAS_Connected)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 3
0
        public void RasEnumConnectionsAsExpected()
        {
            var lpCb          = 0;
            var lpConnections = 1;
            var lpRasConn     = new RASCONN[0];

            api.Setup(o => o.RasEnumConnections(lpRasConn, ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(new RasEnumConnectionsCallback(
                                                                                                                          (RASCONN[] o1, ref int o2, ref int o3) =>
            {
                o2 = 1;
                o3 = 2;
                return(SUCCESS);
            }));

            eventLoggingPolicy.Setup(o => o.LogEvent(It.IsAny <EventLevel>(), It.IsAny <PInvokeInt32CallCompletedTraceEvent>())).Callback(new LogEventCallback(
                                                                                                                                              (level, o1) =>
            {
                Assert.AreEqual(EventLevel.Verbose, level);

                var eventData = (PInvokeInt32CallCompletedTraceEvent)o1;
                Assert.True(eventData.Args.ContainsKey(nameof(lpRasConn)));
                Assert.AreEqual(0, (int)eventData.Args[nameof(lpCb)]);
                Assert.AreEqual(1, (int)eventData.Args[nameof(lpConnections)]);
                Assert.AreEqual(1, (int)eventData.OutArgs[nameof(lpCb)]);
                Assert.AreEqual(2, (int)eventData.OutArgs[nameof(lpConnections)]);
                Assert.True(eventData.Duration > TimeSpan.Zero);
                Assert.AreEqual(SUCCESS, eventData.Result);
            })).Verifiable();

            var target = new RasApi32LoggingAdvice(api.Object, eventLoggingPolicy.Object);
            var result = target.RasEnumConnections(lpRasConn, ref lpCb, ref lpConnections);

            eventLoggingPolicy.Verify();
            Assert.AreEqual(SUCCESS, result);
        }
Esempio n. 4
0
        public static uint UpLink(string phoneBookEntry)
        {
            if (phoneBookEntry == String.Empty)
            {
                throw new ArgumentNullException("phoneBookEntry");
            }
            RASCONN[] rasConnArr = GetAllConnections();
            RASCONN   rConn      = new RASCONN();

            rConn =
                Array.Find <RASCONN>(rasConnArr,
                                     delegate(RASCONN rasConn)
            {
                return(rasConn.szEntryName == phoneBookEntry ? true : false);
            });
            if (rConn.hrasconn != IntPtr.Zero)
            {
                RasHangUp(rConn.hrasconn);
                Thread.Sleep(3000);
            }

            IntPtr        hConnection   = IntPtr.Zero;
            RASDIALPARAMS rasDialParams = new RASDIALPARAMS();

            rasDialParams.dwSize       = Marshal.SizeOf(rasDialParams);
            rasDialParams.szEntryName += phoneBookEntry;
            rasDialParams.szUserName  += "\0";
            rasDialParams.szPassword  += "\0";
            UInt32 result = RasDial(0, null, ref rasDialParams, 0, null, ref hConnection);

            return(result);
        }
Esempio n. 5
0
        public static RASCONN[] GetActiveConnections()
        {
            RASCONN[] connections = new RASCONN[1];
            connections[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
            //Get entries count
            int connectionsCount = 0;
            int cb   = Marshal.SizeOf(typeof(RASCONN));
            int nRet = RasEnumConnections(connections, ref cb, out connectionsCount);

            if (nRet != (int)winerror.ERROR_SUCCESS && nRet != (int)winerror.ERROR_BUFFER_TOO_SMALL)
            {
                throw new Exception("RasEnumConnections" + nRet.ToString());
            }
            if (connectionsCount == 0)
            {
                return(new RASCONN[0]);
            }

            // create array with specified entries number
            connections = new RASCONN[connectionsCount];
            for (int i = 0; i < connections.Length; i++)
            {
                connections[i].dwSize = Marshal.SizeOf(typeof(RASCONN));
            }
            nRet = RasEnumConnections(connections, ref cb, out connectionsCount);
            if (nRet != (int)winerror.ERROR_SUCCESS)
            {
                throw new Exception("RasEnumConnections" + nRet.ToString());
            }

            return(connections);
        }
Esempio n. 6
0
        /// <summary>
        /// 是否正在连接
        /// </summary>
        /// <returns></returns>
        public bool isConnect()
        {
            bool    m_connected = true;
            RASCONN lprasConn   = new RASCONN();

            //lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
            lprasConn.dwSize   = 412;
            lprasConn.hrasconn = IntPtr.Zero;

            int lpcb           = 0;
            int lpcConnections = 0;
            int nRet           = 0;

            lpcb = Marshal.SizeOf(typeof(RASCONN));
            nRet = RasEnumConnections(ref lprasConn, ref lpcb, ref lpcConnections);
            if (nRet != 0)
            {
                m_connected = false;
            }
            if (lpcConnections == 0)
            {
                m_connected = false;
            }
            if (lpcConnections > 0)
            {
                m_ConnectedRasHandle = lprasConn.hrasconn;
            }

            return(m_connected);
        }
Esempio n. 7
0
 /// <summary>
 /// Pobranie wszystkich połączeń RAS.
 /// </summary>
 /// <returns>Struktury połączeń RAS</returns>
 private RASCONN[] GetRasConnections()
 {
     // Stworzenie tablicy, którą później należy przekazać funkcjom
     int rasEnumReturn;
     RASCONN[] rasconnStructs = new RASCONN[256];
     rasconnStructs.Initialize(); // inicjalizacja wszystkich pól struktury
     rasconnStructs[0].dwSize = Marshal.SizeOf(typeof(RASCONN)); // inicjalizacja pierwszego pola pierwszej struktury na wartość wielkości tej struktury
     int sizeOfRasconnStruct = rasconnStructs[0].dwSize * rasconnStructs.Length; // wielkość pojedynczej struktury * ilosc
     // Wywołanie RasEnumConnections do zdobycia wszystkich aktywnych połączeń RAS
     rasEnumReturn = RasEnumConnections(rasconnStructs, ref sizeOfRasconnStruct, ref rasConnectionsAmount);
     // jeżeli RasEnumConnections nie zwróciło ERROR_SUCCESS
     if (rasEnumReturn != 0) throw new Win32Exception(rasEnumReturn);
     return rasconnStructs;
 }
Esempio n. 8
0
 /// <summary>
 /// Rozłącza internet.
 /// </summary>
 public void Disconnect()
 {
     RASCONN[] rasStructs = GetRasConnections();
     // Przejście przez każdą strukturę RASCONN
     for (int i = 0; i < rasConnectionsAmount; i++)
     {
         // Pobranie pojedynczej struktury
         RASCONN rStruct = rasStructs[i];
         // Jeżeli uchwyt do połączenia wynosi 0, to brak połączenia
         if (rStruct.hrasconn == IntPtr.Zero) continue; // i następna struktura...
         // Rozłączenie...
         RasHangUp(rStruct.hrasconn);
     }
 }
Esempio n. 9
0
        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("");
            }
        }
Esempio n. 10
0
        /*
         * 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);
        }
Esempio n. 11
0
        private RasConnection CreateConnection(RASCONN hRasConn)
        {
            var device = deviceTypeFactory.Create(hRasConn.szDeviceName, hRasConn.szDeviceType);

            if (device == null)
            {
                throw new InvalidOperationException("The device was not created.");
            }

            return(new RasConnection(
                       hRasConn.hrasconn,
                       device,
                       hRasConn.szEntryName,
                       hRasConn.szPhonebook,
                       hRasConn.guidEntry,
                       CreateConnectionOptions(hRasConn),
                       hRasConn.luid,
                       hRasConn.guidCorrelationId,
                       serviceLocator));
        }
Esempio n. 12
0
            /// <summary>
            /// 断开所有链接
            /// </summary>
            /// <returns></returns>
            public int HangAllConnection()
            {
                int flags = 0;

                InternetGetConnectedState(ref flags, 0);
                if (!((flags & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED))
                {
                    throw new NotSupportedException();
                }

                //create array of structures to pass to API
                int ret;
                int conns = 0;

                RASCONN[] rarr = new RASCONN[256];
                rarr.Initialize();
                rarr[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
                int lr = rarr[0].dwSize * rarr.Length;

                //call RasEnumConnections to loop all RAS connections
                ret = RasEnumConnections(rarr, ref lr, ref conns);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
                //loop through each RASCONN struct
                for (int i = 0; i < conns; i++)
                {
                    //retrieve RASCONN struct
                    RASCONN r = rarr[i];

                    //if connection bad, handle will be 0
                    if (r.hrasconn == IntPtr.Zero)
                    {
                        continue;
                    }
                    return(RasHangUp(r.hrasconn));
                }

                return(-1);
            }
Esempio n. 13
0
        /// <summary>
        /// Returns all active RAS connections as an array of data structure RASCONN
        /// </summary>
        /// <returns></returns>
        private static RASCONN[] GetAllConnections()
        {
            RASCONN[] tempConn = new RASCONN[1];
            RASCONN[] allConnections = tempConn;

            tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
            int lpcb = tempConn[0].dwSize;
            int lpcConnections = 0;
            uint ret = RasEnumConnections(tempConn, ref lpcb, out lpcConnections);
            if (ret == ERROR_INVALID_SIZE)
            {
                throw new Exception("RAS: RASCONN data structure has invalid format");
            }
            else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
            {
                // first call returned that there are more than one  connections
                // and more memory is required
                allConnections = new RASCONN[lpcb / Marshal.SizeOf(typeof(RASCONN))];
                allConnections[0] = tempConn[0];
                ret = RasEnumConnections(allConnections, ref lpcb, out lpcConnections);
            }

            // Check errors
            if (ret != SUCCESS)
            {
                throw new Exception("RAS returns error: " + ret);
            }
            if (lpcConnections > allConnections.Length)
            {
                throw new Exception("RAS: error retrieving correct connection count");
            }
            else if (lpcConnections == 0)
            {
                // if there are no connections resize the data structure
                allConnections = new RASCONN[0];
            }

            return allConnections;
        }
Esempio n. 14
0
        private static RASCONN[] GetAllConnections()
        {
            RASCONN[] tempConn       = new RASCONN[1];
            RASCONN[] allConnections = tempConn;

            tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
            int lpcb           = tempConn[0].dwSize;
            int lpcConnections = 0;
            int ret            = RasEnumConnections(tempConn, ref lpcb, out lpcConnections);

            if (ret == ERROR_INVALID_SIZE)
            {
                throw new Exception("RAS: RASCONN data structure has invalid format");
            }
            else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
            {
                // first call returned that there are more than one connections
                // and more memory is required
                allConnections    = new RASCONN[lpcb / Marshal.SizeOf(typeof(RASCONN))];
                allConnections[0] = tempConn[0];
                ret = RasEnumConnections(allConnections, ref lpcb, out lpcConnections);
            }

            // Check errors
            if (ret != SUCCESS)
            {
                throw new Exception("RAS returns error: " + ret);
            }
            if (lpcConnections > allConnections.Length)
            {
                throw new Exception("RAS: error retrieving correct connection count");
            }
            else if (lpcConnections == 0)
            {
                allConnections = new RASCONN[0];
            }

            return(allConnections);
        }
        private RasConnection CreateConnection(RASCONN hRasConn)
        {
            var device = deviceTypeFactory.Create(hRasConn.szDeviceName, hRasConn.szDeviceType);

            if (device == null)
            {
                throw new InvalidOperationException("The device was not created.");
            }

            return(new RasConnection(
                       hRasConn.hrasconn,
                       device,
                       hRasConn.szEntryName,
                       hRasConn.szPhonebook,
                       hRasConn.guidEntry,
                       CreateConnectionOptions(hRasConn),
                       hRasConn.luid,
                       hRasConn.guidCorrelationId,
                       serviceLocator.GetRequiredService <IRasGetConnectStatus>(),
                       serviceLocator.GetRequiredService <IRasGetConnectionStatistics>(),
                       serviceLocator.GetRequiredService <IRasHangUp>(),
                       serviceLocator.GetRequiredService <IRasClearConnectionStatistics>()));
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            //make sure that RAS is installed
            int flags = 0;

            InternetGetConnectedState(ref flags, 0);
            if (!((flags & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED))
            {
                throw new NotSupportedException();
            }

            //create array of structures to pass to API
            int ret;
            int conns = 0;

            RASCONN[] rarr = new RASCONN[256];
            rarr.Initialize();
            rarr[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
            int lr = rarr[0].dwSize * rarr.Length;

            //call RasEnumConnections to loop all RAS connections
            ret = RasEnumConnections(rarr, ref lr, ref conns);
            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            //loop through each RASCONN struct
            for (int i = 0; i < conns; i++)
            {
                //retrieve RASCONN struct
                RASCONN r = rarr[i];

                //if connection bad, handle will be 0
                if (r.hrasconn == IntPtr.Zero)
                {
                    continue;
                }

                //get status of RAS connection
                RASCONNSTATUS rcs = new RASCONNSTATUS();
                rcs.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS));

                ret = RasGetConnectStatus(r.hrasconn, ref rcs);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }

                //print useful information to the console for each RAS connection
                Console.WriteLine("RAS CONNECTION {0}:", i + 1);
                Console.WriteLine("\tDevice Name : {0}", r.szDeviceName);
                Console.WriteLine("\tDevice Type : {0}", r.szDeviceType);
                Console.WriteLine("\tPhone Number: {0}", rcs.szPhoneNumber);
                Console.WriteLine("\tConnected?  : {0}",
                                  rcs.rasconnstate == RAS_Connected);
                Console.WriteLine("\tEntry Name  : {0}", r.szEntryName);
                Console.WriteLine("\tPhone Book  : {0}", r.szPhonebook);
                Console.WriteLine();
            }

            Console.ReadLine();
        }
Esempio n. 17
0
    public RASDisplay()
    {
        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;
        }


        int lpNames       = 1;
        int entryNameSize = 0;
        int lpSize        = 0;

        RasEntryName[] names = null;

        entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
        lpSize        = lpNames * entryNameSize;

        names           = new RasEntryName[lpNames];
        names[0].dwSize = entryNameSize;

        uint retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);

        //if we have more than one connection, we need to do it again
        if (lpNames > 1)
        {
            names = new RasEntryName[lpNames];
            for (int i = 0; i < names.Length; i++)
            {
                names[i].dwSize = entryNameSize;
            }

            retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);
        }
        m_ConnectionNames = new string[names.Length];


        if (lpNames > 0)
        {
            for (int i = 0; i < names.Length; i++)
            {
                m_ConnectionNames[i] = names[i].szEntryName;
            }
        }
    }
Esempio n. 18
0
 internal static extern int RasEnumConnections
 (
     ref RASCONN lprasconn, // buffer to receive connections data
     ref int lpcb,          // size in bytes of buffer
     ref int lpcConnections // number of connections written to buffer
 );
 internal static string GetCurrentConnectoid()
 {
     uint num = (uint) Marshal.SizeOf(typeof(RASCONN));
     if (s_RasSupported)
     {
         uint lpcConnections = 4;
         uint num3 = 0;
         RASCONN[] lprasconn = null;
         while (true)
         {
             uint lpcb = num * lpcConnections;
             lprasconn = new RASCONN[lpcConnections];
             lprasconn[0].dwSize = num;
             num3 = RasEnumConnections(lprasconn, ref lpcb, ref lpcConnections);
             if (num3 != 0x25b)
             {
                 break;
             }
             lpcConnections = ((lpcb + num) - 1) / num;
         }
         if ((lpcConnections != 0) && (num3 == 0))
         {
             for (uint i = 0; i < lpcConnections; i++)
             {
                 RASCONNSTATUS rasconnstatus;
                 rasconnstatus = new RASCONNSTATUS {
                     dwSize = (uint) Marshal.SizeOf(rasconnstatus)
                 };
                 if ((RasGetConnectStatus(lprasconn[i].hrasconn, ref rasconnstatus) == 0) && (rasconnstatus.rasconnstate == RASCONNSTATE.RASCS_Connected))
                 {
                     return lprasconn[i].szEntryName;
                 }
             }
         }
     }
     return null;
 }
            internal static string GetCurrentConnectoid()
            {
                uint dwSize = (uint) Marshal.SizeOf(typeof(RASCONN));
                GlobalLog.Print("RasHelper::GetCurrentConnectoid() using struct size dwSize:" + dwSize);

                if (!s_RasSupported) 
                {
                    // if RAS is not supported, behave as if no dial-up/VPN connection is in use
                    // (which is actually the case, since without RAS dial-up/VPN doesn't work)
                    return null;
                }

                uint count = 4;
                uint statusCode = 0;
                RASCONN[] connections = null;
                while (true)
                {
                    uint cb = checked(dwSize * count);
                    connections = new RASCONN[count];
                    connections[0].dwSize = dwSize;
                    statusCode = RasEnumConnections(connections, ref cb, ref count);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() called RasEnumConnections() count:" + count + " statusCode: " + statusCode + " cb:" + cb);
                    if (statusCode != ERROR_BUFFER_TOO_SMALL)
                    {
                        break;
                    }
                    count = checked(cb + dwSize - 1) / dwSize;
                }

                if (count == 0 || statusCode != 0)
                {
                    return null;
                }

                for (uint i=0; i < count; i++)
                {
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "]");
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].dwSize: " + connections[i].dwSize);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].hrasconn: " + connections[i].hrasconn);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szEntryName: " + connections[i].szEntryName);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szDeviceType: " + connections[i].szDeviceType);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szDeviceName: " + connections[i].szDeviceName);

                    RASCONNSTATUS connectionStatus = new RASCONNSTATUS();
                    connectionStatus.dwSize = (uint)Marshal.SizeOf(connectionStatus);
                    // RELIABILITY:
                    // the 'hrasconn' field is an IntPtr because it's defined as a handle
                    // that said, its use is that of a opaque ID, so we're not
                    // allocating anything that needs to be released for reliability.
                    statusCode = RasGetConnectStatus(connections[i].hrasconn, ref connectionStatus);
                    GlobalLog.Print("RasHelper::GetCurrentConnectoid() called RasGetConnectStatus() statusCode: " + statusCode + " dwSize: " + connectionStatus.dwSize);
                    if (statusCode==0) {
                        GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.dwSize: " + connectionStatus.dwSize);
                        GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.rasconnstate: " + connectionStatus.rasconnstate);
                        GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.dwError: " + connectionStatus.dwError);
                        GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.szDeviceType: " + connectionStatus.szDeviceType);
                        GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.szDeviceName: " + connectionStatus.szDeviceName);
                        if (connectionStatus.rasconnstate==RASCONNSTATE.RASCS_Connected) {
                            return connections[i].szEntryName;
                        }
                    }
                }

                return null;
            }
 private RasConnectionOptions CreateConnectionOptions(RASCONN hRasConn)
 {
     return(new RasConnectionOptions(hRasConn.dwFlags));
 }