<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Console Output from a WinForms Application</title>
	<atom:link href="http://www.csharp411.com/console-output-from-winforms-application/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharp411.com/console-output-from-winforms-application/</link>
	<description>C# Information, Code, Tips and News</description>
	<lastBuildDate>Sat, 24 Jul 2010 01:26:51 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: AGreenhill</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5587</link>
		<dc:creator>AGreenhill</dc:creator>
		<pubDate>Fri, 16 Jul 2010 18:23:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5587</guid>
		<description>Perfect - just what I needed! (I don&#039;t mind the dumping to txt file issue)</description>
		<content:encoded><![CDATA[<p>Perfect &#8211; just what I needed! (I don't mind the dumping to txt file issue)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5392</link>
		<dc:creator>Ray</dc:creator>
		<pubDate>Thu, 25 Feb 2010 09:53:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5392</guid>
		<description>Just an FYI, this may not be a huge concern, I&#039;ve noticed that when ran from a batch file, this solution works well which was the point of me adding cmd line functionality, so I&#039;m happy, although obviously I still want &quot;perfect&quot; :)</description>
		<content:encoded><![CDATA[<p>Just an FYI, this may not be a huge concern, I've noticed that when ran from a batch file, this solution works well which was the point of me adding cmd line functionality, so I'm happy, although obviously I still want "perfect" <img src='http://www.csharp411.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shmulik</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5388</link>
		<dc:creator>Shmulik</dc:creator>
		<pubDate>Wed, 17 Feb 2010 19:22:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5388</guid>
		<description>Hi all,
I have developed a C# Windows application and I need to add command line support for it. Using the AttachConsole method I succeeded to output messages from the application to the console window that launched the application.
The problem is that the console also prints the user prompt (&quot;C:\[path]&gt;&quot;) before the apllication messages are printed.
Is there any way to avoid printing the user prompt? I just want to output my messages from the Windows application directly to the cursor location.</description>
		<content:encoded><![CDATA[<p>Hi all,<br />
I have developed a C# Windows application and I need to add command line support for it. Using the AttachConsole method I succeeded to output messages from the application to the console window that launched the application.<br />
The problem is that the console also prints the user prompt ("C:\[path]&gt;") before the apllication messages are printed.<br />
Is there any way to avoid printing the user prompt? I just want to output my messages from the Windows application directly to the cursor location.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: spottedcoin</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5370</link>
		<dc:creator>spottedcoin</dc:creator>
		<pubDate>Thu, 28 Jan 2010 20:33:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5370</guid>
		<description>If you attach the Standard input with the output and error then you won&#039;t have to use the sendkeys command and it will fix the issue with not grabbing the attention of the entire console. I edited the code posted above to the following:
[DllImport(&quot;kernel32.dll&quot;)]
        static extern bool AttachConsole(UInt32 dwProcessId);
        [DllImport(&quot;kernel32.dll&quot;)]
        static extern bool FreeConsole();
        [DllImport(&quot;kernel32.dll&quot;)]
        static extern bool AllocConsole();
        [DllImport(&quot;kernel32.dll&quot;)]
        private static extern bool GetFileInformationByHandle(SafeFileHandle hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);
        [DllImport(&quot;kernel32.dll&quot;)]
        private static extern SafeFileHandle GetStdHandle(UInt32 nStdHandle);
        [DllImport(&quot;kernel32.dll&quot;)]
        private static extern bool SetStdHandle(UInt32 nStdHandle, SafeFileHandle hHandle);
        [DllImport(&quot;kernel32.dll&quot;)]
        private static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeFileHandle hSourceHandle, IntPtr hTargetProcessHandle,
        out SafeFileHandle lpTargetHandle, UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwOptions);

        private const UInt32 ATTACH_PARENT_PROCESS = 0xFFFFFFFF;
        private const UInt32 STD_INPUT_HANDLE = 0xFFFFFFF6;
        private const UInt32 STD_OUTPUT_HANDLE = 0xFFFFFFF5;
        private const UInt32 STD_ERROR_HANDLE = 0xFFFFFFF4;
        private const UInt32 DUPLICATE_SAME_ACCESS = 2;

        static SafeFileHandle hStdIn, hStdOut, hStdErr, hStdOutDup, hStdErrDup, hStdInDup;
        static BY_HANDLE_FILE_INFORMATION bhfi;

        private struct BY_HANDLE_FILE_INFORMATION
        {
            public UInt32 FileAttributes;
            public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
            public UInt32 VolumeSerialNumber;
            public UInt32 FileSizeHigh;
            public UInt32 FileSizeLow;
            public UInt32 NumberOfLinks;
            public UInt32 FileIndexHigh;
            public UInt32 FileIndexLow;
        }

private static void InitConsoleHandles()
        {
            hStdIn = GetStdHandle(STD_INPUT_HANDLE);
            hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
            hStdErr = GetStdHandle(STD_ERROR_HANDLE);

            // Get current process handle
            IntPtr hProcess = Process.GetCurrentProcess().Handle;

            // Duplicate Stdout handle to save initial value
            DuplicateHandle(hProcess, hStdIn, hProcess, out hStdInDup, 0, true, DUPLICATE_SAME_ACCESS);

            // Duplicate Stdout handle to save initial value
            DuplicateHandle(hProcess, hStdOut, hProcess, out hStdOutDup, 0, true, DUPLICATE_SAME_ACCESS);

            // Duplicate Stderr handle to save initial value
            DuplicateHandle(hProcess, hStdErr, hProcess, out hStdErrDup, 0, true, DUPLICATE_SAME_ACCESS);

            // Attach to console window – this may modify the standard handles
            AttachConsole(ATTACH_PARENT_PROCESS);
            //AllocConsole();

            // Adjust the standard handles
            if (GetFileInformationByHandle(GetStdHandle(STD_INPUT_HANDLE), out bhfi))
            {
                SetStdHandle(STD_INPUT_HANDLE, hStdInDup);
            }
            else
            {
                SetStdHandle(STD_INPUT_HANDLE, hStdIn);
            }

            if (GetFileInformationByHandle(GetStdHandle(STD_OUTPUT_HANDLE), out bhfi))
            {
                SetStdHandle(STD_OUTPUT_HANDLE, hStdOutDup);
            }
            else
            {
                SetStdHandle(STD_OUTPUT_HANDLE, hStdOut);
            }

            if (GetFileInformationByHandle(GetStdHandle(STD_ERROR_HANDLE), out bhfi))
            {
                SetStdHandle(STD_ERROR_HANDLE, hStdErrDup);
            }
            else
            {
                SetStdHandle(STD_ERROR_HANDLE, hStdErr);
            }
        }

        private static void ReleaseConsoleHandles()
        {
            FreeConsole();
        }</description>
		<content:encoded><![CDATA[<p>If you attach the Standard input with the output and error then you won't have to use the sendkeys command and it will fix the issue with not grabbing the attention of the entire console. I edited the code posted above to the following:<br />
[DllImport("kernel32.dll")]<br />
        static extern bool AttachConsole(UInt32 dwProcessId);<br />
        [DllImport("kernel32.dll")]<br />
        static extern bool FreeConsole();<br />
        [DllImport("kernel32.dll")]<br />
        static extern bool AllocConsole();<br />
        [DllImport("kernel32.dll")]<br />
        private static extern bool GetFileInformationByHandle(SafeFileHandle hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);<br />
        [DllImport("kernel32.dll")]<br />
        private static extern SafeFileHandle GetStdHandle(UInt32 nStdHandle);<br />
        [DllImport("kernel32.dll")]<br />
        private static extern bool SetStdHandle(UInt32 nStdHandle, SafeFileHandle hHandle);<br />
        [DllImport("kernel32.dll")]<br />
        private static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeFileHandle hSourceHandle, IntPtr hTargetProcessHandle,<br />
        out SafeFileHandle lpTargetHandle, UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwOptions);</p>
<p>        private const UInt32 ATTACH_PARENT_PROCESS = 0xFFFFFFFF;<br />
        private const UInt32 STD_INPUT_HANDLE = 0xFFFFFFF6;<br />
        private const UInt32 STD_OUTPUT_HANDLE = 0xFFFFFFF5;<br />
        private const UInt32 STD_ERROR_HANDLE = 0xFFFFFFF4;<br />
        private const UInt32 DUPLICATE_SAME_ACCESS = 2;</p>
<p>        static SafeFileHandle hStdIn, hStdOut, hStdErr, hStdOutDup, hStdErrDup, hStdInDup;<br />
        static BY_HANDLE_FILE_INFORMATION bhfi;</p>
<p>        private struct BY_HANDLE_FILE_INFORMATION<br />
        {<br />
            public UInt32 FileAttributes;<br />
            public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;<br />
            public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;<br />
            public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;<br />
            public UInt32 VolumeSerialNumber;<br />
            public UInt32 FileSizeHigh;<br />
            public UInt32 FileSizeLow;<br />
            public UInt32 NumberOfLinks;<br />
            public UInt32 FileIndexHigh;<br />
            public UInt32 FileIndexLow;<br />
        }</p>
<p>private static void InitConsoleHandles()<br />
        {<br />
            hStdIn = GetStdHandle(STD_INPUT_HANDLE);<br />
            hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);<br />
            hStdErr = GetStdHandle(STD_ERROR_HANDLE);</p>
<p>            // Get current process handle<br />
            IntPtr hProcess = Process.GetCurrentProcess().Handle;</p>
<p>            // Duplicate Stdout handle to save initial value<br />
            DuplicateHandle(hProcess, hStdIn, hProcess, out hStdInDup, 0, true, DUPLICATE_SAME_ACCESS);</p>
<p>            // Duplicate Stdout handle to save initial value<br />
            DuplicateHandle(hProcess, hStdOut, hProcess, out hStdOutDup, 0, true, DUPLICATE_SAME_ACCESS);</p>
<p>            // Duplicate Stderr handle to save initial value<br />
            DuplicateHandle(hProcess, hStdErr, hProcess, out hStdErrDup, 0, true, DUPLICATE_SAME_ACCESS);</p>
<p>            // Attach to console window – this may modify the standard handles<br />
            AttachConsole(ATTACH_PARENT_PROCESS);<br />
            //AllocConsole();</p>
<p>            // Adjust the standard handles<br />
            if (GetFileInformationByHandle(GetStdHandle(STD_INPUT_HANDLE), out bhfi))<br />
            {<br />
                SetStdHandle(STD_INPUT_HANDLE, hStdInDup);<br />
            }<br />
            else<br />
            {<br />
                SetStdHandle(STD_INPUT_HANDLE, hStdIn);<br />
            }</p>
<p>            if (GetFileInformationByHandle(GetStdHandle(STD_OUTPUT_HANDLE), out bhfi))<br />
            {<br />
                SetStdHandle(STD_OUTPUT_HANDLE, hStdOutDup);<br />
            }<br />
            else<br />
            {<br />
                SetStdHandle(STD_OUTPUT_HANDLE, hStdOut);<br />
            }</p>
<p>            if (GetFileInformationByHandle(GetStdHandle(STD_ERROR_HANDLE), out bhfi))<br />
            {<br />
                SetStdHandle(STD_ERROR_HANDLE, hStdErrDup);<br />
            }<br />
            else<br />
            {<br />
                SetStdHandle(STD_ERROR_HANDLE, hStdErr);<br />
            }<br />
        }</p>
<p>        private static void ReleaseConsoleHandles()<br />
        {<br />
            FreeConsole();<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rocky</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5355</link>
		<dc:creator>Rocky</dc:creator>
		<pubDate>Thu, 07 Jan 2010 10:29:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5355</guid>
		<description>Jonathan,

You are simply a genius da!!!!</description>
		<content:encoded><![CDATA[<p>Jonathan,</p>
<p>You are simply a genius da!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: R. Whitfield</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5338</link>
		<dc:creator>R. Whitfield</dc:creator>
		<pubDate>Mon, 07 Dec 2009 21:36:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5338</guid>
		<description>This seems to work well for my needs.  Greatly appreciated.</description>
		<content:encoded><![CDATA[<p>This seems to work well for my needs.  Greatly appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Counihan</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5325</link>
		<dc:creator>Jonathan Counihan</dc:creator>
		<pubDate>Wed, 04 Nov 2009 06:30:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5325</guid>
		<description>Hi Crayon, Thanks, I tried putting GetConsoleTitle after the AttachConsole call, but no luck so far, still the same error.

I&#039;ll keep looking for an answer and post it here if I find one.

Cheers.</description>
		<content:encoded><![CDATA[<p>Hi Crayon, Thanks, I tried putting GetConsoleTitle after the AttachConsole call, but no luck so far, still the same error.</p>
<p>I'll keep looking for an answer and post it here if I find one.</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Crayon</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5324</link>
		<dc:creator>Crayon</dc:creator>
		<pubDate>Tue, 03 Nov 2009 16:47:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5324</guid>
		<description>Jonathan, in my code I&#039;m calling GetConsoleTitle after 
AttachConsole(ATTACH_PARENT_PROCESS).

For the rest, I can&#039;t really help you. I never programmed in .NET. That&#039;s why the code pasted above is in C/C++.

Good luck...</description>
		<content:encoded><![CDATA[<p>Jonathan, in my code I'm calling GetConsoleTitle after<br />
AttachConsole(ATTACH_PARENT_PROCESS).</p>
<p>For the rest, I can't really help you. I never programmed in .NET. That's why the code pasted above is in C/C++.</p>
<p>Good luck&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Counihan</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5321</link>
		<dc:creator>Jonathan Counihan</dc:creator>
		<pubDate>Tue, 03 Nov 2009 07:24:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5321</guid>
		<description>Thanks Crayon for your post. I have modified what you have posted to fit my situation but unfortunately I can&#039;t seem to get GetConsoleTitle to work! The error code I get is 183, &quot;Cannot create a file when that file already exists.&quot; and I have no idea why! The string builder comes back empty.

I have followed this post http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=109 to use the GetConsoleTitle, but I can&#039;t seem to get it to work.

My code for the main function is currently this:

StringBuilder sb = new StringBuilder(1000000);
UInt32 buffSize = (UInt32)sb.Capacity;
UInt32 titleLen = GetConsoleTitle(sb, buffSize);

if (titleLen &gt; 0 &amp;&amp;
  (sb.ToString().Substring(sb.Length - 7, 7) == &quot;cmd.exe&quot;))
{
  //LaunchedByCmdexe = true;
}

if (args.Length == 1 &amp;&amp; args[0] == &quot;-help&quot;)
{
  InitConsoleHandles();

  Console.WriteLine(&quot;MyApp help is this&quot;);

  ReleaseConsoleHandles();
}
else
{
  Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Any ideas? Thanks.</description>
		<content:encoded><![CDATA[<p>Thanks Crayon for your post. I have modified what you have posted to fit my situation but unfortunately I can't seem to get GetConsoleTitle to work! The error code I get is 183, "Cannot create a file when that file already exists." and I have no idea why! The string builder comes back empty.</p>
<p>I have followed this post <a href="http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=109" rel="nofollow">http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=109</a> to use the GetConsoleTitle, but I can't seem to get it to work.</p>
<p>My code for the main function is currently this:</p>
<p>StringBuilder sb = new StringBuilder(1000000);<br />
UInt32 buffSize = (UInt32)sb.Capacity;<br />
UInt32 titleLen = GetConsoleTitle(sb, buffSize);</p>
<p>if (titleLen &gt; 0 &amp;&amp;<br />
  (sb.ToString().Substring(sb.Length &#8211; 7, 7) == "cmd.exe"))<br />
{<br />
  //LaunchedByCmdexe = true;<br />
}</p>
<p>if (args.Length == 1 &amp;&amp; args[0] == "-help")<br />
{<br />
  InitConsoleHandles();</p>
<p>  Console.WriteLine("MyApp help is this");</p>
<p>  ReleaseConsoleHandles();<br />
}<br />
else<br />
{<br />
  Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);<br />
Application.Run(new Form1());<br />
}</p>
<p>Any ideas? Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Crayon</title>
		<link>http://www.csharp411.com/console-output-from-winforms-application/comment-page-1/#comment-5320</link>
		<dc:creator>Crayon</dc:creator>
		<pubDate>Mon, 02 Nov 2009 22:36:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/console-output-from-winforms-application/#comment-5320</guid>
		<description>You might want to add some protection:

char Title[MAX_PATH] = &quot;&quot;;
if(GetConsoleTitle(Title, MAX_PATH) &amp;&amp; strlen(Title) &gt;= 7 &amp;&amp; strcmp(&amp;Title[strlen(Title) - 7], &quot;cmd.exe&quot;) != 0)
{
// Launched by cmd.exe
}</description>
		<content:encoded><![CDATA[<p>You might want to add some protection:</p>
<p>char Title[MAX_PATH] = "";<br />
if(GetConsoleTitle(Title, MAX_PATH) &amp;&amp; strlen(Title) &gt;= 7 &amp;&amp; strcmp(&amp;Title[strlen(Title) - 7], "cmd.exe") != 0)<br />
{<br />
// Launched by cmd.exe<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
