Results 1 to 11 of 11

Thread: [RESOLVED] IE Proxy/IP Change

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Resolved [RESOLVED] IE Proxy/IP Change

    Alright I know that this question is asked all too often on vbForums but I am running into this problem and none of the posts have been clearly answered, a majority of them just redirect to some deprecated VB6 code that cannot be transferred easily to VB.Net. I was wondering if anyone would be able to help me find the code to refresh my Local Area Network settings on a windows seven computer.

    I have code that will set all of the registry values that I need. (I have read on other posts that this is not the best way but then they just link to old non-.Net code that will not fit into my project.) I was wondering what exactly the 'Ok' button when you exit the LAN Settings window does, because this part is what actually refreshes the Ip of the computer to use proxy settings set by the registry values, I was hoping to have my program only be one button, that would do all of this without it opening the LAN Dialogue (requiring further human interaction.)

    I would post my current code but the code works perfectly as I have already said (well in editing the registry) and as such I don't find a need to post the source, but if someone asks for it I would give it to them, I just don't like showing an unfinished product that needs some key feature fixed, I mean that is part of the whole 'debugging' method but due to the fact that I don't plan on editing the current code other than tacking on the refresh-code I never saw the need. (run on sentence for the win)

    Hoping for help soon
    ~HunterWould

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: IE Proxy/IP Change

    Don't edit the registry directly to change the proxy. That's (imho) a very bad thing.

    Pass the full proxy including port as a parameter: 111.111.111.111:2222

    I'm not taking credit for this code, I only added the code to disable the proxy. All credits go to the original author.

    vb.net Code:
    1. Imports System
    2. Imports System.Runtime.InteropServices
    3.  
    4. Public Class IEProxy
    5.     Public Enum Options
    6.         INTERNET_PER_CONN_FLAGS = 1
    7.         INTERNET_PER_CONN_PROXY_SERVER = 2
    8.         INTERNET_PER_CONN_PROXY_BYPASS = 3
    9.         INTERNET_PER_CONN_AUTOCONFIG_URL = 4
    10.         INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5
    11.         INTERNET_OPTION_REFRESH = 37
    12.         INTERNET_OPTION_PER_CONNECTION_OPTION = 75
    13.         INTERNET_OPTION_SETTINGS_CHANGED = 39
    14.         PROXY_TYPE_PROXY = &H2
    15.         PROXY_TYPE_DIRECT = &H1
    16.     End Enum
    17.  
    18.     <StructLayout(LayoutKind.Sequential)> _
    19.     Private Class FILETIME
    20.         Public dwLowDateTime As Integer
    21.         Public dwHighDateTime As Integer
    22.     End Class
    23.  
    24.     <StructLayout(LayoutKind.Explicit, Size:=12)> _
    25.     Private Structure INTERNET_PER_CONN_OPTION
    26.         <FieldOffset(0)> Dim dwOption As Integer
    27.         <FieldOffset(4)> Dim dwValue As Integer
    28.         <FieldOffset(4)> Dim pszValue As IntPtr
    29.         <FieldOffset(4)> Dim ftValue As IntPtr
    30.  
    31.         Public Function GetBytes() As Byte()
    32.             Dim b(12) As Byte
    33.             BitConverter.GetBytes(dwOption).CopyTo(b, 0)
    34.             Select Case dwOption
    35.                 Case Options.INTERNET_PER_CONN_FLAGS
    36.                     BitConverter.GetBytes(dwValue).CopyTo(b, 4)
    37.                 Case Options.INTERNET_PER_CONN_PROXY_BYPASS
    38.                     BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
    39.                 Case Options.INTERNET_PER_CONN_PROXY_SERVER
    40.                     BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
    41.             End Select
    42.             Return b
    43.         End Function
    44.     End Structure
    45.  
    46.     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    47.     Private Class INTERNET_PER_CONN_OPTION_LIST
    48.         Public dwSize As Integer
    49.         Public pszConnection As String
    50.         Public dwOptionCount As Integer
    51.         Public dwOptionError As Integer
    52.         Public pOptions As IntPtr
    53.     End Class
    54.  
    55.     <StructLayout(LayoutKind.Sequential)> _
    56.     Private Class INTERNET_PROXY_INFO
    57.         Public dwAccessType As Integer
    58.         Public lpszProxy As IntPtr
    59.         Public lpszProxyBypass As IntPtr
    60.     End Class
    61.  
    62.     Private Const ERROR_INSUFFICIENT_BUFFER = 122
    63.     Private Const INTERNET_OPTION_PROXY = 38
    64.     Private Const INTERNET_OPEN_TYPE_DIRECT = 1
    65.  
    66.     <DllImport("wininet.dll")> _
    67.     Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, _
    68.              ByVal dwOption As Integer, _
    69.              ByVal lpBuffer As INTERNET_PER_CONN_OPTION_LIST, _
    70.              ByVal dwBufferLength As Integer) As Boolean
    71.     End Function
    72.  
    73.     <DllImport("kernel32.dll")> _
    74.     Private Shared Function GetLastError() As Integer
    75.     End Function
    76.  
    77.     Public Function SetProxy(ByVal proxy_full_addr As String) As Boolean
    78.         Dim bReturn As Boolean
    79.         Dim list As New INTERNET_PER_CONN_OPTION_LIST
    80.         Dim dwBufSize As Integer = Marshal.SizeOf(list)
    81.         Dim opts(3) As INTERNET_PER_CONN_OPTION
    82.         Dim opt_size As Integer = Marshal.SizeOf(opts(0))
    83.  
    84.         list.dwSize = dwBufSize
    85.         list.pszConnection = ControlChars.NullChar
    86.         list.dwOptionCount = 3
    87.  
    88.         'set flags
    89.         opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS
    90.         opts(0).dwValue = Options.PROXY_TYPE_DIRECT Or Options.PROXY_TYPE_PROXY
    91.  
    92.         'set proxyname
    93.         opts(1).dwOption = Options.INTERNET_PER_CONN_PROXY_SERVER
    94.         opts(1).pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr)
    95.  
    96.         'set override
    97.         opts(2).dwOption = Options.INTERNET_PER_CONN_PROXY_BYPASS
    98.         opts(2).pszValue = Marshal.StringToHGlobalAnsi("local")
    99.  
    100.         Dim b(3 * opt_size) As Byte
    101.         opts(0).GetBytes().CopyTo(b, 0)
    102.         opts(1).GetBytes().CopyTo(b, opt_size)
    103.         opts(2).GetBytes().CopyTo(b, 2 * opt_size)
    104.  
    105.         Dim ptr As IntPtr = Marshal.AllocCoTaskMem(3 * opt_size)
    106.         Marshal.Copy(b, 0, ptr, 3 * opt_size)
    107.  
    108.         list.pOptions = ptr
    109.         'Set the options on the connection
    110.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    111.         If Not bReturn Then
    112.             Debug.WriteLine(GetLastError)
    113.         End If
    114.  
    115.         'Notify existing Internet Explorer instances that the settings have changed
    116.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)
    117.         If Not bReturn Then
    118.             Debug.WriteLine(GetLastError)
    119.         End If
    120.  
    121.         'Flush the current IE proxy setting
    122.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)
    123.         If Not bReturn Then
    124.             Debug.WriteLine(GetLastError)
    125.         End If
    126.  
    127.         Marshal.FreeHGlobal(opts(1).pszValue)
    128.         Marshal.FreeHGlobal(opts(2).pszValue)
    129.         Marshal.FreeCoTaskMem(ptr)
    130.         Return bReturn
    131.     End Function
    132.  
    133.     Public Function DisableProxy() As Boolean
    134.         Dim bReturn As Boolean
    135.         Dim list As New INTERNET_PER_CONN_OPTION_LIST
    136.         Dim dwBufSize As Integer = Marshal.SizeOf(list)
    137.         Dim opts(0) As INTERNET_PER_CONN_OPTION
    138.         Dim opt_size As Integer = Marshal.SizeOf(opts(0))
    139.  
    140.         list.dwSize = dwBufSize
    141.         list.pszConnection = ControlChars.NullChar
    142.         list.dwOptionCount = 1
    143.  
    144.         opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS
    145.         opts(0).dwValue = Options.PROXY_TYPE_DIRECT
    146.  
    147.         Dim b(opt_size) As Byte
    148.         opts(0).GetBytes().CopyTo(b, 0)
    149.  
    150.         Dim ptr As IntPtr = Marshal.AllocCoTaskMem(opt_size)
    151.         Marshal.Copy(b, 0, ptr, opt_size)
    152.  
    153.         list.pOptions = ptr
    154.         'Set the options on the connection
    155.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    156.         If Not bReturn Then
    157.             Debug.WriteLine(GetLastError)
    158.         End If
    159.  
    160.         'Notify existing Internet Explorer instances that the settings have changed
    161.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)
    162.         If Not bReturn Then
    163.             Debug.WriteLine(GetLastError)
    164.         End If
    165.  
    166.         'Flush the current IE proxy setting
    167.         bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)
    168.         If Not bReturn Then
    169.             Debug.WriteLine(GetLastError)
    170.         End If
    171.  
    172.         Marshal.FreeCoTaskMem(ptr)
    173.         Return bReturn
    174.     End Function
    175. End Class
    Last edited by Chris001; Nov 18th, 2010 at 06:50 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: IE Proxy/IP Change

    There is one problem with that though, I have no idea on how to implement that code. I am new to this language still. Could you help me?

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: IE Proxy/IP Change

    Go to Project -> Add Class and add a Class.

    Change the name of the Class to IEProxy.vb and remove the small piece of the code that is already in there. Then copy and paste the code from above in there.

    Add two buttons to your form and paste the code below. Obviously 123.123.123.123:8080 needs to be a valid proxy.

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim clsProxy As New IEProxy
    3.         If clsProxy.SetProxy("123.123.123.123:8080") Then
    4.             MessageBox.Show("Proxy successfully enabled.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    5.         Else
    6.             MessageBox.Show("Error enabling proxy.", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    7.         End If
    8.     End Sub
    9.  
    10.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    11.         Dim clsProxy As New IEProxy
    12.         If clsProxy.DisableProxy Then
    13.             MessageBox.Show("Proxy successfully disabled.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    14.         Else
    15.             MessageBox.Show("Error disabling proxy.", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    16.         End If
    17.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: IE Proxy/IP Change

    You my friend, deserve a cookie.

  6. #6
    Addicted Member
    Join Date
    Aug 2009
    Posts
    227

    Re: [RESOLVED] IE Proxy/IP Change

    Chris, would it be possible to set an IP specific to a webbrowser? For example, use IP #1 with 1 webbrowser, and IP #2 with another webbrowser at the same time?

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: [RESOLVED] IE Proxy/IP Change

    get outta my thread G-man, nah I'm just kidding but yes you can totally do that I have seen programs that can do it, heck I have one right now that can. (not made by me of course)

  8. #8
    Addicted Member
    Join Date
    Aug 2009
    Posts
    227

    Re: [RESOLVED] IE Proxy/IP Change

    Well yeah, I know it's possible. The question was meant as a question of how to do it, not meant to ask if it can be done. Chris knows what I mean.

  9. #9
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] IE Proxy/IP Change

    Sorry, I can't help you with that. I have no idea how to set a proxy for just one instance of Internet Explorer.

  10. #10
    Addicted Member
    Join Date
    Aug 2009
    Posts
    227

    Re: [RESOLVED] IE Proxy/IP Change

    Alright, well thanks anyways Chris. Good job on finding this one.

  11. #11
    New Member
    Join Date
    Jan 2011
    Posts
    1

    Re: [RESOLVED] IE Proxy/IP Change

    Sorry for bumping an old(ish) thread.
    I have been using this code example for a while and it works great.
    However i would like to use it with a private proxy that requires authentication (username & pass)

    Any idea how use this code with a private proxy, so the username and pass are set and stored when the proxy is set?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width