C# 中获取IE的连接设置

http://pinvoke.net/default.aspx/wininet/%20InternetQueryOption.html

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CSProxy
{
 public struct Struct_INTERNET_PROXY_INFO
 {
  public int dwAccessType; //Access type.
  public IntPtr proxy;  //Pointer to a string that contains the proxy server list 指向一个存有代理列表的字符串
  public IntPtr proxyBypass; //Pointer to a string that contains the proxy bypass list.
 };

 public class Set
 {
  [DllImport("wininet.dll", SetLastError = true)]
  private static extern bool InternetQueryOption
   (   
   IntPtr hInternet,  //Handle on which to query information.
   int dwOption,  //Internet option to be queried.
   IntPtr lpBuffer,  //Pointer to a buffer that receives the option setting.
   ref int lpdwBufferLength//Pointer to a variable that contains the size of lpBuffer, in bytes.
   );

  const int ERROR_INSUFFICIENT_BUFFER = 122;
  const int INTERNET_OPTION_PROXY = 38 ;
  public static void GetSet()
  {
   Struct_INTERNET_PROXY_INFO struct_IPI;
   IntPtr intptrStruct = IntPtr.Zero;
   int bufferLength = 0;
   try
   {
    //获取Buffer大小
    bool iReturn = InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, IntPtr.Zero, ref bufferLength);

    if ((Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) || iReturn )
    {
     // Allocating memory 分配内存
     intptrStruct = Marshal.AllocCoTaskMem(bufferLength);
     if (intptrStruct != IntPtr.Zero)
     {
      iReturn = InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, ref bufferLength);
      if (iReturn)
      {
       struct_IPI = (Struct_INTERNET_PROXY_INFO)Marshal.PtrToStructure(intptrStruct,typeof(Struct_INTERNET_PROXY_INFO));
       //*****Proxy Server Config*****
       foreach(string proxy in Marshal.PtrToStringAnsi(struct_IPI.proxy).Split(' '))
       {
        Console.WriteLine(proxy);
       }
       //*****Proxy Server Bypass List *****
       foreach(string proxyBypass in Marshal.PtrToStringAnsi(struct_IPI.proxyBypass).Split(' '))
       {
        Console.WriteLine(proxyBypass);
       }
      }
     }
    }
    else
    {
     MessageBox.Show("ErrorCode="+ Marshal.GetLastWin32Error());
    }

   }
   catch(Exception exp)
   {

    MessageBox.Show(exp.ToString());
   }
   finally
   {
    if (intptrStruct != IntPtr.Zero)
     Marshal.FreeCoTaskMem(intptrStruct);
   }


  }
 }
}

你可能感兴趣的:(C#)