Sebastian Jaurena
Posts: 56
Joined: 2008-08-04
|
I found a web page where the author said that is possible changes the proxy without having to restart IE (http://www.vbforums.com/showthread.php?t=449751) but the problem is the code, is in VB6 and I need translate it to VB2008. Someone can help me?
Seba |
|
David Thompson
Guest
|
Sebastian,
Most of the code will run as-is. The most significant problem would be with the Declare statement.
A good place to get help with that would be
http://www.pinvoke.net/
You will likely be able to find a direct replacement for the InternetSetOption function on that site.
Regards,
David |
|
Sebastian Jaurena
Posts: 56
Joined: 2008-08-04
|
I will replace it and then, tell you the result.
Thanks. |
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Sebastian.
I rewrote the code.
Private Structure INTERNET_PER_CONN_OPTION
Public dwOption As Integer
Public dwValue1 As Integer
Public dwValue2 As Integer
End Structure
Private Structure INTERNET_PER_CONN_OPTION_LIST
Public dwSize As Integer
Public pszConnection As Integer
Public dwOptionCount As Integer
Public dwOptionError As Integer
Public pOptions As Integer
End Structure
Private Const INTERNET_PER_CONN_FLAGS As Integer = 1
Private Const INTERNET_PER_CONN_PROXY_SERVER As Integer = 2
Private Const INTERNET_PER_CONN_PROXY_BYPASS As Integer = 3
Private Const PROXY_TYPE_DIRECT As Integer = &H1
Private Const PROXY_TYPE_PROXY As Integer = &H2
Private Const INTERNET_OPTION_REFRESH As Integer = 37
Private Const INTERNET_OPTION_SETTINGS_CHANGED As Integer = 39
Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Integer = 75
Private Declare Auto Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" ( _
ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal dwBufferLength As Integer) As Boolean
' 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 lpList As IntPtr
Dim bReturn As Boolean
Dim dwBufSize As Integer
Dim options(0 To 2) As INTERNET_PER_CONN_OPTION
Dim abConnName As String
Dim abProxyServer As String
Dim abProxyBypass As String
Dim dwOptSize As Integer
dwBufSize = Len(list)
' Fill out list struct.
list.dwSize = Len(list)
' NULL == LAN, otherwise connection name.
abConnName = StrConv(conn_name & vbNullChar, 0)
list.pszConnection = Marshal.StringToHGlobalAnsi(abConnName)
' 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, 0)
options(1).dwValue1 = Marshal.StringToHGlobalAnsi(abProxyServer)
'//"http://proxy:80"
' Set proxy override.
options(2).dwOption = INTERNET_PER_CONN_PROXY_BYPASS
abProxyBypass = StrConv("local" & vbNullChar, 0)
options(2).dwValue1 = Marshal.StringToHGlobalAnsi(abProxyBypass)
dwOptSize = Len(options(0))
list.pOptions = Marshal.AllocHGlobal(dwOptSize * 3)
Marshal.StructureToPtr(options(0), list.pOptions, False)
Marshal.StructureToPtr(options(1), list.pOptions + dwOptSize, False)
Marshal.StructureToPtr(options(2), list.pOptions + (dwOptSize * 2), False)
' Make sure the memory was allocated.
If (list.pOptions = IntPtr.Zero) 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.
lpList = Marshal.AllocHGlobal(Marshal.SizeOf(list))
Marshal.StructureToPtr(list, lpList, False)
bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, lpList, dwBufSize)
' Free the allocated memory.
Marshal.FreeHGlobal(list.pszConnection)
Marshal.FreeHGlobal(options(1).dwValue1)
Marshal.FreeHGlobal(options(2).dwValue1)
Erase options
dwBufSize = 0
list.dwOptionCount = 0
list.dwSize = 0
list.pOptions = 0
list.pszConnection = 0
Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)
Call InternetSetOption(0, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)
Return 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 lpList As IntPtr
Dim bReturn As Boolean
Dim dwBufSize As Integer
Dim options(0) As INTERNET_PER_CONN_OPTION
Dim abConnName As String
dwBufSize = Len(list)
' Fill out list struct.
list.dwSize = Len(list)
' NULL == LAN, otherwise connectoid name.
abConnName = StrConv(conn_name & vbNullChar, 0)
list.pszConnection = Marshal.StringToHGlobalAnsi(abConnName)
' Set three options.
list.dwOptionCount = 1
' Set flags.
options(0).dwOption = INTERNET_PER_CONN_FLAGS
options(0).dwValue1 = PROXY_TYPE_DIRECT
list.pOptions = Marshal.AllocHGlobal(Marshal.SizeOf(options(0)))
Marshal.StructureToPtr(options(0), list.pOptions, False)
' Make sure the memory was allocated.
If (list.pOptions = IntPtr.Zero) 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.
lpList = Marshal.AllocHGlobal(Marshal.SizeOf(list))
Marshal.StructureToPtr(list, lpList, False)
bReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, lpList, dwBufSize)
' Free the allocated memory.
Marshal.FreeHGlobal(list.pszConnection)
Erase options
dwBufSize = 0
list.dwOptionCount = 0
list.dwSize = 0
list.pOptions = 0
list.pszConnection = 0
Call InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)
Call InternetSetOption(0, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)
Return bReturn
End Function
Private Sub cmdSetProxy_Click()
Dim conn_name As String
Dim 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 |
|
Sebastian Jaurena
Posts: 56
Joined: 2008-08-04
|
Thanks! Seems to be working perfect! |
|