WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Dynamically Change Proxy Settings for an Existing IE Session

 Keywords:  

The following link suggests that it can all be done with 'InternetSetOption': http://www.codeproject.com/KB/IP/changeproxy1.aspx?display=Print

I took the time to convert the C code in the article . I then tested it by first starting in instance of IE and then running the script. The running instance of IE did attempt to use the new proxy settings after the script completed.

The following has been very lightly tested so your mileage may differ but it worked for me when changing the lan setting

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SetConnectionsOptions
;;
;; Purpose: Example UDF that uses the Win32 InternetSetOption function
;;          to dynamically change proxy settings for a running IE
;;          process.
;;
;; Parameters: strConnName       - active connection name.
;;             strProxyFullAdder - eg "210.78.22.87:8000"
;;
;; Return:  @True on success, otherwise @False
;;
;; Notes:  Lacks proper testing and in-function error handling.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#DefineFunction SetConnectionOptions(strConnName, strProxyFullAdder)

   ; Fill out list struct.
   dwBufSize = 20
   hList = BinaryAlloc(dwBufSize)
   BinaryPoke4(hList, 0, dwBufSize) ; Set buffer size (list.dwSize)

   ; Add the connection name to the structure.
   ; (null == lan)
   nStrLen  = StrLen(strConnName)
   If nStrLen
      hConnName = BinaryAlloc(StrLen(strConnName)+1)
      BinaryPokeStr(hConnName, 0, strConnName)
      BinaryPoke(hConnName, nStrLen,0)
      BinaryPoke4(hList, 4, IntControl(42, hConnName, 0, 0, 0))
   Else
      hConnName = 0
      BinaryPoke4(hList, 4, 0)
   EndIf

   ; Create an array of three options structures.
   BinaryPoke4(hList, 8, 3)

   ; Create the options array and add it to the options list structure.
   hOptions = BinaryAlloc(12 * 3)
   BinaryPoke4(hList, 16, IntControl(42, hOptions, 0, 0, 0))

   ; Set option flags.
   INTERNET_PER_CONN_FLAGS  = 1
   PROXY_TYPE_DIRECT          = 1
   PROXY_TYPE_PROXY         = 2
   BinaryPoke4(hOptions, 0, INTERNET_PER_CONN_FLAGS)
    BinaryPoke4(hOptions, 4, PROXY_TYPE_DIRECT|PROXY_TYPE_PROXY)

   ; Set option proxy name.
   INTERNET_PER_CONN_PROXY_SERVER = 2
   BinaryPoke4(hOptions, 12+0, INTERNET_PER_CONN_PROXY_SERVER)
   nStrLen     = StrLen(strProxyFullAdder)
     hProxyAddr = BinaryAlloc(nStrLen + 1)
   BinaryPoke(hProxyAddr , nStrLen,0)
   BinaryPokeStr(hProxyAddr, 0, strProxyFullAdder)
   BinaryPoke4(hOptions, 12 + 4, IntControl(42,hProxyAddr, 0, 0, 0))

   ; Set option proxy override.
   INTERNET_PER_CONN_PROXY_BYPASS = 3
   BinaryPoke4(hOptions, 24 + 0, INTERNET_PER_CONN_PROXY_BYPASS )
   nStrLen     = StrLen("local")
     hProxyOver = BinaryAlloc(nStrLen + 1)
   BinaryPoke(hProxyOver, nStrLen,0)
   BinaryPokeStr(hProxyOver, 0, "local")
   BinaryPoke4(hOptions, 24 + 4, IntControl(42,hProxyOver, 0, 0, 0))

   ; Set the options on the connection.
   INETOPT_PER_CONNECTION_OPTION =  75
   hWinInet = DllLoad(DirWindows(1):"WININET.DLL")
   bReturn  = DllCall(hWinInet, long:"InternetSetOptionA", long:0, long:INETOPT_PER_CONNECTION_OPTION, lpbinary:hList, long:dwBufSize)

   ; Free the allocated memory.
   BinaryFree(hProxyOver)
   BinaryFree(hProxyAddr)
   If hConnName Then BinaryFree(hConnName)
   BinaryFree(hOptions)
   BinaryFree(hList)

   ; Notify running processes.
   INETOPT_SETTINGS_CHANGED = 39
   INETOPT_REFRESH           = 37
   DllCall(hWinInet, long:"InternetSetOptionA", long:0, long:INETOPT_SETTINGS_CHANGED, long:0, long:0)
   DllCall(hWinInet, long:"InternetSetOptionA", long:0, long:INETOPT_REFRESH,          long:0, long:0)

   DllFree(hWinInet)
   Return bReturn;
#EndFunction

; Test call.
bResult = SetConnectionOptions("", "192.168.0.100:8080")

Article ID:   W17790
Filename:   Dynamically Change Proxy Settings for an Existing IE Session.txt
File Created: 2009:02:13:10:01:18
Last Updated: 2009:02:13:10:01:18