Results 1 to 16 of 16

Thread: Change Proxy Without Having to Restart Internet Explorer

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Change Proxy Without Having to Restart Internet Explorer

    The code below allows you to set and disable a proxy without having to restart Internet Explorer.

    It has been converted from the C++ code on this page: http://www.codeproject.com/internet/changeproxy1.asp

    I stumbled upon this code somewhere a long time ago while searching for other code with google, so I'm not the one who converted it. I did make a few small changes, because not all memory was released properly, which made it crash when changing or disabling a proxy for the second time.

    VB Code:
    1. 'Module
    2. Option Explicit
    3.  
    4. Private Type INTERNET_PER_CONN_OPTION
    5.     dwOption As Long
    6.     dwValue1 As Long
    7.     dwValue2 As Long
    8. End Type
    9. Private Type INTERNET_PER_CONN_OPTION_LIST
    10.     dwSize As Long
    11.     pszConnection As Long
    12.     dwOptionCount As Long
    13.     dwOptionError As Long
    14.     pOptions As Long
    15. End Type
    16. Private Const INTERNET_PER_CONN_FLAGS As Long = 1
    17. Private Const INTERNET_PER_CONN_PROXY_SERVER As Long = 2
    18. Private Const INTERNET_PER_CONN_PROXY_BYPASS As Long = 3
    19. Private Const PROXY_TYPE_DIRECT As Long = &H1
    20. Private Const PROXY_TYPE_PROXY As Long = &H2
    21. Private Const INTERNET_OPTION_REFRESH As Long = 37
    22. Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39
    23. Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Long = 75
    24. Private Declare Function InternetSetOption _
    25.         Lib "wininet.dll" Alias "InternetSetOptionA" ( _
    26.         ByVal hInternet As Long, ByVal dwOption As Long, _
    27.         lpBuffer As Any, ByVal dwBufferLength As Long) As Long
    28.  
    29. ' Set Proxy
    30.  
    31. Public Function SetConnectionOptions(ByVal conn_name As String, ByVal proxy_full_addr As String) As Boolean
    32. ' conn_name: active connection name. (LAN = "")
    33. ' proxy_full_addr : eg "193.28.73.241:8080"
    34. Dim list As INTERNET_PER_CONN_OPTION_LIST
    35. Dim bReturn As Boolean
    36. Dim dwBufSize As Long
    37. Dim options(0 To 2) As INTERNET_PER_CONN_OPTION
    38. Dim abConnName() As Byte
    39. Dim abProxyServer() As Byte
    40. Dim abProxyBypass() As Byte
    41.    
    42.     dwBufSize = Len(list)
    43.    
    44.     ' Fill out list struct.
    45.     list.dwSize = Len(list)
    46.    
    47.     ' NULL == LAN, otherwise connection name.
    48.     abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    49.     list.pszConnection = VarPtr(abConnName(0))
    50.    
    51.     ' Set three options.
    52.     list.dwOptionCount = 3
    53.  
    54.     ' Set flags.
    55.     options(0).dwOption = INTERNET_PER_CONN_FLAGS
    56.     options(0).dwValue1 = PROXY_TYPE_DIRECT Or PROXY_TYPE_PROXY
    57.  
    58.     ' Set proxy name.
    59.     options(1).dwOption = INTERNET_PER_CONN_PROXY_SERVER
    60.     abProxyServer() = StrConv(proxy_full_addr & vbNullChar, vbFromUnicode)
    61.     options(1).dwValue1 = VarPtr(abProxyServer(0))  '//"http://proxy:80"
    62.  
    63.     ' Set proxy override.
    64.     options(2).dwOption = INTERNET_PER_CONN_PROXY_BYPASS
    65.     abProxyBypass() = StrConv("local" & vbNullChar, vbFromUnicode)
    66.     options(2).dwValue1 = VarPtr(abProxyBypass(0))
    67.  
    68.     list.pOptions = VarPtr(options(0))
    69.     ' Make sure the memory was allocated.
    70.     If (0& = list.pOptions) Then
    71.         ' Return FALSE if the memory wasn't allocated.
    72.         Debug.Print "Failed to allocate memory in SetConnectionOptions()"
    73.         SetConnectionOptions = 0
    74.     End If
    75.  
    76.     ' Set the options on the connection.
    77.     bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    78.  
    79.     ' Free the allocated memory.
    80.     Erase options
    81.     Erase abConnName
    82.     Erase abProxyServer
    83.     Erase abProxyBypass
    84.     dwBufSize = 0
    85.     list.dwOptionCount = 0
    86.     list.dwSize = 0
    87.     list.pOptions = 0
    88.     list.pszConnection = 0
    89.     Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    90.     Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    91.     SetConnectionOptions = bReturn
    92. End Function
    93.  
    94.  
    95. ' Disable Proxy
    96.  
    97. Public Function DisableConnectionProxy(ByVal conn_name As String) As Boolean
    98. ' conn_name: active connection name. (LAN = "")
    99. Dim list As INTERNET_PER_CONN_OPTION_LIST
    100. Dim bReturn As Boolean
    101. Dim dwBufSize As Long
    102. Dim options(0) As INTERNET_PER_CONN_OPTION
    103. Dim abConnName() As Byte
    104.    
    105.     dwBufSize = Len(list)
    106.    
    107.     ' Fill out list struct.
    108.     list.dwSize = Len(list)
    109.    
    110.     ' NULL == LAN, otherwise connectoid name.
    111.     abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    112.     list.pszConnection = VarPtr(abConnName(0))
    113.    
    114.     ' Set three options.
    115.     list.dwOptionCount = 1
    116.  
    117.     ' Set flags.
    118.     options(0).dwOption = INTERNET_PER_CONN_FLAGS
    119.     options(0).dwValue1 = PROXY_TYPE_DIRECT
    120.  
    121.     list.pOptions = VarPtr(options(0))
    122.     ' Make sure the memory was allocated.
    123.     If (0 = list.pOptions) Then
    124.         ' Return FALSE if the memory wasn't allocated.
    125.         Debug.Print "Failed to allocate memory in DisableConnectionProxy()"
    126.         DisableConnectionProxy = 0
    127.     End If
    128.  
    129.     ' Set the options on the connection.
    130.     bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    131.    
    132.     ' Free the allocated memory.
    133.     Erase options
    134.     Erase abConnName
    135.     dwBufSize = 0
    136.     list.dwOptionCount = 0
    137.     list.dwSize = 0
    138.     list.pOptions = 0
    139.     list.pszConnection = 0
    140.     Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    141.     Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    142.     DisableConnectionProxy = bReturn
    143. End Function

    VB Code:
    1. 'Form
    2. Option Explicit
    3.  
    4. Private Sub cmdSetProxy_Click()
    5.     Dim conn_name As String, proxy_full_addr As String
    6.     conn_name = ""
    7.     proxy_full_addr = "167.35.217.71:8080"
    8.     Call SetConnectionOptions(conn_name, proxy_full_addr)
    9. End Sub
    10.  
    11. Private Sub cmdDisableProxy_Click()
    12.     Dim conn_name As String
    13.     conn_name = ""
    14.     Call DisableConnectionProxy(conn_name)
    15. End Sub

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    1

    Re: Change Proxy Without Having to Restart Internet Explorer

    this one is great and works flawless!

    BUT

    My question is:
    what do i have to change if i want to use socks5 - instead of -http- proxy?
    Please any help?

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    57

    Re: Change Proxy Without Having to Restart Internet Explorer

    howb would i use this code and switch proxies using the same browser? i know its possible in .net but no idea in vb6

  4. #4
    New Member
    Join Date
    Aug 2009
    Posts
    7

    Re: Change Proxy Without Having to Restart Internet Explorer

    Omg Ty Ty Ty! Perfect module

  5. #5
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Change Proxy Without Having to Restart Internet Explorer

    Perfect module with one catch. What about if the proxy require authentication?

    How do do that automatically?

  6. #6
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Change Proxy Without Having to Restart Internet Explorer

    I think I know a possible solution

    If the proxy require authentication we need to supply our own Proxy-Authenticate: Basic blabla header

    It works.

    I can navigate to http://www.google.com for example.

    However, if I try to navigate to http://google.com, it will do a 302 redirect to http://www.google.com

    Then ie will try to retrieve http://www.google.com, this time WITHOUT sending the Proxy-Authenticate: Basic header.

    How to tell ie to remember sending Proxy-Authenticate: Basic every time it surfs the internet?

    I tried to manipulate headers on before_navigate2 event but it doesn't work. Also ie seems to hang if I manipulate headers there.

  7. #7
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Change Proxy Without Having to Restart Internet Explorer

    Never mind. There is a way to insert proxy authentication using the similar method.

    My next project is
    1. Translate this to vb.net (how do we deal with as any)
    2. Add proxy authentication feature to that.

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    31

    Re: Change Proxy Without Having to Restart Internet Explorer

    When i go to navigate with a proxy it just hangs it doesn't even show the progress wheel, i checked the url it has res://ieframe.dll/dnserrordiagoff_webOC.htm# infront of it, anyone know what's the problem?
    Last edited by Jackkkkk; Sep 24th, 2010 at 05:26 PM.

  9. #9
    New Member
    Join Date
    Sep 2010
    Posts
    1

    Re: Change Proxy Without Having to Restart Internet Explorer

    I dont mean to bump an old thread but im having issues with using this.I added the module and 2 buttons:1 to start proxy & 1 to kill it but my IP stays the same..

  10. #10
    New Member
    Join Date
    Mar 2011
    Posts
    5

    Re: Change Proxy Without Having to Restart Internet Explorer

    very very amezing project. thanks

  11. #11
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Question Re: Change Proxy Without Having to Restart Internet Explorer

    Quote Originally Posted by Chris001 View Post
    The code below allows you to set and disable a proxy without having to restart Internet Explorer.

    It has been converted from the C++ code on this page: http://www.codeproject.com/internet/changeproxy1.asp

    I stumbled upon...........................

    hi.. brother. but what about private proxy (proxy that ask for authentication); i mean the proxy with username and password.. ?

    how to handle that with your code?

    please leave a reply asap

    best regards

  12. #12
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Change Proxy Without Having to Restart Internet Explorer

    this one sets the proxy BUT how do you disable proxy

    Code:
        ' The function we will be using to set the proxy settings.
        Public Shared Sub RefreshIESettings(ByVal strProxy As String)
            Const INTERNET_OPTION_PROXY As Integer = 38
            Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
            Dim struct_IPI As Struct_INTERNET_PROXY_INFO
    
            ' Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
            struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy)
            struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local")
    
            ' Allocating memory
            Dim intptrStruct As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
    
            ' Converting structure to IntPtr
            System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
            Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
        End Sub

  13. #13
    Lively Member
    Join Date
    Feb 2012
    Posts
    72

    Re: Change Proxy Without Having to Restart Internet Explorer

    sorry for bumping an old thread but i like to search these things before asking...

    anyways!, does anyone know how i would incorperate this into the web browser control in vb6?

    and just as a side note when i set the command buttons up it didnt modify my ip in any way, does this still work? do i need to add a winsck control?

    thanks in advance
    weeluke

  14. #14
    New Member
    Join Date
    Nov 2012
    Posts
    1

    Re: Change Proxy Without Having to Restart Internet Explorer

    Quote Originally Posted by Weeluke View Post
    old thread... the web browser control in vb6... do i need to add a winsck control?
    weeluke
    The fact that this post is still around is a godsend as I am updating some legacy stuff! Chris001 you saved my life 5 years after the post. lol.
    This will work naively with the web browser control because these changes are made in the registry and affect all browsing; you don't need winsock. Just use the code above to set / unset the proxy before/after navigation. One thing to note is that the default proxy settings are to "Automatically Detect Settings." This can override your manual settings sometimes so be sure to un-check that.

  15. #15
    New Member
    Join Date
    Oct 2012
    Posts
    2

    Smile Re: Change Proxy Without Having to Restart Internet Explorer

    After 11 Years this post has still added value, many thanks to share this post with us.

    Quote Originally Posted by Chris001 View Post
    The code below allows you to set and disable a proxy without having to restart Internet Explorer.

    It has been converted from the C++ code on this page: http://www.codeproject.com/internet/changeproxy1.asp

    I stumbled upon this code somewhere a long time ago while searching for other code with google, so I'm not the one who converted it. I did make a few small changes, because not all memory was released properly, which made it crash when changing or disabling a proxy for the second time.

    VB Code:
    1. 'Module
    2. Option Explicit
    3.  
    4. Private Type INTERNET_PER_CONN_OPTION
    5.     dwOption As Long
    6.     dwValue1 As Long
    7.     dwValue2 As Long
    8. End Type
    9. Private Type INTERNET_PER_CONN_OPTION_LIST
    10.     dwSize As Long
    11.     pszConnection As Long
    12.     dwOptionCount As Long
    13.     dwOptionError As Long
    14.     pOptions As Long
    15. End Type
    16. Private Const INTERNET_PER_CONN_FLAGS As Long = 1
    17. Private Const INTERNET_PER_CONN_PROXY_SERVER As Long = 2
    18. Private Const INTERNET_PER_CONN_PROXY_BYPASS As Long = 3
    19. Private Const PROXY_TYPE_DIRECT As Long = &H1
    20. Private Const PROXY_TYPE_PROXY As Long = &H2
    21. Private Const INTERNET_OPTION_REFRESH As Long = 37
    22. Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39
    23. Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Long = 75
    24. Private Declare Function InternetSetOption _
    25.         Lib "wininet.dll" Alias "InternetSetOptionA" ( _
    26.         ByVal hInternet As Long, ByVal dwOption As Long, _
    27.         lpBuffer As Any, ByVal dwBufferLength As Long) As Long
    28.  
    29. ' Set Proxy
    30.  
    31. Public Function SetConnectionOptions(ByVal conn_name As String, ByVal proxy_full_addr As String) As Boolean
    32. ' conn_name: active connection name. (LAN = "")
    33. ' proxy_full_addr : eg "193.28.73.241:8080"
    34. Dim list As INTERNET_PER_CONN_OPTION_LIST
    35. Dim bReturn As Boolean
    36. Dim dwBufSize As Long
    37. Dim options(0 To 2) As INTERNET_PER_CONN_OPTION
    38. Dim abConnName() As Byte
    39. Dim abProxyServer() As Byte
    40. Dim abProxyBypass() As Byte
    41.    
    42.     dwBufSize = Len(list)
    43.    
    44.     ' Fill out list struct.
    45.     list.dwSize = Len(list)
    46.    
    47.     ' NULL == LAN, otherwise connection name.
    48.     abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    49.     list.pszConnection = VarPtr(abConnName(0))
    50.    
    51.     ' Set three options.
    52.     list.dwOptionCount = 3
    53.  
    54.     ' Set flags.
    55.     options(0).dwOption = INTERNET_PER_CONN_FLAGS
    56.     options(0).dwValue1 = PROXY_TYPE_DIRECT Or PROXY_TYPE_PROXY
    57.  
    58.     ' Set proxy name.
    59.     options(1).dwOption = INTERNET_PER_CONN_PROXY_SERVER
    60.     abProxyServer() = StrConv(proxy_full_addr & vbNullChar, vbFromUnicode)
    61.     options(1).dwValue1 = VarPtr(abProxyServer(0))  '//"http://proxy:80"
    62.  
    63.     ' Set proxy override.
    64.     options(2).dwOption = INTERNET_PER_CONN_PROXY_BYPASS
    65.     abProxyBypass() = StrConv("local" & vbNullChar, vbFromUnicode)
    66.     options(2).dwValue1 = VarPtr(abProxyBypass(0))
    67.  
    68.     list.pOptions = VarPtr(options(0))
    69.     ' Make sure the memory was allocated.
    70.     If (0& = list.pOptions) Then
    71.         ' Return FALSE if the memory wasn't allocated.
    72.         Debug.Print "Failed to allocate memory in SetConnectionOptions()"
    73.         SetConnectionOptions = 0
    74.     End If
    75.  
    76.     ' Set the options on the connection.
    77.     bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    78.  
    79.     ' Free the allocated memory.
    80.     Erase options
    81.     Erase abConnName
    82.     Erase abProxyServer
    83.     Erase abProxyBypass
    84.     dwBufSize = 0
    85.     list.dwOptionCount = 0
    86.     list.dwSize = 0
    87.     list.pOptions = 0
    88.     list.pszConnection = 0
    89.     Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    90.     Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    91.     SetConnectionOptions = bReturn
    92. End Function
    93.  
    94.  
    95. ' Disable Proxy
    96.  
    97. Public Function DisableConnectionProxy(ByVal conn_name As String) As Boolean
    98. ' conn_name: active connection name. (LAN = "")
    99. Dim list As INTERNET_PER_CONN_OPTION_LIST
    100. Dim bReturn As Boolean
    101. Dim dwBufSize As Long
    102. Dim options(0) As INTERNET_PER_CONN_OPTION
    103. Dim abConnName() As Byte
    104.    
    105.     dwBufSize = Len(list)
    106.    
    107.     ' Fill out list struct.
    108.     list.dwSize = Len(list)
    109.    
    110.     ' NULL == LAN, otherwise connectoid name.
    111.     abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    112.     list.pszConnection = VarPtr(abConnName(0))
    113.    
    114.     ' Set three options.
    115.     list.dwOptionCount = 1
    116.  
    117.     ' Set flags.
    118.     options(0).dwOption = INTERNET_PER_CONN_FLAGS
    119.     options(0).dwValue1 = PROXY_TYPE_DIRECT
    120.  
    121.     list.pOptions = VarPtr(options(0))
    122.     ' Make sure the memory was allocated.
    123.     If (0 = list.pOptions) Then
    124.         ' Return FALSE if the memory wasn't allocated.
    125.         Debug.Print "Failed to allocate memory in DisableConnectionProxy()"
    126.         DisableConnectionProxy = 0
    127.     End If
    128.  
    129.     ' Set the options on the connection.
    130.     bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    131.    
    132.     ' Free the allocated memory.
    133.     Erase options
    134.     Erase abConnName
    135.     dwBufSize = 0
    136.     list.dwOptionCount = 0
    137.     list.dwSize = 0
    138.     list.pOptions = 0
    139.     list.pszConnection = 0
    140.     Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    141.     Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    142.     DisableConnectionProxy = bReturn
    143. End Function

    VB Code:
    1. 'Form
    2. Option Explicit
    3.  
    4. Private Sub cmdSetProxy_Click()
    5.     Dim conn_name As String, proxy_full_addr As String
    6.     conn_name = ""
    7.     proxy_full_addr = "167.35.217.71:8080"
    8.     Call SetConnectionOptions(conn_name, proxy_full_addr)
    9. End Sub
    10.  
    11. Private Sub cmdDisableProxy_Click()
    12.     Dim conn_name As String
    13.     conn_name = ""
    14.     Call DisableConnectionProxy(conn_name)
    15. End Sub

  16. #16
    Junior Member scrapersNbots.com's Avatar
    Join Date
    Dec 2016
    Location
    Torrington, CT
    Posts
    25

    Re: Change Proxy Without Having to Restart Internet Explorer

    Quote Originally Posted by Jackkkkk View Post
    When i go to navigate with a proxy it just hangs it doesn't even show the progress wheel, i checked the url it has res://ieframe.dll/dnserrordiagoff_webOC.htm# infront of it, anyone know what's the problem?
    Thats because the proxy is not working or is slow. The web browser control sucks on top of that. A better option than the web browser control is to launch IE, set the parent to be your form, and its just like the web browser control except it is using the latest version of IE on your computer.
    〘SCRAPER〙software that extracts, organizes and displays data from the web.
    〘BOT〙software that automates and speeds up tasks on the web, mimicking human behavior.

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