<?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; PInvoke</title>
	<atom:link href="http://huddledmasses.org/tag/pinvoke/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>WPF Window “Native” Behavior: Metro Window</title>
		<link>http://huddledmasses.org/wpf-metro-windows/</link>
		<comments>http://huddledmasses.org/wpf-metro-windows/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 03:22:25 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Custom Chrome]]></category>
		<category><![CDATA[Metro]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1563</guid>
		<description><![CDATA[A couple of years ago I first introduced my native behaviors for WPF to support enhancing WPF applications with behaviors that require PInvoke or hooking the Window&#8217;s message processing loop: WndProc. You can read all about it on my previous posts, including the original implementations of my Snap To Screen Edges behavior, and my Custom [...]]]></description>
			<content:encoded><![CDATA[	<p>A couple of years ago I first introduced my <a href="http://huddledmasses.org/wpf-window-native-behaviors/">native behaviors for WPF</a> to support enhancing <span class="caps">WPF</span> applications with behaviors that require PInvoke or hooking the Window&#8217;s message processing loop: WndProc.  You can read all about it on my previous posts, including the original implementations of my <a href="http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/">Snap To Screen Edges</a> behavior, and my Custom Chrome for creating windows with buttons on the Aero glass frame like ribbon apps or all those new browsers &#8230;</p>

	<p>However, I&#8217;m writing today because I went back this week to revisit that project because I wanted a &#8220;Metro&#8221; style Window &#8212; basically, something like the Zune client window.</p>

	<p>As I was reviewing it, I discovered <a href="http://msdn.microsoft.com/en-us/library/system.windows.interactivity%28Expression.40%29.aspx">System.Windows.Interactivity</a> class which came out of Expression Blend, and includes the functionality of my NativeBehaviors attached property with regards to allowing you create new behaviors for <span class="caps">WPF</span> elements &#8230; and of course, despite being newer, their system has much broader support than mine  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>So anyway &#8230; long story short: I&#8217;ve rewritten my behaviors on top of <a href="http://msdn.microsoft.com/en-us/library/ff726531%28Expression.40%29.aspx">System.Windows.Interactivity.Behavior<T></a> and written a MetroWindow behavior that makes my PowerBoots application look roughly like this:</p>

	<a href="http://joelbennett.net/wordpress/wp-content/uploads/2010/11/PoshConsole_Metro.png"><img src="http://joelbennett.net/wordpress/wp-content/uploads/2010/11/PoshConsole_Metro-298x300.png" alt="PoshConsole with the MetroWindow behavior" title="PoshConsole_Metro" width="298" height="300" class="size-medium wp-image-1565" /></a>

	<p>I&#8217;m really pleased with the results, and while I was at it I finally fixed a weird glitch in the Snap-To behavior when using the Win+Left or Win+Right keys in Windows 7 to move the window docked from the left of one monitor to the right side of another (or vice versa), and of course this also includes the global hotkeys behavior, and my Quake-console behavior  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  The code to make add the MetroWindow behavior along with the SnapTo would be just:</p>

	<div class="xaml code xaml" style="font-family:monospace;"><br />
&nbsp; &nbsp; &lt;i:Interaction.Behaviors&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;huddled:MetroWindow CaptionHeight=&quot;58&quot; ButtonSize=&quot;20&quot; /&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;huddled:SnapToBehavior SnapDistance=&quot;22&quot; SnapMargin=&quot;18&quot; /&gt;<br />
&nbsp; &nbsp; &lt;/i:Interaction.Behaviors&gt;<br />
&nbsp;</div>

	<p>I&#8217;ll write this up a bit more later when I&#8217;m ready to release PoshConsole and the revised Quake behavior, for now you&#8217;ll have to be content with the screenshots, unless you want to <a href="http://poshconsole.codeplex.com/SourceControl/list/changesets">grab source code</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wpf-metro-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using alternate credentials with the FileSystem in PowerShell</title>
		<link>http://huddledmasses.org/using-alternate-credentials-with-the-filesystem-in-powershell/</link>
		<comments>http://huddledmasses.org/using-alternate-credentials-with-the-filesystem-in-powershell/#comments</comments>
		<pubDate>Fri, 21 May 2010 04:53:55 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Add-Type]]></category>
		<category><![CDATA[Credentials]]></category>
		<category><![CDATA[FileShare]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[UNC]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1478</guid>
		<description><![CDATA[In PowerShell, cmdlets like Get-ChildItem and Get-Content support a -Credential parameter so you can access resources that require you to log in&#8230; the problem is that the built-in FileSystem provider does not. So as an example, if you have a server on a different domain and you want to copy files off of it, you [...]]]></description>
			<content:encoded><![CDATA[	<p>In PowerShell, cmdlets like Get-ChildItem and Get-Content support a -Credential parameter so you can access resources that require you to log in&#8230; the problem is that the built-in FileSystem provider does not. So as an example, if you have a server on a different domain and you want to copy files off of it, you can&#8217;t do this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$cred</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><br />
<span style="color: #0066cc; font-style: italic;">Copy-<span style="font-style: normal;">Item</span></span> \Server\Share\Folder\<span style="color: #66cc66;">*</span>.<span style="color: #003366;">log</span> C:\Logs <span style="color: #000066;">-Credential</span> <span style="color: #660033; font-weight: bold;">$cred</span></div>

	<p>That will throw an error: &#8220;Cannot retrieve the dynamic parameters for the cmdlet. The provider does not support the use of credentials. Perform the operation again without specifying credentials.&#8221;</p>

	<h4>Impersonation</h4>

	<p>To solve this problem at work, I&#8217;ve written an impersonation module. It basically has two methods: Push-ImpersonationContext and Pop-ImpersonationContext. There is one catch: you need to be running in <strong>single-threaded</strong> apartment mode for it to work, because the impersonation only affects the current <a class="zem_slink" href="http://en.wikipedia.org/wiki/Thread_%28computer_science%29" title="Thread (computer science)" rel="wikipedia">thread</a> (if you&#8217;re not running <code>PowerShell.exe -STA</code>, your commands execute on a <a class="zem_slink" href="http://en.wikipedia.org/wiki/Thread_pool_pattern" title="Thread pool pattern" rel="wikipedia">thread pool</a>, so you never know from one to the next what thread you&#8217;ll be on). In any case, you use it like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$cred</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><br />
<span style="color: #0066cc; font-style: italic;">Push-<span style="font-style: normal;">ImpersonationContext</span></span> <span style="color: #660033; font-weight: bold;">$cred</span><br />
<span style="color: #0066cc; font-style: italic;">Copy-<span style="font-style: normal;">Item</span></span> \Server\Share\Folder\<span style="color: #66cc66;">*</span>.<span style="color: #003366;">log</span> C:\Logs<br />
<span style="color: #0066cc; font-style: italic;">Pop-<span style="font-style: normal;">ImpersonationContext</span></span><br />
&nbsp;</div>

	<p>It&#8217;s really very simple, and works great for when you need to access resources across multiple domains. Particularly files, for which PowerShell doesn&#8217;t support alternate credentials at all.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':(' class='wp-smiley' />   Anyway the module code is on PoshCode, save it to your Documents\WindowsPowerShell\Modules\Impersonation\Impersonation.psm1 and use <code>Import-Module Impersonation</code> to load it.  Here you go:</p>

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

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/b2807eb4-e4ea-4df2-8125-5b136d68ce3d/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=b2807eb4-e4ea-4df2-8125-5b136d68ce3d" 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/using-alternate-credentials-with-the-filesystem-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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: The Base Class</title>
		<link>http://huddledmasses.org/wpf-window-native-behaviors/</link>
		<comments>http://huddledmasses.org/wpf-window-native-behaviors/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 21:52:18 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[NativeBehaviors]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[Windows Presentation Foundation]]></category>
		<category><![CDATA[WndProc]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=726</guid>
		<description><![CDATA[I wrote yesterday about the WPF Snap-To Behavor that I created, and showed you how simple it is, but I skipped over where the magic happens, so I thought I&#8217;d go through how I created the Native Behaviors collection class, because there are a few cool tricks in here that I wanted to share with [...]]]></description>
			<content:encoded><![CDATA[	<p>I wrote yesterday about the <a href="http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/"><span class="caps">WPF</span> Snap-To Behavor</a> that I created, and showed you how simple it is, but I skipped over where the magic happens, so I thought I&#8217;d go through how I created the Native Behaviors collection class, because there are a few cool tricks in here that I wanted to share with <span class="caps">WPF</span> developers at large (even if you&#8217;re not interested in hooking the Window Procedure).</p>

	<p>The framework for Native Bahaviors is made up of just two classes, the first of which is basically 3 lines of code.</p>

 <code>NativeBehavior</code> is the abstract base class for behaviors, and consists of just definitions for the mandatory abstract GetHandlers() method (where you return an <code>IEnumerable</code> collection of mappings from Window Messages to your delegate methods) and the two optional (virtual) methods <code>AddTo</code> and <code>RemoveFrom</code> which are called when your behavior is initially added to a window (generally <strong>before</strong> the Window is initialized).

	<p><code>NativeBehaviors</code> is an <a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx">ObservableCollection</a> of <code>NativeBehavior</code>s, obviously  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> . But it&#8217;s ever so much more than that.  First of all, it has the <code>NativeBehaviors</code> attached property, but it also has the code for actually hooking a <span class="caps">WPF</span> window to get the Window Messages, and a WndProc which processes each message against the mappings retrieved from the various <code>NativeBehavior</code>s that have been registered.</p>

	<p>Lets start with the attached dependency property.  There&#8217;s a very clever (and problematic) trick in the way this property is exposed.  If you look at this code, you&#8217;ll see I create a <strong>private</strong> NativeBehavior dependency property, and then create public Get/Set accessors for &#8220;Behavior&#8221; which just call the NativeBehavior property.  The reason for this is that if the dependency property is public, or if the public accessors even have the same name &#8230; the <span class="caps">XAML</span> parser finds the dependency property and uses it directly (bypassing the get/set accesors), which means you have to have an extra element in your <span class="caps">XAML</span> markup to initialize the NativeBehaviors collection, or you get something like this screenshot.</p>

	<a href="http://HuddledMasses.org/wordpress/wp-content/uploads/2008/10/behaviorsisnull.png"><img src="http://HuddledMasses.org/wordpress/wp-content/uploads/2008/10/behaviorsisnull-300x116.png" alt="The behavior collection doesn't get created..." title="Behavior Is Null" class="size-medium wp-image-727" width="300" height="116" /></a>

	<p>So instead, we hide the dependency property, and supply a getter which is backed by the dependency property.  I should not that although this <strong>works</strong> great in the .Net Framework, it&#8217;s not recognized properly by all of the tools (including Resharper 4.1 and even Expression Blend), so you may have to fiddle with things.</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> DependencyProperty NativeBehaviorsProperty <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DependencyProperty.<span style="color: #0000FF;">RegisterAttached</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">&quot;NativeBehaviors&quot;</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>NativeBehaviors<span style="color: #000000;">&#41;</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>NativeBehaviors<span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FrameworkPropertyMetadata<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// A public setter (for the non-existent &quot;Behaviors&quot; property)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SetBehaviors<span style="color: #000000;">&#40;</span>Window window, NativeBehaviors behaviors<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;">if</span> <span style="color: #000000;">&#40;</span>window <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <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> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;window&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>NativeBehaviorsProperty, behaviors<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: #008080; font-style: italic;">// The public getter &nbsp;(for the non-existent &quot;Behaviors&quot; property)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> NativeBehaviors GetBehaviors<span style="color: #000000;">&#40;</span>Window window<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span> &nbsp;<span style="color: #008080; font-style: italic;">// instead of GetValue, call the accessor!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> GetNativeBehaviors<span style="color: #000000;">&#40;</span>window<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: #008080; font-style: italic;">// This dependency property getter makes sure it returns a collection</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> NativeBehaviors GetNativeBehaviors<span style="color: #000000;">&#40;</span>Window window<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;">if</span> <span style="color: #000000;">&#40;</span>window <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <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> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;window&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// This is the plain old normal thing:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var behaviors <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>NativeBehaviors<span style="color: #000000;">&#41;</span>window.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>NativeBehaviorsProperty<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Our raison d'tre: create a new collection if there isn't one yet</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>behaviors <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> behaviors <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NativeBehaviors<span style="color: #000000;">&#40;</span>window<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> behaviors<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>There&#8217;s a little more to it in the <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled/Wpf/NativeBehaviors.cs">actual class source code</a>, I&#8217;m glossing over the simplest parts, and leaving out all the <span class="caps">XML</span> documentation comments too.  We override the CollectionChanged event so that we can hook new Behaviors as they arrive, and we make sure to hook the WndProc whenever the Target Window is set:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnCollectionChanged<span style="color: #000000;">&#40;</span>NotifyCollectionChangedEventArgs nccea<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnCollectionChanged</span><span style="color: #000000;">&#40;</span>nccea<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// design mode bailout because NativeBehaviors don't work in DesignMode</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>DesignerProperties.<span style="color: #0000FF;">GetIsInDesignMode</span><span style="color: #000000;">&#40;</span>Target<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> return<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// notify new behaviors they are being hooked up, and track their handlers</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>nccea.<span style="color: #0000FF;">Action</span> <span style="color: #008000;">==</span> NotifyCollectionChangedAction.<span style="color: #0000FF;">Add</span> <span style="color: #008000;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;nccea.<span style="color: #0000FF;">Action</span> <span style="color: #008000;">==</span> NotifyCollectionChangedAction.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>NativeBehavior behavior <span style="color: #0600FF;">in</span> nccea.<span style="color: #0000FF;">NewItems</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;behavior.<span style="color: #0000FF;">AddTo</span><span style="color: #000000;">&#40;</span>Target<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Handlers.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>behavior.<span style="color: #0000FF;">GetHandlers</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; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// notify removed behaviors they are being unhooked, and stop tracking their handlers</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>nccea.<span style="color: #0000FF;">Action</span> <span style="color: #008000;">==</span> NotifyCollectionChangedAction.<span style="color: #0000FF;">Remove</span> <span style="color: #008000;">||</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;nccea.<span style="color: #0000FF;">Action</span> <span style="color: #008000;">==</span> NotifyCollectionChangedAction.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>NativeBehavior behavior <span style="color: #0600FF;">in</span> nccea.<span style="color: #0000FF;">OldItems</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;behavior.<span style="color: #0000FF;">RemoveFrom</span><span style="color: #000000;">&#40;</span>Target<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var h <span style="color: #0600FF;">in</span> behavior.<span style="color: #0000FF;">GetHandlers</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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; Handlers.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>h<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; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Hook the Window when its added</span><br />
<span style="color: #008080; font-style: italic;">// but keep only a week reference to it...</span><br />
<span style="color: #0600FF;">public</span> Window Target<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;get<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_owner <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> _owner.<span style="color: #0000FF;">Target</span> <span style="color: #0600FF;">as</span> Window<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;set<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// design mode bailout (in Design mode there's no window, and no wndproc)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>DesignerProperties.<span style="color: #0000FF;">GetIsInDesignMode</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> return<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_owner <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> WindowHandle <span style="color: #008000;">!=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HwndSource.<span style="color: #0000FF;">FromHwnd</span><span style="color: #000000;">&#40;</span>WindowHandle<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">RemoveHook</span><span style="color: #000000;">&#40;</span>WndProc<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; Debug.<span style="color: #0000FF;">Assert</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> <span style="color: #008000;">!=</span> value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; _owner <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WeakReference<span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Check for an HWND to determine if the Window has been loaded.</span><br />
&nbsp; &nbsp; &nbsp; WindowHandle <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WindowInteropHelper<span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Handle</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IntPtr.<span style="color: #0000FF;">Zero</span> <span style="color: #008000;">==</span> WindowHandle<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// If there's no handle, set the hook when the Source is initialised.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value.<span style="color: #0000FF;">SourceInitialized</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span>sender, e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WindowHandle <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WindowInteropHelper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>Window<span style="color: #000000;">&#41;</span>sender<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Handle</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// hook the WndProc </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HwndSource.<span style="color: #0000FF;">FromHwnd</span><span style="color: #000000;">&#40;</span>WindowHandle<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddHook</span><span style="color: #000000;">&#40;</span>WndProc<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// hook the WndProc </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HwndSource.<span style="color: #0000FF;">FromHwnd</span><span style="color: #000000;">&#40;</span>WindowHandle<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddHook</span><span style="color: #000000;">&#40;</span>WndProc<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 />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #008080; font-style: italic;">// the weak reference to the actual window...</span><br />
<span style="color: #0600FF;">private</span> WeakReference _owner<span style="color: #008000;">;</span><br />
&nbsp;</div>

	<p>And finally, the last part of the puzzle is my actual WndProc implementation, which has a minor drawback in that we can hypothetically have multiple behaviors which process the same window message &#8230; and may each return a different value (which we cannot reconcile).  For now I&#8217;m just returning the last (non-zero) result (in actuality, all of the behaviors I&#8217;ve written thus far return zero).</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">private</span> IntPtr WndProc<span style="color: #000000;">&#40;</span>IntPtr hwnd, <span style="color: #FF0000;">int</span> msg, IntPtr wParam, IntPtr lParam, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">bool</span> handled<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;Debug.<span style="color: #0000FF;">Assert</span><span style="color: #000000;">&#40;</span>hwnd <span style="color: #008000;">==</span> WindowHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Only expecting messages for our cached HWND.</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// cast and cache the message</span><br />
&nbsp; &nbsp;var message <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">WindowMessage</span><span style="color: #000000;">&#41;</span>msg<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// NOTE: we may process a message multiple times</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// and we have no good way to handle that...</span><br />
&nbsp; &nbsp;var result <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var handlePair <span style="color: #0600FF;">in</span> Handlers<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span> &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>handlePair.<span style="color: #0000FF;">Key</span> <span style="color: #008000;">==</span> message<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var r <span style="color: #008000;">=</span> handlePair.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#40;</span>wParam, lParam, <span style="color: #0600FF;">ref</span> handled<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// So, we'll return the last non-zero result (if any)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>r <span style="color: #008000;">!=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> result <span style="color: #008000;">=</span> r<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span> &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>That pretty much covers the framework.  The actual behaviors can be fairly simple like my <a href="http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/">Snap-To</a> behavior (which is actually <em>too</em> simple), or extremely complex like my latest behavior, which is a port <a href="http://code.msdn.microsoft.com/chrome">Joe Castro&#8217;s custom Window &#8216;Chrome&#8217;</a> to my behavior framework, and I&#8217;m hoping some of you will choose to write new ones and submit them as patches to the CodePlex project (for now, I&#8217;m just piggybacking it on the <a href="http://CodePlex.com/PoshConsole">PoshConsole</a> project). But in any case, the latest code is available <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled">via subversion</a> on CodePlex, and you can download <a href="http://HuddledMasses.org/wordpress/wp-content/uploads/2008/10/huddledinteropwpf.7z">today&#8217;s snapshot here</a>.</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/2128e76c-26a5-40d9-8e1d-cd6d8ddd318f/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=2128e76c-26a5-40d9-8e1d-cd6d8ddd318f" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wpf-window-native-behaviors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get the Char(acter) and VirtualKey from a WPF KeyDown event</title>
		<link>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/</link>
		<comments>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 05:02:34 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Char]]></category>
		<category><![CDATA[EventHandler]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[KeyDown]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[ScanCode]]></category>
		<category><![CDATA[VirtualKey]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=699</guid>
		<description><![CDATA[While working on my WPF PowerShell console, I&#8217;m working on implementing PSRawUserInterface, and had to implement a method called ReadKey which returns a KeyInfo object. KeyInfo is a pretty simple struct class with four properties: VirtualKeyCode Character ControlKeyState KeyDown So it doesn&#8217;t seem like it would be a real problem &#8230; just handle the KeyDown [...]]]></description>
			<content:encoded><![CDATA[	<p>While working on my <span class="caps">WPF</span> <a href="http://www.microsoft.com/powershell" title="Windows PowerShell" rel="homepage" class="zem_slink">PowerShell</a> console, I&#8217;m working on implementing <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.aspx">PSRawUserInterface</a>, and had to implement a method called <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface.readkey.aspx">ReadKey</a> which returns a <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.host.keyinfo.aspx">KeyInfo</a> object.  KeyInfo is a pretty simple struct class with four properties:</p>

	<ul>
		<li>VirtualKeyCode</li>
		<li>Character </li>
		<li>ControlKeyState</li>
	</ul>
	<ul>
		<li>KeyDown</li>
	</ul>

	<p>So it doesn&#8217;t seem like it would be a real problem &#8230; just handle the <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.keydown.aspx">KeyDown</a> <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown.aspx">event</a> or <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.previewkeydown.aspx">PreviewKeyDown</a> <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewkeydown.aspx">event</a> on the <span class="caps">WPF</span> control, right? Well, no. Because all of these just use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyeventargs.aspx">KeyEventArgs</a> parameter which has a <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.key.aspx">Key</a> property which doesn&#8217;t map to a virtual key code (why isn&#8217;t the <span class="caps">WPF</span> Key enumeration in the right order? It&#8217;s ridiculous), and there&#8217;s no character information at all.</p>

	<p>Thankfully, it <strong>is</strong> possible to get this information using PInvoke, but I wouldn&#8217;t want to try to do this in <a href="http://www.microsoft.com/silverlight/default.aspx" title="Microsoft Silverlight" rel="homepage" class="zem_slink">Silverlight</a>.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  </p>

	<p>Here&#8217;s how it goes:</p>

	<p>First, you need to get the VirtualKey code. Thankfully, there&#8217;s a simple class called <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyinterop.aspx">KeyInterop</a>, which exposes a static method <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.keyinterop.virtualkeyfromkey.aspx">VirtualKeyFromKey</a> that gets us this information.</p>

	<p>Then, we need to get the character. This is much trickier.  First we have to <a href="http://msdn.microsoft.com/en-us/library/ms646299.aspx">get the keyboard state</a> and then we have to <a href="http://msdn.microsoft.com/en-us/library/ms646306.aspx">map that VirtualKey</a> we got in the first step to a ScanCode, and finally, convert all of that <a href="http://msdn.microsoft.com/en-us/library/ms646320.aspx">to Unicode</a>, because .Net doesn&#8217;t really speak <span class="caps">ASCII</span>  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>The rest of the code is pretty clear, I hope&#8230;<span id="more-699"></span> you can find the <em>current</em> version of <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled/Interop/Keyboard.cs">Keyboard.cs</a> on CodePlex in the PoshCode source repository, but to make this complete, here&#8217;s the version as it stands now:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Management.Automation.Host</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.Text</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Input</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled.<span style="color: #0000FF;">Interop</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Keyboard<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;The set of valid MapTypes used in MapVirtualKey</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> MapType <span style="color: #008000;">:</span> <span style="color: #FF0000;">uint</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a virtual-key code and is translated into a scan code.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// If it is a virtual-key code that does not distinguish between left- and</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// right-hand keys, the left-hand scan code is returned.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// If there is no translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_VSC <span style="color: #008000;">=</span> 0x0,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a scan code and is translated into a virtual-key code that</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// does not distinguish between left- and right-hand keys. If there is no</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VSC_TO_VK <span style="color: #008000;">=</span> 0x1,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;uCode is a virtual-key code and is translated into an unshifted</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// character value in the low-order word of the return value. Dead keys (diacritics)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// are indicated by setting the top bit of the return value. If there is no</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_CHAR <span style="color: #008000;">=</span> 0x2,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Windows NT/2000/XP: uCode is a scan code and is translated into a</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// virtual-key code that distinguishes between left- and right-hand keys. If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// there is no translation, the function returns 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VSC_TO_VK_EX <span style="color: #008000;">=</span> 0x3,<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Not currently documented</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;&lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAPVK_VK_TO_VSC_EX <span style="color: #008000;">=</span> 0x4,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The ToUnicode function translates the specified virtual-key code and keyboard state </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// to the corresponding Unicode character or characters. To specify a handle to the keyboard layout </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// to use to translate the specified code, use the ToUnicodeEx function.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// </span></div>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4d89ee6c-d3c1-45fd-a043-eaef925688b6/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4d89ee6c-d3c1-45fd-a043-eaef925688b6" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/how-to-get-the-character-and-virtualkey-from-a-wpf-keydown-event/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>

