<?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; C++</title>
	<atom:link href="http://huddledmasses.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>More Custom Attributes for PowerShell (Parameter Transformation)</title>
		<link>http://huddledmasses.org/more-custom-attributes-for-powershell-parameters/</link>
		<comments>http://huddledmasses.org/more-custom-attributes-for-powershell-parameters/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 22:27:39 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Transformation]]></category>
		<category><![CDATA[Type Cast]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1623</guid>
		<description><![CDATA[I wrote a post awhile back about using custom attributes for PowerShell parameter validation but when I did it, I focused on the use of attributes to improve the error messages output by validation (specifically, by: ValidatePattern). There are many other things that can be done with custom attributes. However, PowerShell ships with two base [...]]]></description>
			<content:encoded><![CDATA[	<p>I wrote a post awhile back about using <a href="http://huddledmasses.org/better-error-messages-for-powershell-validatepattern/">custom attributes for PowerShell parameter validation</a> but when I did it, I focused on the use of attributes to improve the error messages output by validation (specifically, by: ValidatePattern).</p>

	<p>There are many other things that can be done with custom attributes. However, PowerShell ships with two <strong>base</strong> types for attributes which derive from the <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.internal.cmdletmetadataattribute.aspx">CmdletMetadataAttribute</a>, and it applies special processing to parameters of functions or cmdlets which have these attributes: <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.validateargumentsattribute.aspx">ValidateArguments</a> and <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.argumenttransformationattribute.aspx">ArgumentTransformation</a>, since I&#8217;ve already written about custom argument validation, I figured a post about argument transformations would be appropriate.</p>

	<p>It just so happens that I had cause to write such an attribute recently:</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.ComponentModel</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Management.Automation</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Automation</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #000000;">&#91;</span>AttributeUsage<span style="color: #000000;">&#40;</span>AttributeTargets.<span style="color: #0000FF;">Field</span> <span style="color: #008000;">|</span> AttributeTargets.<span style="color: #0000FF;">Property</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> StaticFieldAttribute <span style="color: #008000;">:</span> ArgumentTransformationAttribute <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> Type _class<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;[StaticField(OfClass='{0}')]&quot;</span>, OfClass.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">Object</span> Transform<span style="color: #000000;">&#40;</span> EngineIntrinsics engineIntrinsics, <span style="color: #FF0000;">Object</span> inputData<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>inputData <a href="http://www.google.com/search?q=is+msdn.microsoft.com"><span style="color: #008000;">is</span></a> <span style="color: #FF0000;">string</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>inputData <span style="color: #0600FF;">as</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">FieldInfo</span> field <span style="color: #008000;">=</span> _class.<span style="color: #0000FF;">GetField</span><span style="color: #000000;">&#40;</span>inputData <span style="color: #0600FF;">as</span> <span style="color: #FF0000;">string</span>, BindingFlags.<span style="color: #0600FF;">Static</span> <span style="color: #008000;">|</span> BindingFlags.<span style="color: #0600FF;">Public</span><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>field <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; &nbsp; <span style="color: #0600FF;">return</span> field.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</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; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> inputData<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> StaticFieldAttribute<span style="color: #000000;">&#40;</span> Type ofClass <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; OfClass <span style="color: #008000;">=</span> ofClass<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> Type OfClass <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _class<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; set <span style="color: #000000;">&#123;</span> _class <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span> &nbsp; <br />
<span style="color: #000000;">&#125;</span></div>

	<p>This was written in C#, but you can wrap it up in an <code>Add-Type -TypeDefinition</code> in PowerShell as I did in the latest preview of my <a href="http://poshcode.org/2483" title="WASP">Windows Automation Scripts for PowerShell</a> &#8230; and basically embed it in a script, a module, or your profile. A note of warning: you should specify a namespace, really, to avoid collisions.</p>

	<p>However, if you can&#8217;t read C#, or if you&#8217;re not experienced with reflection, you may not even be able to tell what that code does, so let me explain quickly. </p>

	<p>Basically, an <code>ArgumentTransformationAttribute</code> is very simple: it just has to have a <code>Transform</code> method which converts an input object into an output object.  The Transform method has access to the PowerShell <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.engineintrinsics_members%28VS.85%29.aspx">EngineIntrinsics</a> class which gives you access to the current Host, the current SessionState to get variable values, etc. as well as being able to Invoke Commands or PSProviders&#8230;</p>

	<p>In the example above, I&#8217;m trying to transform a string into an object. There&#8217;s a collection of the type of objects that I want which are defined as static fields on a certain class, so I created an argument transformation which takes the string, looks for a field with that name, and returns it.  I made the class that it looks on configurable, so it would be flexible, so the actual usage looks something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ControlType</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>StaticField<span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ControlType</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$ControlType</span><br />
<span style="color: #333;">&#41;</span></div>

	<p>This parameter will now take a <code>ControlType</code> object, or the <span class="caps">NAME</span> of one of the fields on ControlType (which it will look up and return), so instead of always having to call: <code>Select-UIElement -ControlType [System.Windows.Automation.ControlType]::Button</code>, I can just write: <code>Select-UIElement -ControlType Button</code> &#8230; which is clearly a bit nicer to use.</p>

	<p>PowerShell has one of these argument transformations included for use with credentials, so whenever you write a script that has a <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.pscredential%28VS.85%29.aspx">PSCredential</a> parameter, you should decorate it with the <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.credentialattribute%28VS.85%29.aspx">CredentialAttribute</a> like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">Credential</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$Credential</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Empty</span><br />
<span style="color: #333;">&#41;</span></div>

	<p>That one&#8217;s a little confusing because you leave off the &#8220;Attribute&#8221; part of the attribute&#8217;s name (ie: you don&#8217;t have to specify <code>[System.Management.Automation.CredentialAttribute()]</code>), so at first glance, it looks like you&#8217;re specifying the Credential type twice. Of course, in reality this is another <a href="http://huddledmasses.org/parenthesis-in-powershell/">use of parenthesis in PowerShell</a>. To specify an attribute, you use square braces as with types, but with parenthesis in them (even if the attribute doesn&#8217;t require any parameters).</p>

	<h2>Type <em>then</em> Attribute, and non-mandatory parameters</h2>

	<p>When you specify a transformation on a parameter, you must be careful to specify the <strong>type first</strong> and then the transformation attribute (although this may seem counter-intuitive if you&#8217;re a developer). This puts the transformation closest to the variable name, and ensures that it is called <em>before</em> the value is cast to the parameter type.</p>

	<p>When you don&#8217;t specify a parameter as mandatory, and you <strong>do</strong> specify a transformation attribute, the attribute&#8217;s <code>Transform</code> method will still be called, even if the user doesn&#8217;t provide you with an input. This is so that the transform attribute can provide a default value if need be.  However, it&#8217;s called with a null value for the input data &#8212; this means that your attribute needs to be able to deal with a null value and output something which the cmdlet or script can deal with. </p>

	<p>Depending on your use cases, it may be enough to just output null, but in the case of the Credential parameter, passing an empty string causes the Credential entry dialog to pop up.  That&#8217;s the desired behavior if you want it to be mandatory, but otherwise, you need to be sure to provide a default value such as the Empty credentials &#8212; this will supress the prompt, but it will return an empty credential object you can easily distinguish from a passed-in value. In any case, if nothing is passed and the parameter isn&#8217;t marked manadatory, but the transform object creates a default value, the <code>$PSBoundParameters</code> should still be empty.</p>

	<h2>One Last Hurrah</h2>

	<p>To make this post as useful as I can, I&#8217;ve written a <code>TransformAttribute</code> that takes a script block to do the transform with, and a few examples of using it. In these examples I always assume the input is a string which will be converted into an environment variable, a static field value (this example does exactly the same thing as the class above, but using the generic ScriptBlock-based <code>Transform</code>), and even transforming user names into email addresses (by looking them up in Active Directory).  I&#8217;ll post the code in PowerShell format this time, with the requisite Add-Type wrapped around it:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-TypeDefinition</span> @<span style="color: #009900;">&quot;<br />
using System;<br />
using System.ComponentModel;<br />
using System.Management.Automation;<br />
using System.Collections.ObjectModel;<br />
<br />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]<br />
public class TransformAttribute : ArgumentTransformationAttribute {<br />
&nbsp; &nbsp;private ScriptBlock _scriptblock;<br />
&nbsp; &nbsp;private string _noOutputMessage = &quot;</span>Transform Script had no output.<span style="color: #009900;">&quot;;<br />
<br />
&nbsp; &nbsp;public override string ToString() {<br />
&nbsp; &nbsp; &nbsp; return string.Format(&quot;</span><span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span>Script<span style="color: #66cc66;">=</span><span style="color: #009900;">'{{{0}}}'</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #009900;">&quot;, Script);<br />
&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;public override Object Transform( EngineIntrinsics engine, Object inputData) {<br />
&nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collection&lt;PSObject&gt; output = <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; engine.InvokeCommand.InvokeScript( engine.SessionState, Script, inputData );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(output.Count &gt; 1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object[] transformed = new Object[output.Count];<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i =0; i &lt; output.Count;i++) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;transformed[i] = output[i].BaseObject;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return transformed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if(output.Count == 1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return output[0].BaseObject;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentTransformationMetadataException(NoOutputMessage);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br />
&nbsp; &nbsp; &nbsp; } catch (ArgumentTransformationMetadataException) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw;<br />
&nbsp; &nbsp; &nbsp; } catch (Exception e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new ArgumentTransformationMetadataException(string.Format(&quot;</span>Transform Script threw an exception <span style="color: #333;">&#40;</span><span style="color: #009900;">'{0}'</span><span style="color: #333;">&#41;</span>. <span style="color: #003366;">See</span> `<span style="color: #660033; font-weight: bold;">$Error</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Exception</span>.<span style="color: #003366;">InnerException</span>.<span style="color: #003366;">InnerException</span> <span style="color: #666699; font-weight: bold;">for</span> <span style="color: #660033;">more</span> details.<span style="color: #009900;">&quot;,e.Message), e);<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;public TransformAttribute() {<br />
&nbsp; &nbsp; &nbsp; this.Script = ScriptBlock.Create(&quot;</span><span style="color: #333;">&#123;</span>`<span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#125;</span><span style="color: #009900;">&quot;);<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;public TransformAttribute( ScriptBlock Script ) {<br />
&nbsp; &nbsp; &nbsp; this.Script = Script;<br />
&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;public ScriptBlock Script {<br />
&nbsp; &nbsp; &nbsp; get { return _scriptblock; }<br />
&nbsp; &nbsp; &nbsp; set { _scriptblock = value; }<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;public string NoOutputMessage {<br />
&nbsp; &nbsp; &nbsp; get { return _noOutputMessage; }<br />
&nbsp; &nbsp; &nbsp; set { _noOutputMessage = value; }<br />
&nbsp; &nbsp;} &nbsp; <br />
}<br />
&quot;</span>@ <br />
<br />
<span style="color: #666666; font-style: italic;">## Some example transformations:</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Convert a string into the value of the named environment variable (or error)</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEnvironment</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<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><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> <span style="color: #009900;">&quot;Env:$($args[0])&quot;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Environment</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #660033; font-weight: bold;">$Environment</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Test TransformEnvironment </span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEnvironment</span></span> UserName<br />
<span style="color: #666666; font-style: italic;"># Test Error Message 1:</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEnvironment</span></span> <span style="color: #009900;">&quot;This is not an environment variable name&quot;</span><br />
<br />
<br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> UIAutomationTypes<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformStaticFieldValue</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ControlType</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">$FieldName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$field</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ControlType</span><span style="color: #333;">&#93;</span></span>.<span style="color: #003366;">GetField</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$FieldName</span>, <span style="color: #009900;">&quot;IgnoreCase,Public,Static&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$field</span>.<span style="color: #003366;">GetValue</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$ControlType</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$ControlType</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Test TransformStaticFieldValue</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformStaticFieldValue</span></span> Button<br />
<span style="color: #666666; font-style: italic;"># Test Error Message 2:</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformStaticFieldValue</span></span> DoorHandle<br />
<br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEmail</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span>NoOutputMessage <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Specified value is not an email address, and we could not find a user by that name&quot;</span>, Script <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$UserName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$UserName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$UserName</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Username&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ads</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">DirectoryServices</span>.<span style="color: #003366;">DirectorySearcher</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;">ADSI</span><span style="color: #333;">&#93;</span></span><span style="color: #009900;">''</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$UserName</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$a&quot;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Contains</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;@&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$a</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ads</span>.<span style="color: #666699; font-weight: bold;">filter</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(|(samAccountName=$a)(displayName=$a))&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$ads</span>.<span style="color: #003366;">FindAll</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">GetEnumerator</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">GetDirectoryEntry</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Mail</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$UserEmail</span><br />
&nbsp; &nbsp; &nbsp; <br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #660033; font-weight: bold;">$UserEmail</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Test TransformEmail</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEmail</span></span> <span style="color: #000066;">-User</span> <span style="color: #660033; font-weight: bold;">$Env</span>:UserName<br />
<span style="color: #666666; font-style: italic;"># Test Error Message 3:</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">TransformEmail</span></span> <span style="color: #000066;">-User</span> Gremlins<br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">StackingTransforms</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Transform UserNames to Email Adresses</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span>NoOutputMessage <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Specified value is not an email address, and we could not find a user by that name&quot;</span>, Script <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$UserName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ads</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">DirectoryServices</span>.<span style="color: #003366;">DirectorySearcher</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;">ADSI</span><span style="color: #333;">&#93;</span></span><span style="color: #009900;">''</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$UserName</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$a&quot;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Contains</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;@&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$a</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ads</span>.<span style="color: #666699; font-weight: bold;">filter</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(|(samAccountName=$a)(displayName=$a))&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$ads</span>.<span style="color: #003366;">FindAll</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">GetEnumerator</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">GetDirectoryEntry</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Mail</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Transform a path to it's content</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Transform<span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> <span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> Env:\Username <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$UserEmail</span><br />
&nbsp; &nbsp; &nbsp; <br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$UserEmail</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Rely on the default value</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">StackingTransforms</span></span><br />
<br />
<span style="color: #666666; font-style: italic;">## Specify an environment variable</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">StackingTransforms</span></span> Env:\UserName<br />
<br />
<span style="color: #666666; font-style: italic;">## Read from a file (first, build the list)</span><br />
<span style="color: #660033; font-weight: bold;">$Env</span>:UserName <span style="color: #66cc66;">&gt;</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\users.<span style="color: #003366;">txt</span><br />
<span style="color: #660033; font-weight: bold;">$Env</span>:UserName <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\users.<span style="color: #003366;">txt</span><br />
<span style="color: #660033; font-weight: bold;">$Env</span>:UserName <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\users.<span style="color: #003366;">txt</span><br />
<span style="color: #660033; font-weight: bold;">$Env</span>:UserName <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\users.<span style="color: #003366;">txt</span><br />
<span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">StackingTransforms</span></span> <span style="color: #660033; font-weight: bold;">$pwd</span>\users.<span style="color: #003366;">txt</span><br />
&nbsp;</div>

	<p>I want to point out two things about these examples: </p>

	<p>In the <code>TransformAttribute</code> there is custom error handling for the script: exceptions are handled and printed, the case where you get no output is handled and printed, and you can provide your own error message for that no output case.</p>

	<p>Notice that not all of the transformed parameters are mandatory: in the last two I&#8217;ve dealt with null input specially by either prompting the user from inside the transform script, or using a default value when none is provided. These are powerful options which you should use with care. Prompting from inside the script will allow you to build up pretty much anything you need in the transformation attribute, and having a default value can be just as useful, but you need to make sure that you keep the assumptions in your script in line with what actually happens in the parameter processing.</p>

	<p>Hopefully this will give you some ideas for how other transformations  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />   If you&#8217;re inspired, please feel free to share examples in the comments: just use a code tag around them: <code>&#60;code lang=&#34;posh&#34;&#62;</code></p>

]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/more-custom-attributes-for-powershell-parameters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to: Invoke PowerShell and use the results from C#?</title>
		<link>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/</link>
		<comments>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 06:51:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Host]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1603</guid>
		<description><![CDATA[One of the questions that comes up a lot about PowerShell lately is how to use it from C#. For the sake of a simple example, I figured it&#8217;s past time I post the sort-of &#8230; Hello World of PowerShell hosting. I&#8217;m going to present this app plus a little bit more at the next [...]]]></description>
			<content:encoded><![CDATA[	<p>One of the questions that comes up a lot about PowerShell lately is how to use it from C#.  For the sake of a simple example, I figured it&#8217;s past time I post the sort-of &#8230; Hello World of PowerShell hosting. I&#8217;m going to present this app plus a little bit more at the next <a href="http://PowerShellGroup.org/rochester.ny">Upstate NY PowerShell User Group</a>, so let&#8217;s go just a little deeper than a greeting. Let&#8217;s assume that you want to run a cmdlet or a script and get the output back to display it in your application. </p>

	<p>Of course, the simplest thing to do is just stick that stuff in a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.aspx">DataGrid</a>, so we&#8217;ll just whip up a simple <span class="caps">WPF</span> window. Yes, <span class="caps">WPF</span> of course. I can&#8217;t go back to Windows Forms, even for just for a simple example like this, but it&#8217;s basically the same except you have to set the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.aspx&#39;s">DataGrid</a> DataSource property instead of the ItemsSource &#8230; anyway, here&#8217;s the XAML:</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfPowerShell.MainWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;700&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGrid</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;dataGrid1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>Just add a reference to System.Management.Automation (it&#8217;s in the GAC: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll) and then in your window&#8217;s constructor (or wherever you like), put this code:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Note: this takes a little while ...</span><br />
var ps <span style="color: #008000;">=</span> PowerShell.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Let's get all the processes:</span><br />
ps.<span style="color: #0000FF;">AddCommand</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Get-Process&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Use the generic overload to cast the PSObjects to their base object:</span><br />
panel.<span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> ps.<span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&lt;</span>Process<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp;</div>

	<p>That&#8217;s it. We&#8217;re done.</p>

	<h3>And now for something slightly more interesting</h3>

	<p>What about if we also wanted an easy way to spot the biggest memory consumers? We could start by sorting the processes by the WorkingSet, and pick just the 15 worst offenders. But let&#8217;s also add a chart to <em>visualize</em> just how bad those top offenders are. In <span class="caps">WPF</span>, this is simple: I&#8217;m going to use the free <a href="http://wpf.amcharts.com/">amCharts</a> in my example, but you can do the same thing with the free <a href="http://visifire.com/">visifire</a> or <a href="http://www.visiblox.com">visiblox</a> charts, or the (commercial only) <a href="http://www.telerik.com/products/wpf/chart.aspx">Telerik</a> <span class="caps">RAD</span> controls &#8230; etc. All of those work in both <span class="caps">WPF</span> and Silverlight (and all three of the &#8220;free&#8221; ones have commercial versions).</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfPowerShell.MainWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:am</span>=<span style="color: #ff0000;">&quot;http://schemas.amcharts.com/charts/wpf/2009/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;PowerShell Demo&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;800&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;3*&quot;</span> <span style="color: #000066;">MaxWidth</span>=<span style="color: #ff0000;">&quot;350&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;4*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;am:PieChart</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;pieChart1&quot;</span> <span style="color: #000066;">ValueMemberPath</span>=<span style="color: #ff0000;">&quot;WorkingSet&quot;</span> <span style="color: #000066;">TitleMemberPath</span>=<span style="color: #ff0000;">&quot;ProcessName&quot;</span> <span style="color: #000066;">SlicesSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGrid</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;dataGrid1&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>Of course, this got a little more complicated. We needed to put the controls in a panel of some sort (I chose to use a Grid mainly because it automatically causes the scroll viewer around the DataGrid, but that forces us to specify the ColumnDefinitions) and the PieChart needs bindings for the value and title of each slice. You&#8217;ll notice I set the ItemsSource of the DataGrid and the SlicesSource of the PieChart to &#8220;{Binding}&#8221; (side note: why can&#8217;t they just use &#8220;ItemsSource&#8221; like normal people?) ... </p>

	<p>Setting the sources to {Binding} causes them to just look at their data context, so now in the code behind, I can bind them <em>both at once</em> by just setting the DataContext of the window (try that in Windows Forms).</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Note: this takes a little while ...</span><br />
var ps <span style="color: #008000;">=</span> PowerShell.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Get the 15 biggest memory users. Note that we're using AddScript now,</span><br />
<span style="color: #008080; font-style: italic;">// We should probably put this in a resource or a separate script file:</span><br />
ps.<span style="color: #0000FF;">AddScript</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Get-Process | Sort WorkingSet | Select -Last 15&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Use the generic overload to cast the PSObjects to their base object, </span><br />
<span style="color: #008080; font-style: italic;">// Just set the DataContext of the window, the controls are data bound</span><br />
DataContext <span style="color: #008000;">=</span> ps.<span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&lt;</span>Process<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp;</div>

	<p>That&#8217;s all there is to it. I can attach my solution after I give my presentation if people want it, but there&#8217;s really nothing else in it (so far) except the references to the amCharts and System.Management.Automation &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clip.exe and the missing Paste.exe</title>
		<link>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/</link>
		<comments>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 16:13:23 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Clipboard]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=783</guid>
		<description><![CDATA[One of the guys on #PowerShell in IRC was asking about how to get the contents of the clipboard in PowerShell, and after we had tried several different scripts, we realized that in Version 1, because PowerShell runs in an MTA thread, you cannot access the clipboard without creating a new thread (in STA mode). [...]]]></description>
			<content:encoded><![CDATA[	<p>One of the guys on <a href="irc://irc.freenode.net/PowerShell">#PowerShell in IRC</a> was asking about how to <strong>get</strong> the contents of the clipboard in PowerShell, and after we had tried several different scripts, we realized that in Version 1, because PowerShell runs in an <a href="http://support.microsoft.com/kb/150777"><span class="caps">MTA</span> thread</a>, you cannot access the clipboard without creating a new thread (in <span class="caps">STA</span> mode).  After I got done ranting, and pointing out that everyone needs to use PowerShell v2 (yeah, it&#8217;s still in <span class="caps">CTP</span>, so I was mostly kidding) in <a href="http://support.microsoft.com/kb/150777">-STA</a> mode &#8230; I whipped together the C# one liner and stuck it into a class with instructions for compiling, and figured I might as well share.</p>

	<p>The code is <a href="clipexe-and-the-missing-pasteexe#more-783">below</a>. There are two files: Clip.cs and Paste.cs &#8230; you probably only need Paste.cs, because Clip.exe is <a href="http://weblogs.asp.net/owscott/archive/2006/12/15/clip-saving-command-line-and-powershell-output-directly-to-the-clipboard.aspx">already included</a> in Windows <em>after</em> Windows XP, but for the sake of completeness (and because it was just a few lines), I wrote them both.</p>

	<p>To use them, you must compile them using <code>Csc.exe</code>, which is included as part of the .Net Framework.  If it&#8217;s not already on your path, it&#8217;s in the folder for your most recent .Net Framework version (eg: C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe  or C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe). All you have to do is run: <code>csc Paste.cs</code> and <code>csc Clip.cs</code> and you should end up with a <code>Paste.exe</code> and <code>Clip.exe</code> which you can use to copy and paste from the command-line&#8230; <span id="more-783"></span></p>

	<h3>Paste.cs</h3>

	<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.Windows.Forms</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Paste <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>STAThread<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args <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;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> line <span style="color: #0600FF;">in</span> Clipboard.<span style="color: #0000FF;">GetText</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringSplitOptions.<span style="color: #0000FF;">None</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> line <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></div>

	<h3>Clip.cs</h3>

	<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.Text</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled <span style="color: #000000;">&#123;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Clip <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#91;</span>STAThread<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">string</span> s<span style="color: #008000;">;</span> <br />
&nbsp; &nbsp; &nbsp; StringBuilder output <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=</span> Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output.<span style="color: #0000FF;">AppendLine</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span> output.<span style="color: #0000FF;">ToString</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;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/clipexe-and-the-missing-pasteexe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NativeConsole.cs &#8211; Run console apps and get their output</title>
		<link>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/</link>
		<comments>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 18:53:18 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=649</guid>
		<description><![CDATA[To those of you who are not software developers: feel free to skip this post A while ago I wrote a little class for calling console apps from a .Net application, and I&#8217;ve been using it in several of my apps (most notably in PoshConsole) and it works great, but since the only place I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[	<p>To those of you who are not software developers: feel free to skip this post  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>A while ago I wrote a little class for calling console apps from a .Net application, and I&#8217;ve been using it in several of my apps (most notably in <a href="http://CodePlex.com/PoshConsole">PoshConsole</a>) and it works great, but since the only place I&#8217;ve really published it is in PoshConsole, I thought I&#8217;d write it up here, and share it with you &#8230;</p>

	<p>Basically, it&#8217;s a slick invisible event-based wrapper around the Windows native console.  What I mean is, it calls AllocConsole when it&#8217;s instantiated to create a native console, and hides the console window so it doesn&#8217;t show up. This allows you to run any console app you need to from within your app without having it popup a black window  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  Note this doesn&#8217;t let you run graphical consoles like <span class="caps">EDIT</span>.<span class="caps">COM</span>, but it can handle interactive apps like cmd.exe, batch files, or ftp.exe).  All you really have to do is create one of my NativeConsole objects, handle its WriteOutputLine and WriteErrorLine events &#8230; and use its WriteInput method to send input or commands to the console app.</p>

	<p>You can check out how it works in my WPF-based PoshConsole, and you can get the latest version of it from that project as well (it&#8217;s in \trunk\Huddled\Interop\NativeConsole.cs) but for now, here&#8217;s the single file source code, with a more liberal set of licenses than I allow for PoshConsole.<span id="more-649"></span></p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Copyright (c) 2008 Joel Bennett</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Permission is hereby granted, free of charge, to any person obtaining a copy </span><br />
<span style="color: #008080; font-style: italic;">// of this software and associated documentation files (the &quot;Software&quot;), to deal</span><br />
<span style="color: #008080; font-style: italic;">// in the Software without restriction, including without limitation the rights </span><br />
<span style="color: #008080; font-style: italic;">// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell </span><br />
<span style="color: #008080; font-style: italic;">// copies of the Software, and to permit persons to whom the Software is </span><br />
<span style="color: #008080; font-style: italic;">// furnished to do so, subject to the following conditions:</span><br />
<br />
<span style="color: #008080; font-style: italic;">// The above copyright notice and this permission notice shall be included in </span><br />
<span style="color: #008080; font-style: italic;">// all copies or substantial portions of the Software.</span><br />
<br />
<span style="color: #008080; font-style: italic;">// THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR </span><br />
<span style="color: #008080; font-style: italic;">// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, </span><br />
<span style="color: #008080; font-style: italic;">// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE </span><br />
<span style="color: #008080; font-style: italic;">// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER </span><br />
<span style="color: #008080; font-style: italic;">// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span><br />
<span style="color: #008080; font-style: italic;">// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE </span><br />
<span style="color: #008080; font-style: italic;">// SOFTWARE.</span><br />
<br />
<span style="color: #008080; font-style: italic;">// *****************************************************************************</span><br />
<span style="color: #008080; font-style: italic;">// NOTE: YOU MAY *ALSO* DISTRIBUTE THIS FILE UNDER ANY OF THE FOLLOWING LICENSES:</span><br />
<span style="color: #008080; font-style: italic;">// BSD: &nbsp; http://opensource.org/licenses/bsd-license.php</span><br />
<span style="color: #008080; font-style: italic;">// MIT: &nbsp; http://opensource.org/licenses/mit-license.html</span><br />
<span style="color: #008080; font-style: italic;">// Ms-PL: http://opensource.org/licenses/ms-pl.html</span><br />
<span style="color: #008080; font-style: italic;">// GPL 2: http://opensource.org/licenses/gpl-2.0.php</span><br />
<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.Diagnostics</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.Threading</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 />
<br />
<span style="color: #0600FF;">namespace</span> Huddled.<span style="color: #0000FF;">Interop</span><br />
<span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// A wrapper around AllocConsole, with some nice eventing to handle</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NativeConsole <span style="color: #008000;">:</span> IDisposable<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</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;">/// The API/Interop/PInvoke methods for the NativeConsole </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> NativeMethods<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Fields (5)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> UInt32 DUPLICATE_SAME_ACCESS <span style="color: #008000;">=</span> 0x00000002<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LWA_ALPHA <span style="color: #008000;">=</span> 0x2<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LWA_COLORKEY <span style="color: #008000;">=</span> 0x1<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> WS_EX_LAYERED <span style="color: #008000;">=</span> 0x80000<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> WS_EX_TRANSPARENT <span style="color: #008000;">=</span> 0x00000020<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Enums (3)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> ShowState <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SW_HIDE <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// and lots of others</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> GwlIndex <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">12</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">16</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExStyle <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> StdHandle <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard input device</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INPUT_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">10</span>, <span style="color: #008080; font-style: italic;">//(DWORD)-10 &nbsp; &nbsp;The standard input device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard output device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OUTPUT_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">11</span>, <span style="color: #008080; font-style: italic;">//(DWORD)-11 &nbsp; The standard output device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard error device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ERROR_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">12</span> <span style="color: #008080; font-style: italic;">//(DWORD)-12 &nbsp;The standard error device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Methods (13)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;Public Methods (13)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> AllocConsole<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: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> CloseHandle<span style="color: #000000;">&#40;</span>IntPtr hHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> CreatePipe<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> IntPtr hReadPipe, <span style="color: #0600FF;">out</span> IntPtr hWritePipe, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">ref</span> SECURITY_ATTRIBUTES lpPipeAttributes, <span style="color: #FF0000;">uint</span> nSize<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> DuplicateHandle<span style="color: #000000;">&#40;</span>IntPtr hSourceProcessHandle,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntPtr hSourceHandle, IntPtr hTargetProcessHandle, <span style="color: #0600FF;">out</span> IntPtr lpTargetHandle,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">uint</span> dwDesiredAccess, <span style="color: #FF0000;">bool</span> bInheritHandle, <span style="color: #FF0000;">uint</span> dwOptions<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, ExactSpelling <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> FreeConsole<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: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> IntPtr GetConsoleWindow<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: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> GetWindowLong<span style="color: #000000;">&#40;</span>IntPtr hWnd, GwlIndex nIndex<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> ReadFile<span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr hFile, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// handle to file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> lpBuffer, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// data buffer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> nNumberOfBytesToRead, &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// number of bytes to read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> lpNumberOfBytesRead, &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// number of bytes read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr overlapped &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// overlapped buffer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> SetLayeredWindowAttributes<span style="color: #000000;">&#40;</span>IntPtr hwnd, <span style="color: #FF0000;">int</span> crKey, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">byte</span> bAlpha, <span style="color: #FF0000;">int</span> dwFlags<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> SetStdHandle<span style="color: #000000;">&#40;</span>StdHandle nStdHandle, IntPtr hHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> SetWindowLong<span style="color: #000000;">&#40;</span>IntPtr hWnd, GwlIndex nIndex, <span style="color: #FF0000;">int</span> dwNewLong<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &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; &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> ShowWindow<span style="color: #000000;">&#40;</span>IntPtr hWnd, ShowState nCmdShow<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</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: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> WriteFile<span style="color: #000000;">&#40;</span>IntPtr hFile, <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> numBytesToWrite, <span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> numBytesWritten, IntPtr lpOverlapped<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> SECURITY_ATTRIBUTES<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> nLength<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> IntPtr lpSecurityDescriptor<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</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; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> bInheritHandle<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 />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OutputEventArgs<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Text<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region Delegate and Events (3)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Delegate, I changed this to be compatible with normal wpf/forms events</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> OutputDelegate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> source, OutputEventArgs args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Events (2)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> OutputDelegate WriteErrorLine<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> OutputDelegate WriteOutputLine<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region &nbsp;Private Fields (14)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Track whether Dispose has been called.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> disposed <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// A nice handle to our console window</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr handle<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// And our process</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Process</span> process<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Our two threads</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> Thread outputThread, errorThread<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// and the original handles to the console</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr stdOutRead, stdOutWrite, stdInRead, stdInWrite, stdErrRead, stdErrWrite<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// and the copy handles ...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr stdOutReadCopy, stdInWriteCopy, stdErrReadCopy<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region &nbsp;Constructors and Destructors (2)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Initializes a new instance of the &lt;see cref=&quot;NativeConsole&quot;/&gt; class.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> NativeConsole<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: #008080; font-style: italic;">// Make ourselves a nice console</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">AllocConsole</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: #008080; font-style: italic;">// hide the window ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;handle <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">GetConsoleWindow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">ShowWindow</span><span style="color: #000000;">&#40;</span>handle, NativeMethods.<span style="color: #0000FF;">ShowState</span>.<span style="color: #0000FF;">SW_HIDE</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;">//NativeMethods.SetWindowLong(handle, NativeMethods.GwlIndex.ExStyle, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// (NativeMethods.GetWindowLong(handle, NativeMethods.GwlIndex.ExStyle) |</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// NativeMethods.WS_EX_LAYERED | NativeMethods.WS_EX_TRANSPARENT));</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.SetLayeredWindowAttributes(handle, 0, 0, NativeMethods.LWA_ALPHA);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Process</span>.<span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">SECURITY_ATTRIBUTES</span> saAttr<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Set the bInheritHandle flag so pipe handles are inherited.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">nLength</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>NativeMethods.<span style="color: #0000FF;">SECURITY_ATTRIBUTES</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">bInheritHandle</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">lpSecurityDescriptor</span> <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The steps for redirecting STDOUT:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDOUT for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDOUT of our process to be WRITE handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the read handle, and...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdOutRead, <span style="color: #0600FF;">out</span> stdOutWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the STDOUT pipe&quot;</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: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">OUTPUT_HANDLE</span>, stdOutWrite<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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect STDOUT!&quot;</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;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdOutRead, process.<span style="color: #0000FF;">Handle</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> stdOutReadCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate STDOUT Handle&quot;</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;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutRead<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;">// For the output handles we need a thread to read them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #000000;">&#40;</span>OutputThread<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread.<span style="color: #0000FF;">SetApartmentState</span><span style="color: #000000;">&#40;</span>ApartmentState.<span style="color: #0000FF;">STA</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread.<span style="color: #0000FF;">Start</span><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: #008080; font-style: italic;">// The steps for redirecting STDERR are the same:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDERR for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDERR of our process to be WRITE handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the read handle and </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdErrRead, <span style="color: #0600FF;">out</span> stdErrWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the STDERR pipe&quot;</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: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">ERROR_HANDLE</span>, stdErrWrite<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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect STDERR!&quot;</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;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdErrRead, process.<span style="color: #0000FF;">Handle</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> stdErrReadCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate STDERR Handle&quot;</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;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrRead<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;">// For the output handles we need a thread to read them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #000000;">&#40;</span>ErrorThread<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread.<span style="color: #0000FF;">SetApartmentState</span><span style="color: #000000;">&#40;</span>ApartmentState.<span style="color: #0000FF;">STA</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread.<span style="color: #0000FF;">Start</span><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: #008080; font-style: italic;">// The steps for redirecting STDIN:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDIN for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDIN of our process to be READ handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the WRITE handle and </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable WRITE handle.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdInRead, <span style="color: #0600FF;">out</span> stdInWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the StdIn pipe&quot;</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: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">INPUT_HANDLE</span>, stdInRead<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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect StdIn!&quot;</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;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdInWrite, process.<span style="color: #0000FF;">Handle</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">out</span> stdInWriteCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</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; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate StdIn Handle&quot;</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;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInWrite<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;">/// &lt;summary&gt;Releases unmanaged resources and performs other cleanup operations </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// before the &lt;see cref=&quot;Console&quot;/&gt; is reclaimed by garbage collection.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Use C# destructor syntax for finalization code.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// This destructor will run only if the Dispose method does not get called.</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;NOTE: Do not provide destructors in types derived from this class.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; ~NativeConsole<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: #008080; font-style: italic;">// Instead of cleaning up in BOTH Dispose() and here ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// We call Dispose(false) for the best readability and maintainability.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</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;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region Methods (5)</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// &nbsp;Public Methods (2)</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;">/// Implement IDisposable</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Performs application-defined tasks associated with </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// freeing, releasing, or resetting unmanaged resources.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<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: #008080; font-style: italic;">// This object will be cleaned up by the Dispose method.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Therefore, we call GC.SupressFinalize to tell the runtime </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// that we dont' need to be finalized (we would clean up twice)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</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;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Writes the input.</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;param name=&quot;input&quot;&gt;The input.&lt;/param&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WriteInput<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> input<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bytes <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span>.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> written<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> hresult <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdInWriteCopy, bytes, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bytes.<span style="color: #0000FF;">Length</span>, <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><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>hresult <span style="color: #008000;">!=</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; <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> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error Writing to StdIn, HRESULT: &quot;</span> <span style="color: #008000;">+</span> hresult.<span style="color: #0000FF;">ToString</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 />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// &nbsp;Private Methods (3)</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;">/// Handles actual cleanup actions, under two different scenarios</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;param name=&quot;disposing&quot;&gt;if set to &lt;c&gt;true&lt;/c&gt; we've been called directly or </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// indirectly by user code and can clean up both managed and unmanaged resources.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Otherwise it's been called from the destructor/finalizer and we can't</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// reference other managed objects (they might already be disposed).</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">///&lt;/param&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposing<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: #008080; font-style: italic;">// Check to see if Dispose has already been called.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>disposed<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// // If disposing equals true, dispose all managed resources ALSO.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposing<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorThread.<span style="color: #0000FF;">Abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputThread.<span style="color: #0000FF;">Abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//WriteInput(&quot;\n&quot;);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bytes <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span>.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">char</span><span style="color: #000000;">&#41;</span><span style="color: #FF0000;">26</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">int</span> written<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdErrWrite, bytes, bytes.<span style="color: #0000FF;">Length</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdOutWrite, bytes, bytes.<span style="color: #0000FF;">Length</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//errorThread.Join();</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//outputThread.Join();</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Clean up UnManaged resources</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// If disposing is false, only the following code is executed.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">FreeConsole</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdOutWrite);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdOutReadCopy);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdErrWrite);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdErrReadCopy);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInWriteCopy<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInRead<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; &nbsp; <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<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;Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">StackTrace</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;disposed <span style="color: #008000;">=</span> true<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;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The ErrorThread ThreadStart delegate</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ErrorThread<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: #FF0000;">int</span> BytesRead<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> BufBytes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// consider wrapping this in a System.IO.FileStream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">ReadFile</span><span style="color: #000000;">&#40;</span>stdErrReadCopy, BufBytes, <span style="color: #FF0000;">4096</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> BytesRead, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</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;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>WriteErrorLine <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteErrorLine<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputEventArgs <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text <span style="color: #008000;">=</span> UTF8Encoding.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>BufBytes, <span style="color: #FF0000;">0</span>, BytesRead<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</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 />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ThreadAbortException<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">finally</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrWrite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrReadCopy<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 />
<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 OutputThread ThreadStart delegate</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> OutputThread<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: #FF0000;">int</span> BytesRead<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> BufBytes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// consider wrapping this in a System.IO.FileStream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">ReadFile</span><span style="color: #000000;">&#40;</span>stdOutReadCopy, BufBytes, <span style="color: #FF0000;">4096</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> BytesRead, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</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;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>WriteOutputLine <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteOutputLine<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputEventArgs <span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text <span style="color: #008000;">=</span> UTF8Encoding.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>BufBytes, <span style="color: #FF0000;">0</span>, BytesRead<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</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 />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ThreadAbortException<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">finally</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutWrite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutReadCopy<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; &nbsp; <span style="color: #008080;">#endregion</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Visual C++ 2008 Feature Pack Released</title>
		<link>http://huddledmasses.org/visual-c-2008-feature-pack-released/</link>
		<comments>http://huddledmasses.org/visual-c-2008-feature-pack-released/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 21:24:46 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Feature Pack]]></category>
		<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/visual-c-2008-feature-pack-released/</guid>
		<description><![CDATA[Well, Microsoft has released its free Visual C++ 2008 Feature Pack for download &#8212; including a major update to MFC and an implementation of TR1 which includes smart pointers, regular expressions, containers and better random number generators. The update to MFC is a fairly major overhaul, and includes components which provide the L&#038;F(Look And Feel) [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, Microsoft has released its free <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=D466226B-8DAB-445F-A7B4-448B326C48E7&#38;amp;amp;displaylang=en">Visual C++ 2008 Feature Pack</a> for download &#8212; including a major update to <a href="http://msdn2.microsoft.com/en-us/library/bb982354.aspx" title="Microsoft Foundation Class">MFC</a> and an implementation of <a href="http://msdn2.microsoft.com/en-us/library/bb982198.aspx" title="Technical Report 1">TR1</a> which includes smart pointers, regular expressions, containers and better random number generators.</p>

	<p>The update to <span class="caps">MFC</span> is a fairly major overhaul, and includes components which provide the L&#038;F(Look And Feel) of IE, Office 2007 (including the ribbon bar), Visual Studio (including Visual Studio docking and autohide windows), with support for Vista themes, and on-the-fly customization of toolbars and menus, according to <a href="http://blogs.msdn.com/somasegar/archive/2008/04/07/visual-c-2008-feature-pack-shipped.aspx">Somasegar</a>.  The list goes on, and it honestly left me feeling conflicted &#8212;  I mean, now that <span class="caps">MFC</span> has joined us in the 21st century, I&#8217;m almost tempted to actually use it &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/visual-c-2008-feature-pack-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

