<?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>InsaneDevelopers &#187; console</title>
	<atom:link href="http://insanedevelopers.net/tag/console/feed/" rel="self" type="application/rss+xml" />
	<link>http://insanedevelopers.net</link>
	<description>My personal trash bin</description>
	<lastBuildDate>Mon, 29 Mar 2010 19:55:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a console from a windows subsystem program</title>
		<link>http://insanedevelopers.net/2009/03/13/creating-a-console-from-a-windows-subsystem-program/</link>
		<comments>http://insanedevelopers.net/2009/03/13/creating-a-console-from-a-windows-subsystem-program/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 23:51:43 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[allocconsole]]></category>
		<category><![CDATA[console]]></category>

		<guid isPermaLink="false">http://insanedevelopers.net/?p=9</guid>
		<description><![CDATA[Sometimes, printing messages and dumping variables can be very handy for debugging purposes, but most of today's programs do not need and in fact do not open a console window.
The following is a way a console can be opened from a windows subsystem application.
// Open the console window
AllocConsole();

// Assign the stdin/stdout/stderr streams to the newly [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, printing messages and dumping variables can be very handy for debugging purposes, but most of today's programs do not need and in fact do not open a console window.</p>
<p>The following is a way a console can be opened from a windows subsystem application.</p>
<pre>// Open the console window
AllocConsole();

// Assign the stdin/stdout/stderr streams to the newly created console
_tfreopen_s(&amp;fpStdIn, _T("CONIN$"), _T("r"), stdin);
_tfreopen_s(&amp;fpStdOut, _T("CONOUT$"), _T("w"), stdout);
_tfreopen_s(&amp;fpStdErr, _T("CONOUT$"), _T("w"), stderr);</pre>
<p>Once opened and initialized, the console can be used just like you do in a normal text-based application. Note that you need to explicitly release both the console and the streams you opened.</p>
<pre>// Release the console
FreeConsole();

// Release the streams
fclose(stdin);
fclose(stdout);
fclose(stderr);</pre>
<p>It's a good idea to add this code inside an #ifdef statement, in order to remove this code (as well as the printf functions) from the release version.</p>
<p>Source code: <a href="http://www.InsaneDevelopers.net/sources/CreatingAConsole.zip">http://www.InsaneDevelopers.net/sources/CreatingAConsole.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://insanedevelopers.net/2009/03/13/creating-a-console-from-a-windows-subsystem-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling CTRL+C in your console application</title>
		<link>http://insanedevelopers.net/2009/03/13/handling-ctrlc-in-your-console-application/</link>
		<comments>http://insanedevelopers.net/2009/03/13/handling-ctrlc-in-your-console-application/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 23:50:37 +0000</pubDate>
		<dc:creator>Alessandro</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[signals]]></category>

		<guid isPermaLink="false">http://insanedevelopers.net/?p=7</guid>
		<description><![CDATA[First of all, to make sure we can receive this event, the ENABLE_PROCESSED_INPUT console mode must be enabled using SetConsoleMode; without this, the CTRL+C signal would be sent as a keystroke inside the input buffer.
SetConsoleMode(hdlStdInput, ENABLE_ECHO_INPUT &#124; ENABLE_INSERT_MODE &#124; ENABLE_LINE_INPUT &#124; ENABLE_MOUSE_INPUT &#124; ENABLE_PROCESSED_INPUT &#124; ENABLE_QUICK_EDIT_MODE);
We can now register our HandlerRoutine function using SetConsoleCtrlHandler
BOOL WINAPI [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, to make sure we can receive this event, the ENABLE_PROCESSED_INPUT console mode must be enabled using SetConsoleMode; without this, the CTRL+C signal would be sent as a keystroke inside the input buffer.</p>
<pre>SetConsoleMode(hdlStdInput, ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE);</pre>
<p>We can now register our HandlerRoutine function using SetConsoleCtrlHandler</p>
<pre>BOOL WINAPI blCtrlCHandler(DWORD dwCtrlType) { ... }
...
SetConsoleCtrlHandler(blCtrlCHandler, TRUE);</pre>
<p>The handler function can be also handy for cleanup code, since it does not receives only the CTRL+C signal, but other events as well. Checking the dwCtrlType variable allows you to detect CTRL+BREAK and system events like logoff, shutdown, and the program's termination through the close button.</p>
<p>If you don't need the CTRL+C signal, and you want to mask it, it is possible to register a null function pointer that will prevent the default handler from handling the signal.</p>
<pre>SetConsoleCtrlHandler(NULL, TRUE);</pre>
<p>Source code: <a href="http://www.insanedevelopers.net/sources/CTRL-CHandling.zip">http://www.insanedevelopers.net/sources/CTRL-CHandling.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://insanedevelopers.net/2009/03/13/handling-ctrlc-in-your-console-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
