<?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; Win32</title>
	<atom:link href="http://huddledmasses.org/tag/win32/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>Fun with PInvoke and Aero Peek</title>
		<link>http://huddledmasses.org/fun-with-pinvoke-and-aero-peek/</link>
		<comments>http://huddledmasses.org/fun-with-pinvoke-and-aero-peek/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 05:07:16 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Aero Peek]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[DwmSetWindowAttribute]]></category>
		<category><![CDATA[DWMWA_EXCLUDED_FROM_PEEK]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1247</guid>
		<description><![CDATA[There are so many fun things you can do in Windows when your scripting language allows you to make PInvoke calls to Win32 APIs &#8230; but I have to say it&#8217;s amazing how many things have been added to Windows recently and still left out of the .Net framework &#8230; Anyway, on to the Aero [...]]]></description>
			<content:encoded><![CDATA[	<p>There are so many fun things you can do in Windows when your scripting language allows you to make PInvoke calls to Win32 APIs &#8230; but I have to say it&#8217;s amazing how many things have been added to Windows recently and still left out of the .Net framework &#8230;</p>

	<p>Anyway, on to the <a href="http://windows.microsoft.com/en-us/windows7/products/features/aero-peek">Aero Peek</a> stuff.  If you haven&#8217;t seen it, Aero Peek is a feature of Windows 7, which lets you get a peek at your desktop, or at a single window for a moment.  Basically, you can press Win+Space (the Windows logo key and the space bar) and all of your open windows instantly turn transparent, revealing &#8230; whatever was on your desktop: wallpaper, icons, and gadgets.   You can also use it by hovering your mouse on the right corner of the taskbar, or you can peek at a single window by hovering over it&#8217;s taskbar button and then over it&#8217;s thumbnail.</p>

	<p>In any case, I have a couple of windows which I would like to have stay visible on the desktop when I hit the aero peek hotkey: Rainlendar and Miranda.  It turns out there&#8217;s a simple <span class="caps">API</span> call for this: <code>DwmSetWindowAttribute</code> which lets you set the <code>DWMWA_EXCLUDED_FROM_PEEK</code> attribute to <code>ENABLED</code> &#8230; causing a window to no longer hide when you press that hotkey. Of course, that <span class="caps">API</span> call should be made by those apps, in response to a user setting (so I&#8217;ve told their authors about it), but it doesn&#8217;t <em>have</em> to be (so I wrote a script to do it myself).</p>

	<p>In the old days, I would have written a little systray app which would give you a popup list of all windows, or perhaps added a menu item to a window&#8217;s right-click menu &#8230; and I would have had to deal with creating some way to persist which apps you wanted to apply this to, and then I could have applied the setting to them whenever you opened them. </p>

	<p>But now, I have PowerShell. I don&#8217;t need to give you menus and store settings, because I can just let you edit a little script instead.</p>

	<p>So here&#8217;s a script which will let you turn off Aero Peek transparency for windows by window title and/or process name &#8230; Once you have this function available, you can keep Rainlendar&#8217;s calendar, tasks, and event windows all visible by just running <code>Remove-AeroPeek -Process Rainlendar2</code> or you can keep your <a class="zem_slink" href="http://www.miranda-im.org/" title="Miranda IM" rel="homepage">Miranda</a> contact list visible by running <code>Remove-AeroPeek &#34;Miranda IM&#34;</code> (although you should not that depends on the window title matching just that one window &#8212; and Miranda lets you change what your title is, so you may have to adjust it).</p>

<script type="text/javascript" src="http://PoshCode.org/embed/1288"></script>

	<p>Of course, that script really deserves explanation, because it&#8217;s showing off quite a few advanced things&#8230;</p>

	<p>The first thing is that I&#8217;m using a Try/Catch block in the <span class="caps">BEGIN</span> block to make sure I only execute that code once. You can&#8217;t call Add-Type with the same code multiple times in a single PowerShell session, because the type will already exist when you call it the second time.  So the code in the <code>try</code> block will throw an exception if the type doesn&#8217;t already exist, and in the catch handler, we&#8217;ll create the type, and define the other function we need.</p>

	<p>Add-Type is a super-powerful cmdlet which compiles code on the fly (or imports types from pre-compiled assemblies). In this case we&#8217;re using it to import a little class called Dwm which I started writing myself from PInvoke.net and the <span class="caps">MSDN</span> documentation, but then eventually copied most of from a NeoWin forum thread&#8230;  All this class really does is define the <span class="caps">API</span> function and the flags we need to pass to it, and then provides a wrapper for the DwmSetWindowAttribute call. We could have written that call in PowerShell, but at the end of the day, once you start compiling C# code in PowerShell, it&#8217;s hard to know when to stop  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>The Select-Window function is (yet another customized version of) a function I wrote awhile back on PoshCode as part of my (still in progress) rewrite of <a href="http://WASP.CodePlex.com">WASP</a> to use the <a href="http://msdn.microsoft.com/en-us/library/aa348551.aspx#Testing_with_UI_Automation">UIAutomationClient</a> &#8230; I&#8217;ve just modified it to add only the three properties of the window that I&#8217;m interested in: Title and ProcessId (for identifying the correct windows) and Handle (for passing to the <code>DwmSetWindowAttribute</code> call).  It uses the <code>RootElement</code> property of <a href="http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.aspx">System.Windows.Automation.AutomationElement</a> to do a search, and then a series of GetCurrentPropertyValue calls to determine the Name, ProcessId, and NativeWindowHandle of the windows it finds.</p>

	<p>That&#8217;s pretty much all there is to it, other than filtering out the window(s) that we want and actually calling the <span class="caps">API</span>. I think I&#8217;m going to have to play a little bit more with this to see what else we can do &#8212; I&#8217;ve already realized that this means we can make little widgets with PowerBoots and set them to stick around just like regular desktop gadgets &#8230;</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/9d449ee5-040d-4fc6-8cf0-9791b23f0898/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=9d449ee5-040d-4fc6-8cf0-9791b23f0898" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/fun-with-pinvoke-and-aero-peek/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WPF Window &#8220;Native&#8221; Behavior: Snap-To Screen Edges</title>
		<link>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/</link>
		<comments>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 05:06:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[NativeBehaviors]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[WM_WINDOWPOSCHANGING]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=718</guid>
		<description><![CDATA[There are many desirable behaviors for Windows applications that are just much harder to do than they should be with the tools that Microsoft has provided in the .Net Framework. In WPF, many of these behaviors are even harder to create than in Windows Forms because the necessary hooks take a bit more work to [...]]]></description>
			<content:encoded><![CDATA[	<p>There are many desirable behaviors for Windows applications that are just much harder to do than they should be with the tools that Microsoft has provided in the .Net Framework. In <a href="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation" title="Windows Presentation Foundation" rel="wikipedia" class="zem_slink">WPF</a>, many of these behaviors are even harder to create than in <a href="http://en.wikipedia.org/wiki/Windows_Forms" title="Windows Forms" rel="wikipedia" class="zem_slink">Windows Forms</a> because the necessary hooks take a bit more work to write (thanks to the fact that there&#8217;s no window handles, so dropping down to &#8220;Native&#8221; code is harder.  Luckily, <span class="caps">WPF</span> Attached Properties give us a whole new way to reuse these behaviors once they&#8217;re written.</p>

	<p>I&#8217;ve started working on a few classes I&#8217;m calling NativeBehaviors, because they rely on intercepting the native <em>Window Message</em> processing, and I&#8217;ve put together a simple framework to let me write them, which I thought I would share.  The first one I wrote is a SnapToBehavior, which implements a feature that people seem to be constantly re-implementing in apps. Basically, it makes a window snap to the screen edge when it gets close (and prevents them from being moved off-screen).  I&#8217;ve also written a Global HotkeysBehavior which lets you register Hotkeys that work whenever your app is running (even if another app is active) so you can create a Hotkey to hide your app and show it, or whatever.</p>

	<p>In the rest of this article I&#8217;ll show you how to achieve this in <span class="caps">WPF</span> using my base NativeBehavior class, and how to use it on a Window. Since some of you won&#8217;t really care how it works, let&#8217;s start with:</p>

	<h3>How to make your <span class="caps">WPF</span> Windows snap to screen edges in 3 easy steps:</h3>

	<p>Step 1. Add a reference to the Huddled.Wpf.Interop library.<br />
Step 2. Add my Xml namespace to your root Window element<br />
Step 3. Paste three lines from below&#8230;.</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys.DemoWindow&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;DemoWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:huddled</span>=<span style="color: #ff0000;">&quot;http://schemas.huddledmasses.org/wpf&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- You just add a reference to the library, add the &quot;huddled&quot; namespace, and paste: --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:SnapToBehavior</span> <span style="color: #000066;">SnapDistance</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- The rest of your window can be whatever you like. --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Drag this window near the screen edges&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>I should point out the &#8220;SnapDistance&#8221; property of the SnapToBehavior is actually a <span class="caps">WPF</span> &#8220;Thickness&#8221; which lets you specify the distance from the screen edge as a single number to use on all sides, or as a separate number for each side: Left, Top, Right, Bottom. And that&#8217;s pretty much all there is to know.</p>

	<h3>How to implement a NativeBehavior.</h3>

	<p>The implementation of the SnapToBehavior is actually ridiculously simple, given the NativeBehavior framework.  I simply created a SnapToBehavior class which derives from NativeBehavior, and implemented the single mandatory method:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// Gets the MessageMappings for this behavior:</span><br />
<span style="color: #008080; font-style: italic;">/// A single mapping of a handler for WM_WINDOWPOSCHANGING.</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;An enumerable collection of MessageMapping objects.&lt;/returns&gt;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IEnumerable<span style="color: #008000;">&lt;</span>MessageMapping<span style="color: #008000;">&gt;</span> GetHandlers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;yield <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MessageMapping<span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WindowMessage</span>.<span style="color: #0000FF;">WindowPositionChanging</span>, <span style="color: #008080; font-style: italic;">// the message</span><br />
&nbsp; &nbsp; &nbsp; OnPreviewPositionChange<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// the delegate which will handle that message</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>When my new behavior is added to the Behaviors collection, my handler will be registered, and whenever the WM_WINDOWPOSCHANGING message arrives, it will be called.  Now I defined a <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx">DependencyProperty</a> for the SnapDistance, so that you could set that in <span class="caps">XAML</span>.  It&#8217;s extremely simple, and VisualStudio has a built-in snippet for dependency properties, but here&#8217;s the code:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> DependencyProperty SnapDistanceProperty <span style="color: #008000;">=</span><br />
&nbsp; &nbsp;DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666;">&quot;SnapDistance&quot;</span>, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// The name of the property (must match)</span><br />
&nbsp; &nbsp; &nbsp; <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>Thickness<span style="color: #000000;">&#41;</span>, &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The type of the values</span><br />
&nbsp; &nbsp; &nbsp; <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>SnapToBehavior<span style="color: #000000;">&#41;</span>, &nbsp; <span style="color: #008080; font-style: italic;">// The type this property shows up on</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UIPropertyMetadata<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thickness<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// The default value</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">public</span> Thickness SnapDistance<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>Thickness<span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>SnapDistanceProperty<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>SnapDistanceProperty, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>Once I have that, the last piece of the puzzle is to actually handle the window position changing message (think of it as an event, if you&#8217;re not used to Win32 message-based programming).</p>

	<p>The basics of handling WM_WINDOWPOSCHANGING is that you get a structure passed in which has the <strong>proposed</strong> position of the Window, (including it&#8217;s z-index related to other apps) and you can basically tweak that however you like.  Obviously there are lots of possibilities for this single message: always-on-bottom windows, undraggable windows, etc., but in our case we&#8217;re just concerned about how close we are to the edge.</p>

	<p>We use the SystemParameters class to get the VirtualScreenLeft, VirtualScreenTop, and VirtualScreenWidth and VirtualScreenHeight which define the rectangle we&#8217;ll use for snapping.  In the case of non-rectangular arrangements of multiple monitors this isn&#8217;t quite sufficient, but for our example anything more would be a distraction.  Then we just check to see if the proposed position is within the &#8220;SnapDistance&#8221; of any of the edges, and if so, we change the position to be against the edge.  That&#8217;s really all there is to it.</p>

	<p>If you look at the code example below you&#8217;ll see I have to &#8220;Marshal&#8221; the WindowPosition structure in and out of an IntPtr which is passed in the WindowMessage &#8230; that&#8217;s the downside of intercepting window messages that the framework doesn&#8217;t already translate for us, but in this particular case, it&#8217;s actually fairly trivial.</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;lParam for WindowPositionChanging</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> WindowPosition<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> IntPtr Handle<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> IntPtr HandleInsertAfter<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Left<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Top<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Width<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Height<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> WindowPositionFlags Flags<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Right <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Left <span style="color: #008000;">+</span> Width<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Bottom <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Top <span style="color: #008000;">+</span> Height<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span> &nbsp; <br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr OnPreviewPositionChange<span style="color: #000000;">&#40;</span>IntPtr wParam, IntPtr lParam, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">bool</span> handled<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: #FF0000;">bool</span> updated <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var windowPosition <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">WindowPosition</span><span style="color: #000000;">&#41;</span>Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>lParam, <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>NativeMethods.<span style="color: #0000FF;">WindowPosition</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Flags</span> <span style="color: #008000;">&amp;</span> NativeMethods.<span style="color: #0000FF;">WindowPositionFlags</span>.<span style="color: #0000FF;">NoMove</span><span style="color: #000000;">&#41;</span> <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If we use the WPF SystemParameters, these should be &quot;Logical&quot; pixels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rect validArea <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Rect<span style="color: #000000;">&#40;</span>SystemParameters.<span style="color: #0000FF;">VirtualScreenLeft</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenTop</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenWidth</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenHeight</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rect snapToBorder <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Rect<span style="color: #000000;">&#40;</span>SystemParameters.<span style="color: #0000FF;">VirtualScreenLeft</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Left</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenTop</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Top</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenWidth</span> <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>SnapDistance.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Right</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenHeight</span> <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>SnapDistance.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Bottom</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce left boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">&lt;</span> snapToBorder.<span style="color: #0000FF;">Left</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;windowPosition.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>validArea.<span style="color: #0000FF;">Left</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce top boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">&lt;</span> snapToBorder.<span style="color: #0000FF;">Y</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;windowPosition.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>validArea.<span style="color: #0000FF;">Top</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce right boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Right</span> <span style="color: #008000;">&gt;</span> snapToBorder.<span style="color: #0000FF;">Right</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;windowPosition.<span style="color: #0000FF;">Left</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>validArea.<span style="color: #0000FF;">Right</span> <span style="color: #008000;">-</span> windowPosition.<span style="color: #0000FF;">Width</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce bottom boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Bottom</span> <span style="color: #008000;">&gt;</span> snapToBorder.<span style="color: #0000FF;">Bottom</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;windowPosition.<span style="color: #0000FF;">Top</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>validArea.<span style="color: #0000FF;">Bottom</span> <span style="color: #008000;">-</span> windowPosition.<span style="color: #0000FF;">Height</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>updated<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; Marshal.<span style="color: #0000FF;">StructureToPtr</span><span style="color: #000000;">&#40;</span>windowPosition, lParam, <span style="color: #0600FF;">true</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> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<h3>Download it, or get the source code</h3>

	<p>If you&#8217;d like, you can download the current <a href='http://HuddledMasses.org/wordpress/wp-content/uploads/2008/10/huddledinteropwpf.dll'>Huddled Interop for <span class="caps">WPF</span> library</a> right now, or you can check out the source from CodePlex <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled/">SVN</a> or just download the <a href="http://www.codeplex.com/PoshConsole/SourceControl/ListDownloadableCommits.aspx">most recent commit</a> (You are only interested in the &#8220;Huddled&#8221; project which is in the &#8220;trunk&#8221;, not the &#8220;trunk-3.5&#8221;).  <span class="emph1">The source is licensed freely under the <span class="caps">BSD</span> or <span class="caps">GPL</span> v2</span> (and a few other licenses, see the top of the source code files).</p>

	<p>Either way you&#8217;ll get not just the SnapToBehavior but also the global HotkeysBehavior, and the Native Behaviors framework which I&#8217;ll write more about later, but for now, in case you want to try to use it, here&#8217;s an example using both the SnapTo and HotkeysBehavior (you can find this in the <a href="https://poshconsole.svn.codeplex.com/svn/trunk/GlobalHotkeys">GlobalHotkeys</a> demo project, which includes some sample code for handling hotkey conflicts).  Enjoy.</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys.DemoWindow&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;window&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:huddled</span>=<span style="color: #ff0000;">&quot;http://schemas.huddledmasses.org/wpf&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Demo Window!&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:SnapToBehavior</span> <span style="color: #000066;">SnapDistance</span>=<span style="color: #ff0000;">&quot;80,20,80,10&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:HotkeysBehavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.ActivateWindow&quot;</span> <span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;K&quot;</span> &nbsp;<span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.CloseWindow&quot;</span> &nbsp; &nbsp;<span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;F4&quot;</span> <span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.ToggleWindow&quot;</span> &nbsp; <span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;S&quot;</span> &nbsp;<span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:HotkeysBehavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Drag this window near the screen edges&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

