'
' Created by SharpDevelop.
' User: Ying-Shen
' Date: 2004-11-12
' Time: 11:16
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Imports System
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports Microsoft.VisualBasic
Imports NUnit.Framework


Namespace DefaultNamespaceNamespace DefaultNamespace.Tests
<TestFixture> _

Public Class Test1Class Test1

Public Enum MyOptionsEnum MyOptions
INTERNET_PER_CONN_FLAGS = 1
INTERNET_PER_CONN_PROXY_SERVER = 2
INTERNET_PER_CONN_PROXY_BYPASS = 3
INTERNET_PER_CONN_AUTOCONFIG_URL = 4
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
INTERNET_OPTION_SETTINGS_CHANGED = 39
PROXY_TYPE_PROXY = &H2
PROXY_TYPE_DIRECT = &H1
End Enum

<StructLayout(LayoutKind.Sequential)> _

Private Class FILETIMEClass FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Class
<StructLayout(LayoutKind.Explicit, Size:=12)> _

Private Structure INTERNET_PER_CONN_OPTIONStructure INTERNET_PER_CONN_OPTION
<FieldOffset(0)> Dim dwOption As Integer
<FieldOffset(4)> Dim dwValue As Integer
<FieldOffset(4)> Dim pszValue As IntPtr
<FieldOffset(4)> Dim ftValue As IntPtr

Public Function GetBytes()Function GetBytes() As Byte()
Dim b(12) As Byte
BitConverter.GetBytes(dwOption).CopyTo(b, 0)
Select Case dwOption
Case MyOptions.INTERNET_PER_CONN_FLAGS
BitConverter.GetBytes(dwValue).CopyTo(b, 4)
Case MyOptions.INTERNET_PER_CONN_PROXY_BYPASS
BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
Case MyOptions.INTERNET_PER_CONN_PROXY_SERVER
BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
End Select
Return b
End Function
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _

Private Class INTERNET_PER_CONN_OPTION_LISTClass INTERNET_PER_CONN_OPTION_LIST
Public dwSize As Integer
Public pszConnection As String
Public dwOptionCount As Integer
Public dwOptionError As Integer
Public pOptions As IntPtr
End Class
<StructLayout(LayoutKind.Sequential)> _

Private Class INTERNET_PROXY_INFOClass INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public lpszProxy As IntPtr
Public lpszProxyBypass As IntPtr
End Class
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const INTERNET_OPTION_PROXY = 38
Private Const INTERNET_OPEN_TYPE_DIRECT = 1

<DllImport("wininet.dll")> _

Private Shared Function InternetSetOption()Function InternetSetOption(ByVal hInternet As IntPtr, _
ByVal dwOption As Integer, _
ByVal lpBuffer As INTERNET_PER_CONN_OPTION_LIST, _
ByVal dwBufferLength As Integer) As Boolean
End Function
<DllImport("kernel32.dll")> _

Private Shared Function GetLastError()Function GetLastError() As Integer
End Function

<Test> _

Public Sub TestMethod()Sub TestMethod
' TODO: Add your test.
SetProxy
End Sub

Private Function SetProxy()Function SetProxy() As Boolean
Dim bReturn As Boolean
Dim list As New INTERNET_PER_CONN_OPTION_LIST
Dim dwBufSize As Integer = Marshal.SizeOf(list)
Dim opts(3) As INTERNET_PER_CONN_OPTION
Dim opt_size As Integer = Marshal.SizeOf(opts(0))
Debug.WriteLine("sizeof(option) " + opt_size.ToString)

list.dwSize = dwBufSize
list.pszConnection = ControlChars.NullChar
list.dwOptionCount = 3

Debug.WriteLine("sizeof list " + dwBufSize.ToString())

'set flags
opts(0).dwOption = MyOptions.INTERNET_PER_CONN_FLAGS
opts(0).dwValue = MyOptions.PROXY_TYPE_DIRECT Or MyOptions.PROXY_TYPE_PROXY

'set proxyname
opts(1).dwOption = MyOptions.INTERNET_PER_CONN_PROXY_SERVER
opts(1).pszValue = Marshal.StringToHGlobalAnsi("http://proxy:80")
'set override
opts(2).dwOption = MyOptions.INTERNET_PER_CONN_PROXY_BYPASS
opts(2).pszValue = Marshal.StringToHGlobalAnsi("local")

Dim b(3*opt_size) As Byte
opts(0).GetBytes().CopyTo(b, 0)
opts(1).GetBytes().CopyTo(b, opt_size)
opts(2).GetBytes().CopyTo(b, 2 * opt_size)
Dim ptr As IntPtr = Marshal.AllocCoTaskMem(3*opt_size)
Marshal.Copy(b, 0, ptr, 3*opt_size)

list.pOptions = ptr
'set the options on the connection
bReturn = InternetSetOption(IntPtr.Zero, MyOptions.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
'Flush the current IE proxy setting
bReturn = InternetSetOption( IntPtr.Zero, MyOptions.INTERNET_OPTION_REFRESH,Nothing, 0 )
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If

Marshal.FreeHGlobal(opts(1).pszValue)
Marshal.FreeHGlobal(opts(2).pszValue)
Marshal.FreeCoTaskMem(ptr)
Return bReturn
End Function
End Class
End Namespace