<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; InternetSetOptions</title>
	<atom:link href="http://huddledmasses.org/tag/internetsetoptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Setting Windows internet connection proxy from C#</title>
		<link>http://huddledmasses.org/setting-windows-internet-connection-proxy-from-c/</link>
		<comments>http://huddledmasses.org/setting-windows-internet-connection-proxy-from-c/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 04:54:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[InternetSetOptions]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Proxies]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=578</guid>
		<description><![CDATA[I have a problem with my laptop where I connect to my home network and my work network, and my school network &#8230; and I have to use different proxy settings for Windows internet connection settings (which are used by virtually all .Net apps, including PowerShell) by default&#8230; Of course, there are a lot of [...]]]></description>
			<content:encoded><![CDATA[	<p>I have a problem with my laptop where I connect to my home network and my work network, and my school network &#8230; and I have to use different proxy settings for Windows internet connection settings (which are used by virtually all .Net apps, including PowerShell) by default&#8230;</p>

	<p>Of course, there are a lot of different apps that can manage those settings (and more) automatically using Windows <acronym title="Network Location Awareness">NLA</acronym> to trigger.  However, I have an extra special requirement: when I plug in at work or at home, I want to run Synergy on the laptop as a client &#8230; but the name and ip of the host is different, of course &#8212; so I need to stop synergy and restart it with a different server name &#8230; something I can easily accomplish from a script or batch file &#8230; but changing proxies is a bit trickier.</p>

	<p>I thought it would a simple call to a .Net library method, but after much searching, the only way of <a href="http://msdn.microsoft.com/en-us/library/aa385384.aspx">setting or retrieving Internet options</a> seems to be through the old WinInet <span class="caps">API</span>, using <a href="http://msdn.microsoft.com/en-us/library/aa385114.aspx">InternetSetOptions</a> (or <a href="http://msdn.microsoft.com/en-us/library/aa385101.aspx">InternetQueryOption</a> to read them).  Of course, to use these, you have to map the <a href="http://msdn.microsoft.com/en-us/library/aa385146.aspx">INTERNET_PER_CONN_OPTION_LIST</a> and <a href="http://msdn.microsoft.com/en-us/library/aa385145.aspx">INTERNET_PER_CONN_OPTION</a> structures to C#, and do whole lot of marshalling and manual memory management.</p>

	<p>I <em>did</em> find some C# mappings for the <a href="http://www.pinvoke.net/default.aspx/wininet/InternetPerConnOptionList.htm">Option structure</a> on <a href="http://PInvoke.net">PInvoke.net</a> but not for the others.  I actually started writing them all by hand and then found a &#8220;recipe&#8221; in the <a href="http://oreilly.com/catalog/9780596516109/">(see the examples link) C# 3.0 Cookbook</a> for retrieving Internet settings which had versions of all the structures I needed, as well as definitions for the flags I wanted.  So&#8230; without further ado, let me share the code with you &#8230; <span id="more-578"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">#require -version 2.0</span><br />
<span style="color: #666666; font-style: italic;">## Set and unset the Windows Internet Settings Proxy</span><br />
<span style="color: #666666; font-style: italic;">## Works instantly on already running apps like IE ...</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Usage:</span><br />
<span style="color: #666666; font-style: italic;">## Set-Proxy web.proxy.xerox.com 8000</span><br />
<span style="color: #666666; font-style: italic;">## Remove-Proxy</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #0066cc; font-style: italic;">add-<span style="font-style: normal;">type</span></span> $<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">File</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">ReadAllText</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$PsScriptRoot\PoshHttp.Proxies.cs&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp;<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Proxy</span></span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span> <span style="color: #660033; font-weight: bold;">$proxy</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$port</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$port</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$proxy</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;:$port&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>PoshHttp.<span style="color: #003366;">Proxies</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SetProxy</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$proxy</span>, <span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Proxy</span></span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>PoshHttp.<span style="color: #003366;">Proxies</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SetProxy</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Null</span>,<span style="color: #660033; font-weight: bold;">$Null</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">ModuleMember</span></span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Proxy</span></span>, <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Proxy</span></span></div>

	<p>You&#8217;ll notice the first line of that calls add-type and passes it a C# file &#8230; Add-Type has the very impressive feature of being able to compile C# code straight into memory.  The C# code is here below &#8230; and if you wanted to use it on PowerShell version 1, all you&#8217;d have to do is compile it using csc (which is in C:\WINDOWS\Microsoft.NET\Framework\ ... in either the v3.5 directory or the v2.0.50727 directory, your choice): <code>csc /target:library .\PoshHttp.Proxies.cs</code> and you could then load the dll by using <code>[Reflection.Assembly]::LoadFrom(&#34;PoshHttp.Proxies.dll&#34;)</code>!  Amazing  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>I&#8217;ll actually be adding this to my <a href="http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/">PoshHttp</a> module and releasing a new build of that sometime this coming week, because I think it would be useful to have it pre-compiled &#8230; but I thought I would show off how cool it is to be able to use heavy-duty pinvoke stuff from a &#8220;script&#8221;  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> PoshHttp<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Proxies<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> UnsetProxy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> SetProxy<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> SetProxy<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> strProxy<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> SetProxy<span style="color: #000000;">&#40;</span>strProxy, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> SetProxy<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> strProxy, <span style="color: #FF0000;">string</span> exceptions<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InternetPerConnOptionList list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InternetPerConnOptionList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> optionCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>strProxy<span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">:</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>exceptions<span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InternetConnectionOption<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> options <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InternetConnectionOption<span style="color: #000000;">&#91;</span>optionCount<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// USE a proxy server ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Option</span> <span style="color: #008000;">=</span> PerConnOption.<span style="color: #0000FF;">INTERNET_PER_CONN_FLAGS</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Value</span>.<span style="color: #0000FF;">m_Int</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>optionCount <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> PerConnFlags.<span style="color: #0000FF;">PROXY_TYPE_DIRECT</span> <span style="color: #008000;">:</span> <span style="color: #000000;">&#40;</span>PerConnFlags.<span style="color: #0000FF;">PROXY_TYPE_DIRECT</span> <span style="color: #008000;">|</span> PerConnFlags.<span style="color: #0000FF;">PROXY_TYPE_PROXY</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// use THIS proxy server</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>optionCount <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Option</span> <span style="color: #008000;">=</span> PerConnOption.<span style="color: #0000FF;">INTERNET_PER_CONN_PROXY_SERVER</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Value</span>.<span style="color: #0000FF;">m_StringPtr</span> <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">StringToHGlobalAuto</span><span style="color: #000000;">&#40;</span>strProxy<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// except for these addresses ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>optionCount <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Option</span> <span style="color: #008000;">=</span> PerConnOption.<span style="color: #0000FF;">INTERNET_PER_CONN_PROXY_BYPASS</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">m_Value</span>.<span style="color: #0000FF;">m_StringPtr</span> <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">StringToHGlobalAuto</span><span style="color: #000000;">&#40;</span>exceptions<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// default stuff</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.<span style="color: #0000FF;">dwSize</span> <span style="color: #008000;">=</span> Marshal.<a href="http://www.google.com/search?q=sizeof+msdn.microsoft.com"><span style="color: #008000;">SizeOf</span></a><span style="color: #000000;">&#40;</span>list<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.<span style="color: #0000FF;">szConnection</span> <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.<span style="color: #0000FF;">dwOptionCount</span> <span style="color: #008000;">=</span> options.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.<span style="color: #0000FF;">dwOptionError</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> optSize <span style="color: #008000;">=</span> Marshal.<a href="http://www.google.com/search?q=sizeof+msdn.microsoft.com"><span style="color: #008000;">SizeOf</span></a><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>InternetConnectionOption<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// make a pointer out of all that ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr optionsPtr <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">AllocCoTaskMem</span><span style="color: #000000;">&#40;</span>optSize <span style="color: #008000;">*</span> options.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// copy the array over into that spot in memory ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> options.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntPtr opt <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> IntPtr<span style="color: #000000;">&#40;</span>optionsPtr.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>i <span style="color: #008000;">*</span> optSize<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Marshal.<span style="color: #0000FF;">StructureToPtr</span><span style="color: #000000;">&#40;</span>options<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, opt, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.<span style="color: #0000FF;">options</span> <span style="color: #008000;">=</span> optionsPtr<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// and then make a pointer out of the whole list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr ipcoListPtr <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">AllocCoTaskMem</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>Int32<span style="color: #000000;">&#41;</span>list.<span style="color: #0000FF;">dwSize</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Marshal.<span style="color: #0000FF;">StructureToPtr</span><span style="color: #000000;">&#40;</span>list, ipcoListPtr, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// and finally, call the API method!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> returnvalue <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">InternetSetOption</span><span style="color: #000000;">&#40;</span>IntPtr.<span style="color: #0000FF;">Zero</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InternetOption.<span style="color: #0000FF;">INTERNET_OPTION_PER_CONNECTION_OPTION</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ipcoListPtr, list.<span style="color: #0000FF;">dwSize</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>returnvalue <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span> &nbsp;<span style="color: #008080; font-style: italic;">// get the error codes, they might be helpful</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnvalue <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// FREE the data ASAP</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Marshal.<span style="color: #0000FF;">FreeCoTaskMem</span><span style="color: #000000;">&#40;</span>optionsPtr<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Marshal.<span style="color: #0000FF;">FreeCoTaskMem</span><span style="color: #000000;">&#40;</span>ipcoListPtr<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>returnvalue <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span> &nbsp;<span style="color: #008080; font-style: italic;">// throw the error codes, they might be helpful</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Win32Exception<span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>returnvalue <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080;">#region WinInet structures</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span>, CharSet <span style="color: #008000;">=</span> CharSet.<span style="color: #0600FF;">Auto</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> InternetPerConnOptionList<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> dwSize<span style="color: #008000;">;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// size of the INTERNET_PER_CONN_OPTION_LIST struct</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> IntPtr szConnection<span style="color: #008000;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// connection name to set/query options</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> dwOptionCount<span style="color: #008000;">;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// number of options to set/query</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> dwOptionError<span style="color: #008000;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// on error, which option failed</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//[MarshalAs(UnmanagedType.)]</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> IntPtr options<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span>, CharSet <span style="color: #008000;">=</span> CharSet.<span style="color: #0600FF;">Auto</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> InternetConnectionOption<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">int</span> Size<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> PerConnOption m_Option<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> InternetConnectionOptionValue m_Value<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">static</span> InternetConnectionOption<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InternetConnectionOption.<span style="color: #0000FF;">Size</span> <span style="color: #008000;">=</span> Marshal.<a href="http://www.google.com/search?q=sizeof+msdn.microsoft.com"><span style="color: #008000;">SizeOf</span></a><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>InternetConnectionOption<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Nested Types</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0600FF;">Explicit</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> InternetConnectionOptionValue<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Fields</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>FieldOffset<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Runtime</span>.<span style="color: #0000FF;">InteropServices</span></span>.<span style="color: #0000FF;">ComTypes</span>.<span style="color: #0000FF;">FILETIME</span> m_FileTime<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>FieldOffset<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> m_Int<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>FieldOffset<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> IntPtr m_StringPtr<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080;">#region WinInet enums</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// options manifests for Internet{Query|Set}Option</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> InternetOption <span style="color: #008000;">:</span> <span style="color: #FF0000;">uint</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; INTERNET_OPTION_PER_CONNECTION_OPTION <span style="color: #008000;">=</span> <span style="color: #FF0000;">75</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Options used in INTERNET_PER_CONN_OPTON struct</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> PerConnOption<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; INTERNET_PER_CONN_FLAGS <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, <span style="color: #008080; font-style: italic;">// Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags </span><br />
&nbsp; &nbsp; &nbsp; INTERNET_PER_CONN_PROXY_SERVER <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, <span style="color: #008080; font-style: italic;">// Sets or retrieves a string containing the proxy servers. &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; INTERNET_PER_CONN_PROXY_BYPASS <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>, <span style="color: #008080; font-style: italic;">// Sets or retrieves a string containing the URLs that do not use the proxy server. &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; INTERNET_PER_CONN_AUTOCONFIG_URL <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008080; font-style: italic;">//, // Sets or retrieves a string containing the URL to the automatic configuration script. &nbsp;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// PER_CONN_FLAGS</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#91;</span>Flags<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> PerConnFlags<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; PROXY_TYPE_DIRECT <span style="color: #008000;">=</span> 0x00000001, &nbsp;<span style="color: #008080; font-style: italic;">// direct to net</span><br />
&nbsp; &nbsp; &nbsp; PROXY_TYPE_PROXY <span style="color: #008000;">=</span> 0x00000002, &nbsp;<span style="color: #008080; font-style: italic;">// via named proxy</span><br />
&nbsp; &nbsp; &nbsp; PROXY_TYPE_AUTO_PROXY_URL <span style="color: #008000;">=</span> 0x00000004, &nbsp;<span style="color: #008080; font-style: italic;">// autoproxy URL</span><br />
&nbsp; &nbsp; &nbsp; PROXY_TYPE_AUTO_DETECT <span style="color: #008000;">=</span> 0x00000008 &nbsp; <span style="color: #008080; font-style: italic;">// use autoproxy detection</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> NativeMethods<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WinInet.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, CharSet <span style="color: #008000;">=</span> CharSet.<span style="color: #0600FF;">Auto</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> InternetSetOption<span style="color: #000000;">&#40;</span>IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, <span style="color: #FF0000;">int</span> dwBufferLength<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/setting-windows-internet-connection-proxy-from-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

