<?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>Dirty Motherfucking Blog &#187; win32</title>
	<atom:link href="http://dirty-motherfucker.org/blog/tag/win32/feed/" rel="self" type="application/rss+xml" />
	<link>http://dirty-motherfucker.org/blog</link>
	<description>All kinds of shit</description>
	<lastBuildDate>Wed, 04 Jan 2012 15:38:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19620</generator>
		<item>
		<title>Output to the parent console in Windows application</title>
		<link>http://dirty-motherfucker.org/blog/2009/03/10/output-the-parent-console-in-windows-application/</link>
		<comments>http://dirty-motherfucker.org/blog/2009/03/10/output-the-parent-console-in-windows-application/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 00:27:42 +0000</pubDate>
		<dc:creator>gencha</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[win32]]></category>

		<guid isPermaLink="false">http://www.dirty-motherfucker.org/blog/?p=248</guid>
		<description><![CDATA[So I looked into boost.program_options today to parse command line arguments for my Windows Forms application. Which basically works great. Except for the little part to output the &#8211;help output to the console. Now, obviously when you start your application through Explorer you won&#8217;t have a console. And that is fine. I was only interested [...]]]></description>
			<content:encoded><![CDATA[<p>So I looked into boost.program_options today to parse command line arguments for my Windows Forms application. Which basically works great. Except for the little part to output the &#8211;help output to the console. Now, obviously when you start your application through Explorer you won&#8217;t have a console. And that is fine. I was only interested in the case when you start the application through a console window. Additionally it would also have to work with Unicode of course :P<br />
So after looking around the web for a while I finally managed to put it all together. So here it is:</p>
<pre class="brush: cpp; title: ; notranslate">
BOOL consoleAttached = AttachConsole( ATTACH_PARENT_PROCESS );

boost::program_options::options_description desc( &quot;Allowed options&quot; );
desc.add_options()
	( &quot;help&quot;, &quot;Show this message&quot; )
	( &quot;width&quot;, &quot;Backbuffer width&quot; )
	( &quot;height&quot;, &quot;Backbuffer height&quot; )
	;

wstring commandLine = GetCommandLine();
int     argc = 0;
LPWSTR* argv = 0;
argv = CommandLineToArgvW( commandLine.c_str(), &amp;argc );

boost::program_options::variables_map vm;
boost::program_options::store( boost::program_options::parse_command_line( argc, argv, desc ), vm );
boost::program_options::notify( vm );

LocalFree( argv );

if( vm.count( &quot;help&quot; ) ) {
	if( consoleAttached ) {
		HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
		if( INVALID_HANDLE_VALUE == hStdOut ) {
			return 1;
		}
		DWORD dwCharsWritten;
		std::stringstream desc_strings;
		desc_strings &lt;&lt; desc;
		std::string desc_string = desc_strings.str();

		WriteConsoleA( hStdOut, desc_string.c_str(), desc_string.length(), &amp;dwCharsWritten, NULL );
		FreeConsole();
	}
	return 1;
}
</pre>
<p>Sadly after the application finishes the prompt doesn&#8217;t instantly return. Maybe I&#8217;ll look into that some day&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://dirty-motherfucker.org/blog/2009/03/10/output-the-parent-console-in-windows-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

