# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Change Proxy Without Having to Restart Internet Explorer

## Chris001

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:
'Module
Option Explicit
 Private Type INTERNET_PER_CONN_OPTION
    dwOption As Long
    dwValue1 As Long
    dwValue2 As Long
End Type
Private Type INTERNET_PER_CONN_OPTION_LIST
    dwSize As Long
    pszConnection As Long
    dwOptionCount As Long
    dwOptionError As Long
    pOptions As Long
End Type
Private Const INTERNET_PER_CONN_FLAGS As Long = 1
Private Const INTERNET_PER_CONN_PROXY_SERVER As Long = 2
Private Const INTERNET_PER_CONN_PROXY_BYPASS As Long = 3
Private Const PROXY_TYPE_DIRECT As Long = &H1
Private Const PROXY_TYPE_PROXY As Long = &H2
Private Const INTERNET_OPTION_REFRESH As Long = 37
Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39
Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Long = 75
Private Declare Function InternetSetOption _
        Lib "wininet.dll" Alias "InternetSetOptionA" ( _
        ByVal hInternet As Long, ByVal dwOption As Long, _
        lpBuffer As Any, ByVal dwBufferLength As Long) As Long
 ' Set Proxy
 Public Function SetConnectionOptions(ByVal conn_name As String, ByVal proxy_full_addr As String) As Boolean
' conn_name: active connection name. (LAN = "")
' proxy_full_addr : eg "193.28.73.241:8080"
Dim list As INTERNET_PER_CONN_OPTION_LIST
Dim bReturn As Boolean
Dim dwBufSize As Long
Dim options(0 To 2) As INTERNET_PER_CONN_OPTION
Dim abConnName() As Byte
Dim abProxyServer() As Byte
Dim abProxyBypass() As Byte
    
    dwBufSize = Len(list)
    
    ' Fill out list struct.
    list.dwSize = Len(list)
    
    ' NULL == LAN, otherwise connection name.
    abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    list.pszConnection = VarPtr(abConnName(0))
    
    ' Set three options.
    list.dwOptionCount = 3
     ' Set flags.
    options(0).dwOption = INTERNET_PER_CONN_FLAGS
    options(0).dwValue1 = PROXY_TYPE_DIRECT Or PROXY_TYPE_PROXY
     ' Set proxy name.
    options(1).dwOption = INTERNET_PER_CONN_PROXY_SERVER
    abProxyServer() = StrConv(proxy_full_addr & vbNullChar, vbFromUnicode)
    options(1).dwValue1 = VarPtr(abProxyServer(0))  '//"http://proxy:80"
     ' Set proxy override.
    options(2).dwOption = INTERNET_PER_CONN_PROXY_BYPASS
    abProxyBypass() = StrConv("local" & vbNullChar, vbFromUnicode)
    options(2).dwValue1 = VarPtr(abProxyBypass(0))
     list.pOptions = VarPtr(options(0))
    ' Make sure the memory was allocated.
    If (0& = list.pOptions) Then
        ' Return FALSE if the memory wasn't allocated.
        Debug.Print "Failed to allocate memory in SetConnectionOptions()"
        SetConnectionOptions = 0
    End If
     ' Set the options on the connection.
    bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
     ' Free the allocated memory.
    Erase options
    Erase abConnName
    Erase abProxyServer
    Erase abProxyBypass
    dwBufSize = 0
    list.dwOptionCount = 0
    list.dwSize = 0
    list.pOptions = 0
    list.pszConnection = 0
    Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    SetConnectionOptions = bReturn
End Function
  ' Disable Proxy
 Public Function DisableConnectionProxy(ByVal conn_name As String) As Boolean
' conn_name: active connection name. (LAN = "")
Dim list As INTERNET_PER_CONN_OPTION_LIST
Dim bReturn As Boolean
Dim dwBufSize As Long
Dim options(0) As INTERNET_PER_CONN_OPTION
Dim abConnName() As Byte
    
    dwBufSize = Len(list)
    
    ' Fill out list struct.
    list.dwSize = Len(list)
    
    ' NULL == LAN, otherwise connectoid name.
    abConnName() = StrConv(conn_name & vbNullChar, vbFromUnicode)
    list.pszConnection = VarPtr(abConnName(0))
    
    ' Set three options.
    list.dwOptionCount = 1
     ' Set flags.
    options(0).dwOption = INTERNET_PER_CONN_FLAGS
    options(0).dwValue1 = PROXY_TYPE_DIRECT
     list.pOptions = VarPtr(options(0))
    ' Make sure the memory was allocated.
    If (0 = list.pOptions) Then
        ' Return FALSE if the memory wasn't allocated.
        Debug.Print "Failed to allocate memory in DisableConnectionProxy()"
        DisableConnectionProxy = 0
    End If
     ' Set the options on the connection.
    bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
    
    ' Free the allocated memory.
    Erase options
    Erase abConnName
    dwBufSize = 0
    list.dwOptionCount = 0
    list.dwSize = 0
    list.pOptions = 0
    list.pszConnection = 0
    Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0)
    Call InternetSetOption(0, INTERNET_OPTION_REFRESH, ByVal 0&, 0)
    DisableConnectionProxy = bReturn
End Function


VB Code:
'Form
Option Explicit
 Private Sub cmdSetProxy_Click()
    Dim conn_name As String, proxy_full_addr As String
    conn_name = ""
    proxy_full_addr = "167.35.217.71:8080"
    Call SetConnectionOptions(conn_name, proxy_full_addr)
End Sub
 Private Sub cmdDisableProxy_Click()
    Dim conn_name As String
    conn_name = ""
    Call DisableConnectionProxy(conn_name)
End Sub

----------


## kara123

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? :Alien Frog:

----------


## babyjesus

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

----------


## Tierrize

Omg Ty Ty Ty! Perfect module

----------


## teguh123

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

How do do that automatically?

----------


## teguh123

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.

----------


## teguh123

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.

----------


## Jackkkkk

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?

----------


## c1ph3r

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..

----------


## spiderzsoft

very very  amezing project. thanks

----------


## Shohag_ifas

> 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

----------


## rafhelp

this one sets the proxy BUT how do you disable proxy



```
    ' 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
```

----------


## Weeluke

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

----------


## praesagus

> 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.

----------


## sylvaink

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




> 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:
> 'Module
> ...

----------


## scrapersNbots.com

> 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.

----------

