public static void SetIEProxy(bool enable, bool global, string proxyServer, string pacURL) { string[] allConnections = null; var ret = RAS.GetAllConns(ref allConnections); if (ret == 2) { throw new Exception("Cannot get all connections"); } if (ret == 1) { // no entries, only set LAN SetIEProxy(enable, global, proxyServer, pacURL, null); } else if (ret == 0) { // found entries, set LAN and each connection SetIEProxy(enable, global, proxyServer, pacURL, null); foreach (string connName in allConnections) { SetIEProxy(enable, global, proxyServer, pacURL, connName); } } }
/// <summary> /// Get all entries from RAS /// </summary> /// <param name="allConns"></param> /// <returns> /// 0: success with entries /// 1: success but no entries found /// 2: failed /// </returns> public static uint GetAllConns(ref string[] allConns) { int lpNames = 1; int entryNameSize = 0; int lpSize = 0; uint retval = ERROR_SUCCESS; RasEntryName[] names = null; entryNameSize = Marshal.SizeOf(typeof(RasEntryName)); lpSize = lpNames * entryNameSize; names = new RasEntryName[lpNames]; names[0].dwSize = entryNameSize; retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames); //if we have more than one connection, we need to resize if (retval == ERROR_BUFFER_TOO_SMALL) { 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); } if (retval == ERROR_SUCCESS) { if (lpNames == 0) { // no entries found. return(1); } allConns = new string[names.Length]; for (int i = 0; i < names.Length; i++) { allConns[i] = names[i].szEntryName; } return(0); } else { return(2); } }