asdawrqtgf
asdawrqtgf

Reputation: 640

Visual Basic InternetSetOption

I have got problem with reloading proxy settings in IE. I want to reload proxy options without restarting IE and Chrome. I've notice that InternetSetOption returns false.

Definitions:

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

Private Const INTERNET_OPTION_REFRESH As Long = 37
Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39

And inside function:

InternetSetOption(vbNull, INTERNET_OPTION_SETTINGS_CHANGED, vbNull, 0)
InternetSetOption(vbNull, INTERNET_OPTION_REFRESH, vbNull, 0)

Here is whole function:

Public Sub SetProxy() 'ByVal ServerName As String, ByVal port As Integer
    Dim regkey1 As RegistryKey
    regkey1 = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.Default)
    regkey1.SetValue("ProxyServer", "ftp=10.8.0.1:808;http=10.8.0.1:808;https=10.8.0.1:808;socks=10.8.0.1:1080", RegistryValueKind.Unknown)
    regkey1.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
    regkey1.Close()

    Dim regKey7 As RegistryKey
    regKey7 = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", RegistryKeyPermissionCheck.Default)


    Dim regKe As Object = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", True).GetValue("DefaultConnectionSettings")
    If regKe Is Nothing Then
    Else
        regKey7.DeleteValue("DefaultConnectionSettings")
    End If

    Dim regk As Object = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", True).GetValue("SavedLegacySettings")
    If regk Is Nothing Then
    Else
        regKey7.DeleteValue("SavedLegacySettings")
    End If

    regKey7.Close()

    InternetSetOption(vbNull, INTERNET_OPTION_SETTINGS_CHANGED, vbNull, 0)
    InternetSetOption(vbNull, INTERNET_OPTION_REFRESH, vbNull, 0)

    Label1.Text = "Connected to Disa's Proxy Server"
    Label1.ForeColor = Color.Green



End Sub

Upvotes: 1

Views: 3533

Answers (1)

Pete
Pete

Reputation: 6723

You need to change the vbnulls to IntPtr.Zero. This should correct the problem.

Imports System
Imports System.Runtime.InteropServices

Namespace UpdateProxy
    Friend Class Program
        Public Shared Declare Auto Function InternetSetOption Lib "wininet.dll" (hInternet As IntPtr, dwOption As Integer, lpBuffer As IntPtr, dwBufferLength As Integer) As Boolean

        Private Shared Sub Main(args As String())
            Program.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0)
            Program.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0)
        End Sub
    End Class
End Namespace

This simple program will update running instances of IE with the proxy settings from the registry.

Upvotes: 1

Related Questions