Programmatically Set Proxy Settings In Visual Basic

Note: This guide was written in 2012 for VB .NET and targets the WinINet API (wininet.dll). It still functions on modern Windows, but with important caveats:

  • Scope is application-only — This only affects your own running application’s HTTP requests. It does not change settings for IE, Chrome, Edge, or any other browser running simultaneously.
  • Not a registry edit — Unlike registry-based approaches, this leaves the rest of the system completely untouched, which is exactly what you want for most use cases.
  • For modern .NET apps, consider using HttpClient with an explicit WebProxy instead, which is cleaner and cross-platform.
  • Best use case today: Legacy WinForms apps or anything using the WebBrowser control that relies on WinINet as its HTTP layer.

Working with proxies is a common requirement when you’re developing applications that need to talk to the internet, especially in corporate environments. Often, you want your application to use a specific proxy without affecting the rest of the user’s computer.

If you search the web for how to do this in Visual Basic, you’ll find plenty of tutorials that tell you to modify the Windows Registry. The problem with that approach is that it changes the settings for the entire PC, which usually isn’t what you want. Finding a clean way to set an “application-only” proxy can be surprisingly difficult.

Because I needed this for my own projects, I’ve put together a small VB module that handles this task cleanly. You can import this into your project to change your application’s proxy settings programmatically without touching the registry. I’ve also included a small example project to show you exactly how to use it. You can find the download links at the bottom of this post.

How it works

I was recently building a custom web browser and needed it to use a specific proxy. After a lot of digging, I found a clean way to tap into the Windows wininet.dll API. Here is the core logic:

    ' The structure we use for the information
    ' to be interpreted correctly by API.
    Private Structure Struct_INTERNET_PROXY_INFO
        Public dwAccessType As Integer
        Public proxy As IntPtr
        Public proxyBypass As IntPtr
    End Structure

    ' The Windows API function that allows us to manipulate
    ' IE settings programmatically.
    Private Declare Auto Function InternetSetOption Lib "wininet.dll" _
    (ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, _
     ByVal lpdwBufferLength As Integer) As Boolean

    ' The function we will be using to set the proxy settings.
    Friend 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

This code uses native Windows functions to manipulate the proxy settings for the current process. When I tested this, I found that it successfully applied the proxy to my application without affecting other browsers like Google Chrome or IE that were running at the same time.

Using the script is very straightforward. To enable a proxy, just call the subroutine with your proxy address:

RefreshIESettings("41.75.201.146:3128")

To disable the proxy and return to a direct connection, you can pass a colon:

RefreshIESettings(":")

To make things even simpler, I’ve wrapped this logic into a module called proxyconfig.vb. You can drop this into your project and use the proxy_enable() function.

  1. To disable the proxy: proxy_enable(0)
  2. To enable your default proxy: proxy_enable(1)
  3. To enable a specific proxy address: proxy_enable("your.proxy.address:port")

This makes handling proxy settings incredibly easy. Go ahead and grab the files below to get started.

Download Links:

2 thoughts on “Programmatically Set Proxy Settings In Visual Basic”

  1. but i need to configure it for the whole system?
    how will i do that using visual basic..you know something like
    HOTSPOT shield..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.