Setting proxy settings VB
Author |
Message |
Ric #1 / 5
|
 Setting proxy settings VB
I am trying to set the Proxy settings via VB 6. I can get InternetSetOption to work somewhat but the proxy settings only reflect the first character of the proxy server. Here is the code: Private Declare Function InternetSetOption Lib "wininet.dll" _ Alias "InternetSetOptionA" _ (ByVal hInternet As Long, ByVal lOption As Long, _ ByRef sBuffer As Any, ByVal lBufferLength As Long) As Long Const INTERNET_PER_CONN_FLAGS = 1 Const INTERNET_PER_CONN_PROXY_SERVER = 2 Const INTERNET_PER_CONN_PROXY_BYPASS = 3 Const INTERNET_OPTION_PER_CONNECTION_OPTION = 75 Const INTERNET_OPTION_SETTINGS_CHANGED = 39 Const PROXY_TYPE_PROXY = &H2 Const PROXY_TYPE_DIRECT = &H1 Private Type INTERNET_PER_CONN_OPTION Option As Long Value As Long Value2 As Long End Type Private Type INTERNET_PER_CONN_OPTION_LIST Size As Long Connection As Long OptionCount As Long OptionError As Long Options As Long End Type Public Function SetInternetProxyOptions(Enable As Boolean, Optional Proxy, Optional Port, Optional ProxyBypass) As Boolean Dim lngBuffer As Long Dim strProxyServer As String Dim strProxyBypass As String Dim optList As INTERNET_PER_CONN_OPTION_LIST Dim arrOptions() As INTERNET_PER_CONN_OPTION Dim addArray() Dim lngReturn As Long On Error GoTo SetError lngBuffer = Len(optList) 'Set flags. ReDim arrOptions(0) As INTERNET_PER_CONN_OPTION arrOptions(UBound(arrOptions)).Option = INTERNET_PER_CONN_FLAGS arrOptions(UBound(arrOptions)).Value = IIf(Enable, PROXY_TYPE_PROXY, PROXY_TYPE_DIRECT) arrOptions(UBound(arrOptions)).Value2 = 0 If Not IsMissing(Proxy) And Not IsMissing(Port) Then 'Set proxy name. ReDim Preserve arrOptions(UBound(arrOptions) + 1) Debug.Print UBound(arrOptions) strProxyServer = Proxy & ":" & Port arrOptions(UBound(arrOptions)).Option = INTERNET_PER_CONN_PROXY_SERVER arrOptions(UBound(arrOptions)).Value = StrPtr(strProxyServer) arrOptions(UBound(arrOptions)).Value2 = 0 End If If Not IsMissing(ProxyBypass) Then 'Set proxy bypass. ReDim Preserve arrOptions(UBound(arrOptions) + 1) strProxyBypass = ProxyBypass arrOptions(UBound(arrOptions)).Option = INTERNET_PER_CONN_PROXY_BYPASS arrOptions(UBound(arrOptions)).Value = StrPtr(strProxyBypass) arrOptions(UBound(arrOptions)).Value2 = 0 End If With optList .Size = Len(optList) 'Fill in options list struct. .Connection = 0 .OptionCount = UBound(arrOptions) + 1 .Options = VarPtr(arrOptions(0)) End With lngReturn = 1 If lngReturn = 1 Then lngReturn = InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, optList, lngBuffer) If lngReturn = 1 Then lngReturn = InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) SetInternetProxyOptions = CBool(lngReturn) SetError: If Err.Number Then Debug.Print "SetInternetProxyOptions -> " & Err.Description End Function If I send it setInternetProxySettings(True, "myproxy.com", "3100") the setting in control panel comes out as "Use a proxy server is checked and the proxy server name is m and the port is blank. Where am I wrong? Thanks in advance, rick
|
Wed, 13 Oct 2004 02:59:44 GMT |
|
 |
Leo Chen [M #2 / 5
|
 Setting proxy settings VB
Hi Rick, You may want to convert the Proxy name to Ansi string before getting it's address using StrPtr. Since we are using ANSI version of the API, sending wide char strings to the API will cause this problem. For example: strProxyServer = StrConv(strProxyServer, vbFromUnicode) arrOptions(UBound(arrOptions)).Value = StrPtr(strProxyServer) I hope this helps. Best Regards, Leo Chen This posting is provided "AS IS" with no warranties, and confers no rights. --------------------
| Newsgroups: microsoft.public.vb.controls.internet | Subject: Setting proxy settings VB | Date: 26 Apr 2002 11:59:44 -0700 | Organization: http://groups.google.com/ | Lines: 100
| NNTP-Posting-Host: 149.142.68.222 | Content-Type: text/plain; charset=ISO-8859-1 | Content-Transfer-Encoding: 8bit | X-Trace: posting.google.com 1019847584 15877 127.0.0.1 (26 Apr 2002 18:59:44 GMT)
| NNTP-Posting-Date: 26 Apr 2002 18:59:44 GMT | Path: cpmsftngxa08!cpmsftngxa06!tkmsftngxs01!tkmsftngp01!newsfeed00.sul.t-online.d e!t-online.de!skynet.be!skynet.be!proxad.net!proxad.net!wanadoo.fr!isdnet!sn -xit-02!supernews.com!postnews1.google.com!not-for-mail | Xref: cpmsftngxa08 microsoft.public.vb.controls.internet:34577 | X-Tomcat-NG: microsoft.public.vb.controls.internet | | I am trying to set the Proxy settings via VB 6. I can get | InternetSetOption to work somewhat but the proxy settings only reflect | the first character of the proxy server. Here is the code: | | Private Declare Function InternetSetOption Lib "wininet.dll" _ | Alias "InternetSetOptionA" _ | (ByVal hInternet As Long, ByVal lOption As Long, _ | ByRef sBuffer As Any, ByVal lBufferLength As Long) As Long | Const INTERNET_PER_CONN_FLAGS = 1 | Const INTERNET_PER_CONN_PROXY_SERVER = 2 | Const INTERNET_PER_CONN_PROXY_BYPASS = 3 | Const INTERNET_OPTION_PER_CONNECTION_OPTION = 75 | Const INTERNET_OPTION_SETTINGS_CHANGED = 39 | Const PROXY_TYPE_PROXY = &H2 | Const PROXY_TYPE_DIRECT = &H1 | Private Type INTERNET_PER_CONN_OPTION | Option As Long | Value As Long | Value2 As Long | End Type | | Private Type INTERNET_PER_CONN_OPTION_LIST | Size As Long | Connection As Long | OptionCount As Long | OptionError As Long | Options As Long | End Type | | Public Function SetInternetProxyOptions(Enable As Boolean, Optional | Proxy, Optional Port, Optional ProxyBypass) As Boolean | Dim lngBuffer As Long | Dim strProxyServer As String | Dim strProxyBypass As String | Dim optList As INTERNET_PER_CONN_OPTION_LIST | Dim arrOptions() As INTERNET_PER_CONN_OPTION | Dim addArray() | Dim lngReturn As Long | | On Error GoTo SetError | lngBuffer = Len(optList) | | 'Set flags. | ReDim arrOptions(0) As INTERNET_PER_CONN_OPTION | arrOptions(UBound(arrOptions)).Option = INTERNET_PER_CONN_FLAGS | arrOptions(UBound(arrOptions)).Value = IIf(Enable, | PROXY_TYPE_PROXY, PROXY_TYPE_DIRECT) | arrOptions(UBound(arrOptions)).Value2 = 0 | | If Not IsMissing(Proxy) And Not IsMissing(Port) Then | 'Set proxy name. | ReDim Preserve arrOptions(UBound(arrOptions) + 1) | Debug.Print UBound(arrOptions) | strProxyServer = Proxy & ":" & Port | arrOptions(UBound(arrOptions)).Option = | INTERNET_PER_CONN_PROXY_SERVER | arrOptions(UBound(arrOptions)).Value = StrPtr(strProxyServer) | arrOptions(UBound(arrOptions)).Value2 = 0 | End If | | If Not IsMissing(ProxyBypass) Then | 'Set proxy bypass. | ReDim Preserve arrOptions(UBound(arrOptions) + 1) | strProxyBypass = ProxyBypass | arrOptions(UBound(arrOptions)).Option = | INTERNET_PER_CONN_PROXY_BYPASS | arrOptions(UBound(arrOptions)).Value = StrPtr(strProxyBypass) | arrOptions(UBound(arrOptions)).Value2 = 0 | End If | | With optList | .Size = Len(optList) 'Fill in options list struct. | .Connection = 0 | .OptionCount = UBound(arrOptions) + 1 | .Options = VarPtr(arrOptions(0)) | End With | | lngReturn = 1 | If lngReturn = 1 Then lngReturn = InternetSetOption(0, | INTERNET_OPTION_PER_CONNECTION_OPTION, optList, lngBuffer) | If lngReturn = 1 Then lngReturn = InternetSetOption(0, | INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) | SetInternetProxyOptions = CBool(lngReturn) | | SetError: | If Err.Number Then Debug.Print "SetInternetProxyOptions -> " & | Err.Description | End Function | | | If I send it setInternetProxySettings(True, "myproxy.com", "3100") the | setting | in control panel comes out as "Use a proxy server is checked and the | proxy server name is m and the port is blank. | | Where am I wrong? | | Thanks in advance, | | rick |
|
Sat, 16 Oct 2004 16:58:22 GMT |
|
 |
mr706 #3 / 5
|
 Setting proxy settings VB
Hi there, I've trying to do the same job, that is, one of the following: 1. Get web brower control to use different proxy other thatn internet explorer 2. change the internet explorer proxy setting What i've been trying is to avoid using so much code to do the job, instead i was trying to modify the registry with the proxy i want to use, however, when i tested this method by changing the string values it did not change in the internet explore setting, the key address is: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion \Internet Settings plase let me know if it work and what is the trick to it thak you Quote: >-----Original Message----- >I am trying to set the Proxy settings via VB 6. I can get >InternetSetOption to work somewhat but the proxy settings only reflect >the first character of the proxy server. Here is the code: >Private Declare Function InternetSetOption Lib "wininet.dll" _ > Alias "InternetSetOptionA" _ > (ByVal hInternet As Long, ByVal lOption As Long, _ > ByRef sBuffer As Any, ByVal lBufferLength As Long) As Long > Const INTERNET_PER_CONN_FLAGS = 1 > Const INTERNET_PER_CONN_PROXY_SERVER = 2 > Const INTERNET_PER_CONN_PROXY_BYPASS = 3 > Const INTERNET_OPTION_PER_CONNECTION_OPTION = 75 > Const INTERNET_OPTION_SETTINGS_CHANGED = 39 > Const PROXY_TYPE_PROXY = &H2 > Const PROXY_TYPE_DIRECT = &H1 >Private Type INTERNET_PER_CONN_OPTION > Option As Long > Value As Long > Value2 As Long >End Type >Private Type INTERNET_PER_CONN_OPTION_LIST > Size As Long > Connection As Long > OptionCount As Long > OptionError As Long > Options As Long >End Type >Public Function SetInternetProxyOptions(Enable As Boolean, Optional >Proxy, Optional Port, Optional ProxyBypass) As Boolean > Dim lngBuffer As Long > Dim strProxyServer As String > Dim strProxyBypass As String > Dim optList As INTERNET_PER_CONN_OPTION_LIST > Dim arrOptions() As INTERNET_PER_CONN_OPTION > Dim addArray() > Dim lngReturn As Long > On Error GoTo SetError > lngBuffer = Len(optList) > 'Set flags. > ReDim arrOptions(0) As INTERNET_PER_CONN_OPTION > arrOptions(UBound(arrOptions)).Option =
INTERNET_PER_CONN_FLAGS Quote: > arrOptions(UBound(arrOptions)).Value = IIf(Enable, >PROXY_TYPE_PROXY, PROXY_TYPE_DIRECT) > arrOptions(UBound(arrOptions)).Value2 = 0 > If Not IsMissing(Proxy) And Not IsMissing(Port) Then > 'Set proxy name. > ReDim Preserve arrOptions(UBound(arrOptions) + 1) > Debug.Print UBound(arrOptions) > strProxyServer = Proxy & ":" & Port > arrOptions(UBound(arrOptions)).Option = >INTERNET_PER_CONN_PROXY_SERVER > arrOptions(UBound(arrOptions)).Value = StrPtr (strProxyServer) > arrOptions(UBound(arrOptions)).Value2 = 0 > End If > If Not IsMissing(ProxyBypass) Then > 'Set proxy bypass. > ReDim Preserve arrOptions(UBound(arrOptions) + 1) > strProxyBypass = ProxyBypass > arrOptions(UBound(arrOptions)).Option = >INTERNET_PER_CONN_PROXY_BYPASS > arrOptions(UBound(arrOptions)).Value = StrPtr (strProxyBypass) > arrOptions(UBound(arrOptions)).Value2 = 0 > End If > With optList > .Size = Len(optList) 'Fill in options list struct. > .Connection = 0 > .OptionCount = UBound(arrOptions) + 1 > .Options = VarPtr(arrOptions(0)) > End With > lngReturn = 1 > If lngReturn = 1 Then lngReturn = InternetSetOption(0, >INTERNET_OPTION_PER_CONNECTION_OPTION, optList, lngBuffer) > If lngReturn = 1 Then lngReturn = InternetSetOption(0, >INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) > SetInternetProxyOptions = CBool(lngReturn) >SetError: > If Err.Number Then
Debug.Print "SetInternetProxyOptions -> " & Quote: >Err.Description >End Function >If I send it setInternetProxySettings(Tru{ w -
P l IO% e, "myproxy.com", "3100") the Quote: >setting >in control panel comes out as "Use a proxy server is checked and the >proxy server name is m and the port is blank. >Where am I wrong? >Thanks in advance, >rick >.
|
Sat, 16 Oct 2004 23:34:18 GMT |
|
 |
Ric #4 / 5
|
 Setting proxy settings VB
Quote:
> Hi there, > I've trying to do the same job, that is, one of the > following: > 1. Get web brower control to use different proxy other > thatn internet explorer > 2. change the internet explorer proxy setting > What i've been trying is to avoid using so much code to do > the job, instead i was trying to modify the registry with > the proxy i want to use, however, when i tested this > method by changing the string values it did not change in > the internet explore setting, the key address is: > HKEY CURRENT USER\Software\Microsoft\Windows\CurrentVersion > \Internet Settings > plase let me know if it work and what is the trick to it > thak you > >-----Original Message----- > >I am trying to set the Proxy settings via VB 6. I can get > >InternetSetOption to work somewhat but the proxy settings > only reflect > >the first character of the proxy server. Here is the code: > >Private Declare Function InternetSetOption > Lib "wininet.dll" > > Alias "InternetSetOptionA" > > (ByVal hInternet As Long, ByVal lOption As Long, > > ByRef sBuffer As Any, ByVal lBufferLength As Long) As > Long > > Const INTERNET PER CONN FLAGS = 1 > > Const INTERNET PER CONN PROXY SERVER = 2 > > Const INTERNET PER CONN PROXY BYPASS = 3 > > Const INTERNET OPTION PER CONNECTION OPTION = 75 > > Const INTERNET OPTION SETTINGS CHANGED = 39 > > Const PROXY TYPE PROXY = &H2 > > Const PROXY TYPE DIRECT = &H1 > >Private Type INTERNET PER CONN OPTION > > Option As Long > > Value As Long > > Value2 As Long > >End Type > >Private Type INTERNET PER CONN OPTION LIST > > Size As Long > > Connection As Long > > OptionCount As Long > > OptionError As Long > > Options As Long > >End Type > >Public Function SetInternetProxyOptions(Enable As > Boolean, Optional > >Proxy, Optional Port, Optional ProxyBypass) As Boolean > > Dim lngBuffer As Long > > Dim strProxyServer As String > > Dim strProxyBypass As String > > Dim optList As INTERNET PER CONN OPTION LIST > > Dim arrOptions() As INTERNET PER CONN OPTION > > Dim addArray() > > Dim lngReturn As Long > > On Error GoTo SetError > > lngBuffer = Len(optList) > > 'Set flags. > > ReDim arrOptions(0) As INTERNET PER CONN OPTION > > arrOptions(UBound(arrOptions)).Option = > INTERNET PER CONN FLAGS > > arrOptions(UBound(arrOptions)).Value = IIf(Enable, > >PROXY TYPE PROXY, PROXY TYPE DIRECT) > > arrOptions(UBound(arrOptions)).Value2 = 0 > > If Not IsMissing(Proxy) And Not IsMissing(Port) Then > > 'Set proxy name. > > ReDim Preserve arrOptions(UBound(arrOptions) + 1) > > Debug.Print UBound(arrOptions) > > strProxyServer = Proxy & ":" & Port > > arrOptions(UBound(arrOptions)).Option = > >INTERNET PER CONN PROXY SERVER > > arrOptions(UBound(arrOptions)).Value = StrPtr > (strProxyServer) > > arrOptions(UBound(arrOptions)).Value2 = 0 > > End If > > If Not IsMissing(ProxyBypass) Then > > 'Set proxy bypass. > > ReDim Preserve arrOptions(UBound(arrOptions) + 1) > > strProxyBypass = ProxyBypass > > arrOptions(UBound(arrOptions)).Option = > >INTERNET PER CONN PROXY BYPASS > > arrOptions(UBound(arrOptions)).Value = StrPtr > (strProxyBypass) > > arrOptions(UBound(arrOptions)).Value2 = 0 > > End If > > With optList > > .Size = Len(optList) 'Fill in options list struct. > > .Connection = 0 > > .OptionCount = UBound(arrOptions) + 1 > > .Options = VarPtr(arrOptions(0)) > > End With > > lngReturn = 1 > > If lngReturn = 1 Then lngReturn = InternetSetOption(0, > >INTERNET OPTION PER CONNECTION OPTION, optList, lngBuffer) > > If lngReturn = 1 Then lngReturn = InternetSetOption(0, > >INTERNET OPTION SETTINGS CHANGED, 0, 0) > > SetInternetProxyOptions = CBool(lngReturn) > >SetError: > > If Err.Number Then > Debug.Print "SetInternetProxyOptions -> " & > >Err.Description > >End Function > >If I send it setInternetProxySettings(True, "myproxy.com", "3100") the > >setting > >in control panel comes out as "Use a proxy server is > checked and the > >proxy server name is m and the port is blank. > >Where am I wrong? > >Thanks in advance, > >rick > >.
Leo, That was what I was missing! Works Great Thanks, rick
|
Sun, 17 Oct 2004 06:33:56 GMT |
|
 |
mr706 #5 / 5
|
 Setting proxy settings VB
Hi, what i just found is the following: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion \Internet Settings\<proxysevers> will set the LAN proxy and HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion \Internet Settings\connections\<your dialup account> has a the setting for dial up internet options, but i did not know how to modify its value through vb since it is binary not just a string, please help Quote: >-----Original Message----- >Hi there, >I've trying to do the same job, that is, one of the >following: >1. Get web brower control to use different proxy other >thatn internet explorer >2. change the internet explorer proxy setting >What i've been trying is to avoid using so much code to do >the job, instead i was trying to modify the registry with >the proxy i want to use, however, when i tested this >method by changing the string values it did not change in >the internet explore setting, the key address is: >HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersio n >\Internet Settings >plase let me know if it work and what is the trick to it >thak you >>-----Original Message----- >>I am trying to set the Proxy settings via VB 6. I can get >>InternetSetOption to work somewhat but the proxy settings >only reflect >>the first character of the proxy server. Here is the code: >>Private Declare Function InternetSetOption >Lib "wininet.dll" _ >> Alias "InternetSetOptionA" _ >> (ByVal hInternet As Long, ByVal lOption As Long, _ >> ByRef sBuffer As Any, ByVal lBufferLength As Long) As >Long >> Const INTERNET_PER_CONN_FLAGS = 1 >> Const INTERNET_PER_CONN_PROXY_SERVER = 2 >> Const INTERNET_PER_CONN_PROXY_BYPASS = 3 >> Const INTERNET_OPTION_PER_CONNECTION_OPTION = 75 >> Const INTERNET_OPTION_SETTINGS_CHANGED = 39 >> Const PROXY_TYPE_PROXY = &H2 >> Const PROXY_TYPE_DIRECT = &H1 >>Private Type INTERNET_PER_CONN_OPTION >> Option As Long >> Value As Long >> Value2 As Long >>End Type >>Private Type INTERNET_PER_CONN_OPTION_LIST >> Size As Long >> Connection As Long >> OptionCount As Long >> OptionError As Long >> Options As Long >>End Type >>Public Function SetInternetProxyOptions(Enable As >Boolean, Optional >>Proxy, Optional Port, Optional ProxyBypass) As Boolean >> Dim lngBuffer As Long >> Dim strProxyServer As String >> Dim strProxyBypass As String >> Dim optList As INTERNET_PER_CONN_OPTION_LIST >> Dim arrOptions() As INTERNET_PER_CONN_OPTION >> Dim addArray() >> Dim lngReturn As Long >> On Error GoTo SetError >> lngBuffer = Len(optList) >> 'Set flags. >> ReDim arrOptions(0) As INTERNET_PER_CONN_OPTION >> arrOptions(UBound(arrOptions)).Option = >INTERNET_PER_CONN_FLAGS >> arrOptions(UBound(arrOptions)).Value = IIf(Enable, >>PROXY_TYPE_PROXY, PROXY_TYPE_DIRECT) >> arrOptions(UBound(arrOptions)).Value2 = 0 >> If Not IsMissing(Proxy) And Not IsMissing(Port) Then >> 'Set proxy name. >> ReDim Preserve arrOptions(UBound(arrOptions) + 1) >> Debug.Print UBound(arrOptions) >> strProxyServer = Proxy & ":" & Port >> arrOptions(UBound(arrOptions)).Option = >>INTERNET_PER_CONN_PROXY_SERVER >> arrOptions(UBound(arrOptions)).Value = StrPtr >(strProxyServer) >> arrOptions(UBound(arrOptions)).Value2 = 0 >> End If >> If Not IsMissing(ProxyBypass) Then >> 'Set proxy bypass. >> ReDim Preserve arrOptions(UBound(arrOptions) + 1) >> strProxyBypass = ProxyBypass >> arrOptions(UBound(arrOptions)).Option = >>INTERNET_PER_CONN_PROXY_BYPASS >> arrOptions(UBoun{ w D,!,$"|! d
(arrOptions)).Value = StrPtr Quote: >(strProxyBypass) >> arrOptions(UBound(arrOptions)).Value2 = 0 >> End If >> With optList >> .Size = Len(optList) 'Fill in options list struct. >> .Connection = 0 >> .OptionCount = UBound(arrOptions) +
|
Sun, 17 Oct 2004 12:09:08 GMT |
|
|
|