Programmatically Set Proxy Settings In Visual Basic

Proxies are very useful while developing application especially in those who need internet connection. Proxies are used according to applications requirement and organization needs. Always a developer wants to create a proxy connection for his or her application only and not for whole PC. There are many tutorials available on the web which might help you in doing this task but most of them deals with registry entries and hence proxy connection is applied to whole computer. While some tutorials deals with application only proxy, but they are very hard to get found on this huge internet.

Hence, I have written a module to change proxy settings. All those developers who are interested in changing application only proxy setting can use this module. This module is written in VB and can be imported in your project easily. Also I have developed a small example based on this module which can help you in better understanding of usage. You can scroll down to get download links.

The story..

Actually from last few days I am trying to make a web browser with built in proxy. Hence for this project I was looking for the VB script which will change the proxy setting for my browser only and not for whole system. Now let’s keep story aside and let start exploring the code I got from web while searching.

    ' 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

I found this code from web and which is seem to be working as needed. If you look carefully to the code then you can identify that this code uses inbuilt windows functions to change the proxy related settings. When I inserted this code in my application and tested it, I found that the proxy is applied to my application only and not to other browsers like Google Chrome and Internet Explorer 9 which was installed on my PC.

Use of this script is very easy. To enable the proxy you can use

RefreshIESettings('Enter your proxy here')

and proxy will be applied. For example


Note: This subroutine accepts string values only.

To disable proxy you can use following code


That’s all this is how you can deal with this code. But this is just a raw code. To make it more easier to understand, I made a module named as proxyconfig.vb. You can import this module in your project and can enable and disable the proxy by using 0 and 1.

Once you have imported this module you can use following three function to deal with proxy setting.
1. To disable proxy, just pass 0 to proxy_enable()


2. To enable default proxy, just pass 1 to proxy_enable()


You can change this default proxy in module file according to your requirements

3. To enable specific proxy, just pass proxy address as string to proxy_enable(). For example


Now you can handle the proxy settings very easily. Go ahead, download this module and integrate in your project.

Download Links:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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