Beispiel #1
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;
 }
Beispiel #2
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);
            }
        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();
        }