<?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>Fooling Around &#187; Source</title>
	<atom:link href="http://alax.info/blog/category/source/feed" rel="self" type="application/rss+xml" />
	<link>http://alax.info/blog</link>
	<description>// Software Production Line</description>
	<lastBuildDate>Wed, 02 May 2012 15:42:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Enumerating Binary Resource Languages</title>
		<link>http://alax.info/blog/1362</link>
		<comments>http://alax.info/blog/1362#comments</comments>
		<pubDate>Tue, 01 May 2012 17:05:35 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.rc]]></category>
		<category><![CDATA[.res]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[Windows API]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1362</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1362" title="Enumerating Binary Resource Languages"></a>The small application is a goos sample and useful tool in the same time. It enumerates PE binary resources and counts languages used. Why? Normally you want single language of resources, however Visual Studio IDE does not show you languages &#8230;<p class="read-more"><a href="http://alax.info/blog/1362">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1362" title="Enumerating Binary Resource Languages"></a><p>The small application is a goos sample and useful tool in the same time. It enumerates PE binary resources and counts languages used. Why? Normally you want single language of resources, however Visual Studio IDE does not show you languages in a convenient way and it is so easy to make a language mess which does not bite you immediately but might bring you troubles later. Apart from this there was a suspicion that language mess might cause runtime bugs on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648049%28v=vs.85%29.aspx"><code>UpdateResource</code></a> API.</p>
<p>The application gets you a summary of languages used and returns with exit code (checkable using e.g. <code>errorlevel</code>) indicating number of languages.</p>
<p>Output is <code>TSV</code>: <code>LANGID</code>, Language Name, Resource Count:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #e34adc;">C:</span>\<span style="color: #808030;">&gt;</span>ListResourceLanguages<span style="color: #808030;">.</span>exe C<span style="color: #800080;">:</span>\Windows\syswow64\shell32<span style="color: #808030;">.</span>dll
<span style="color: #008c00;">1049</span>    Russian <span style="color: #008c00;">545</span>
<span style="color: #008c00;">1033</span>    English <span style="color: #008c00;">3318</span></pre>
<p>Download links:</p>
<ul>
<li>Visual C++ .NET 2010 source code: <a href="http://alax.info/trac/public/browser/trunk/Utilities/ListResourceLanguages">Trac</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/ListResourceLanguages/">Subversion</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1362/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enabling ATLTRACE output in Release configuration builds</title>
		<link>http://alax.info/blog/1351</link>
		<comments>http://alax.info/blog/1351#comments</comments>
		<pubDate>Sun, 01 Apr 2012 11:40:10 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[OutputDebugString]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1351</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1351" title="Enabling ATLTRACE output in Release configuration builds"></a>The original intent is pretty clear, as MSDN states: In release builds, ATLTRACE2 compiles to (void) 0. As simple as this, but once in a while you are in a situation where release build fails to work for unknown reason &#8230;<p class="read-more"><a href="http://alax.info/blog/1351">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1351" title="Enabling ATLTRACE output in Release configuration builds"></a><p>The original intent is pretty clear, as <a href="http://msdn.microsoft.com/en-us/library/dhxsse89.aspx">MSDN states</a>:</p>
<blockquote><p>In release builds, <strong>ATLTRACE2</strong> compiles to <strong>(void) 0</strong>.</p></blockquote>
<p>As simple as this, but once in a while you are in a situation where release build fails to work for unknown reason and you need additional information for troubleshooting, and then you remember that you had debug tracing code still nicely available in the source, it is just being stripped out by definition of <a href="http://msdn.microsoft.com/en-us/library/6xkxyz08.aspx">ATLTRACE</a>/<a href="http://msdn.microsoft.com/en-us/library/dhxsse89.aspx">ATLTRACE2</a> macros for release builds.</p>
<p>To avoid reinvention of the wheel and putting new tracing, it might make sense to just re-enable existing tracing (certainly, if putting debug build binary is out of question, which might be the case in production environment and/or to avoid the hassle of installing additional runtime).</p>
<p>The macros need to be #undef&#8217;ined and redefined appropriately with or without limiting scope by <a href="http://msdn.microsoft.com/en-us/library/2bs04eza%28v=vs.100%29.aspx">push_macro</a>/pop_macro #pragma&#8217;s. The trick with macro has to reach two goals, to pick file name, line and current symbol name using <a href="http://msdn.microsoft.com/en-us/library/b0084kay%28v=VS.71%29.aspx">__FILE__</a> and friend macros, and also accept variable number of arguments.</p>
<p>The trick ATL does and we can use too is to define a helper class, with constructor taking file name, line and symbol name values, and cast operator () taking actual tracing parameters and arguments. Internally the output can be mapped to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx">OutputDebugString </a>API so that output could be seen using external tool such as <a href="http://technet.microsoft.com/en-us/sysinternals/bb896647">DebugView</a>.</p>
<p>When everything is well set, new the macros in question can be defined as follows:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">push_macro(</span><span style="color: #0000e6; font-weight: bold;">"ATLTRACE"</span><span style="color: #bb7977; font-weight: bold;">)</span>
<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">push_macro(</span><span style="color: #0000e6; font-weight: bold;">"ATLTRACE2"</span><span style="color: #bb7977; font-weight: bold;">)</span>

<span style="color: #004a43;">#</span><span style="color: #004a43;">undef</span><span style="color: #004a43;"> ATLTRACE</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">undef</span><span style="color: #004a43;"> ATLTRACE2</span>

<span style="color: #004a43;">#</span><span style="color: #004a43;">define</span><span style="color: #004a43;"> ATLTRACE2 CAtlTrace</span><span style="color: #808030;">(</span><span style="color: #004a43;">__FILE__</span><span style="color: #808030;">,</span><span style="color: #004a43;"> __LINE__</span><span style="color: #808030;">,</span><span style="color: #004a43;"> __FUNCTION__</span><span style="color: #808030;">)</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">define</span><span style="color: #004a43;"> ATLTRACE ATLTRACE2</span>

<span style="color: #800000; font-weight: bold;">int</span> _tmain<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">int</span> argc<span style="color: #808030;">,</span> _TCHAR<span style="color: #808030;">*</span> argv<span style="color: #808030;">[</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    ATLTRACE<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">First: </span><span style="color: #0f69ff;">%d</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> __LINE__<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    ATLTRACE<span style="color: #808030;">(</span><span style="color: #800000;">L"</span><span style="color: #0000e6;">Second: </span><span style="color: #0f69ff;">%d</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> __LINE__<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    ATLTRACE2<span style="color: #808030;">(</span>atlTraceGeneral<span style="color: #808030;">,</span> <span style="color: #008c00;">2</span><span style="color: #808030;">,</span> <span style="color: #800000;">"</span><span style="color: #0000e6;">Third: </span><span style="color: #0f69ff;">%d</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> __LINE__<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    ATLTRACE2<span style="color: #808030;">(</span>atlTraceGeneral<span style="color: #808030;">,</span> <span style="color: #008c00;">2</span><span style="color: #808030;">,</span> <span style="color: #800000;">L"</span><span style="color: #0000e6;">Fourth: </span><span style="color: #0f69ff;">%d</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> __LINE__<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>

<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">pop_macro(</span><span style="color: #0000e6; font-weight: bold;">"ATLTRACE2"</span><span style="color: #bb7977; font-weight: bold;">)</span>
<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">pop_macro(</span><span style="color: #0000e6; font-weight: bold;">"ATLTRACE"</span><span style="color: #bb7977; font-weight: bold;">)</span></pre>
<p>And the Release configuration output will be:</p>
<p><img class="alignnone size-full wp-image-1352" title="DebugView Output" src="http://alax.info/blog/wp-content/uploads/2012/04/Image001.png" alt="" width="772" height="343" /></p>
<p>Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/AtlTrace">source code</a> is available from SVN; in particular <strong>CAtlTrace</strong> class is <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/AtlTrace/AtlTrace.cpp#L9">here</a>.</p>
<p>Bonus reading:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/2hfwwtbs%28v=vs.100%29.aspx">Debugging and Error Reporting Macros</a></li>
<li><a href="http://www.cprogramming.com/tutorial/visual_studio_trace_log_messages.html">Debugging with Visual Studio 2005/2008: Logging and Tracing</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1351/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arithmetics Problem Generator</title>
		<link>http://alax.info/blog/1340</link>
		<comments>http://alax.info/blog/1340#comments</comments>
		<pubDate>Thu, 23 Feb 2012 21:47:30 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Source]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1340</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1340" title="Arithmetics Problem Generator"></a>Having no creativity to build problems myself yet trying to train 9 yo buddy in arithmetics, a small application is here to help generate problem to solve to get familiar with operator priorities and basic math. Tweaking code back and &#8230;<p class="read-more"><a href="http://alax.info/blog/1340">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1340" title="Arithmetics Problem Generator"></a><p>Having no creativity to build problems myself yet trying to train 9 yo buddy in arithmetics, a small application is here to help generate problem to solve to get familiar with operator priorities and basic math.</p>
<p>Tweaking code back and forth can make it more or less complex. Regular expression replace by pattern erases all answers from end of lines and the text is ready to be printed for the one being trained.</p>
<pre>180 - 70 - (6460 + 91 - 6) : 77 + (152 - 74 + 2733 - 67) : (4606 : 47) = <strong><span style="color: #ff0000;">?</span></strong>
(1747 - (153 - 66)) : (43 + 125 - (117 - 32)) - 13 * 1 = 7
21 + 145 - (155 - 79) - 120 : 10 - (99 + 560 : 56 - 96) = 65
2 * (270 + 40) : 62 + (139 - 60 + 8753) : (40 + 29) - (180 - (149 - 36 - (91 - 63))) = 43
(4071 - 12 + 2640 : 80) : (98 - (46 + 35 + 7) + 34) = 93
151 + 39 - 45 - (59 + 62 - 33 - (61 - (70 - 46))) = 94
1 * 11 * 4 - (185 - 73 - (118 + 54 + 14 - 89)) = 29
122 - 83 + 81 - (1 + 8) - (101 - 42 - 5 * 2) = 62
254 - 82 - 65 - ((3342 + 13) : 55 + 118 - 96) + (41 * 2 - 38) * 0 * 95 = 24
(1 * 35 + 204 - 87 - 79 + 4 + 5 - (16 + 2758) : (81 - 43)) * 2133 : 79 : 27 = 9</pre>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/GenerateMathProblem01/Release/GenerateMathProblem01.exe">Win32</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/GenerateMathProblem01">source code</a> are available from SVN.</p>
<p>If you just need an infinite source of things to crack, it&#8217;s here also: <a href="http://www.alax.info/svn/public/trunk/Utilities/GenerateMathProblem01/Release/01.txt">01.txt</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1340/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Mappings: Virtual Memory and Virtual Address Space</title>
		<link>http://alax.info/blog/1325</link>
		<comments>http://alax.info/blog/1325#comments</comments>
		<pubDate>Thu, 15 Dec 2011 23:36:34 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[32-bit]]></category>
		<category><![CDATA[CreateFileMapping]]></category>
		<category><![CDATA[file mapping]]></category>
		<category><![CDATA[MapViewOfFile]]></category>
		<category><![CDATA[RAM]]></category>
		<category><![CDATA[virtual memory]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1325</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1325" title="File Mappings: Virtual Memory and Virtual Address Space"></a>More and more applications hit the Windows limit of available address space for 32-bit applications, and the whole concept becomes more important for understanding due to necessity to work things around. A thing, which is more or less easy to &#8230;<p class="read-more"><a href="http://alax.info/blog/1325">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1325" title="File Mappings: Virtual Memory and Virtual Address Space"></a><p>More and more applications hit the Windows limit of available <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366912%28v=VS.85%29.aspx">address space</a> for 32-bit applications, and the whole concept becomes more important for understanding due to necessity to work things around.</p>
<p>A thing, which is more or less easy to understand, is that a user mode 32-bit application can address 2^32 addresses. The addresses are not directly physical RAM and the operating system is responsible for management of the mapping addresses into RAM as a part of virtual memory manager operation. Paged memory organization is <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366914%28v=VS.85%29.aspx">well documented on MSDN</a>, and the questions has been raised numerous times. An interesting question is whether a 32-bit application can effectively manage memory amounts exceeding address space limits.</p>
<p>Back in 80386 times, the systems could address megabytes of RAM in 16-bit code through <a href="http://en.wikipedia.org/wiki/EXtended_Memory_Specification#eXtended_Memory_Specification_.28XMS.29">XMS</a> and <a href="http://en.wikipedia.org/wiki/Expanded_memory">EMS</a> services. The application could access &#8220;high&#8221; memory addresses by requesting mapping portions of RAM into lower megabyte address space. In some way similar technique is also here for 32-bit applications in Windows through use of <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366556%28v=vs.85%29.aspx">file mappings</a>.</p>
<p>A regular memory backed file mapping requests Windows to reserve a memory block which becomes available for mapping into address space of one or more processes. Creating file mapping itself does not imply mapping and this leaves a great option for the owner to allocate more data than it can actually map into address space: if 32-bit process virtual address space is fundamentally constrained, the file mapping allocation space is more loosely limited by amount of physical memory and paging file. The application can allocate 2, 3, 4 and more gigabytes of memory &#8211; it just cannot still map it all together into address space and make it available simultaneously.</p>
<p>The FileMappingVirtualAddress utility does a simple thing:</p>
<ul>
<li>on startup it allocates (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=VS.85%29.aspx">CreateFileMapping</a>) as many 256 MB file mappings as operating system would allow, and shows it in a list</li>
<li>each time a user checks a box, the application maps (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx">MapViewOfFile</a>) corresponding file mapping into address space; unchecking a box unmaps the view</li>
<li>the caption shows currently used and maximal available virtual address space</li>
</ul>
<p><img class="alignnone size-full wp-image-1326" title="Allocated and Mapped 256 MB File Mappings" src="http://alax.info/blog/wp-content/uploads/2011/12/Image001.png" alt="" width="462" height="426" /></p>
<p>A plain 32-bit version of the application allocated 51 blocks for me (which totals in 13 GB of memory, with 8 GB physical RAM installed in the system). The allocation takes place immediately because the operating system does not actually make all this memory prepared for use &#8211; the actual pages would be allocated and ready to use on demand when the application requires them.</p>
<p><strong>The most important part made so obvious is that the 32-bit application succeeds in allocating well over 4 GB, which is maximal virtual address space it can ever get.</strong></p>
<p>The virtual address space in use is only 1641 MB and another request to map an additional section with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx">MapViewOfFile</a> would fail (the default address space limit is 2 GB) &#8211; space fragmentation make mapping unavailable earlier than we actually use the whole space, since the API would need to allocate contiguous range of addresses to satisfy the request.</p>
<p><img class="alignnone size-full wp-image-1327" title="Error Hitting Virtual Address Space Limit" src="http://alax.info/blog/wp-content/uploads/2011/12/Image002.png" alt="" width="426" height="171" /></p>
<p>32-bit application built with <a href="http://msdn.microsoft.com/en-us/library/wz223b1z%28v=VS.100%29.aspx">/LARGEADDRESSAWARE</a> parameter might manage to do more allocations: 64-bit versions of Windows provide 4 GB of addresses to 32-bit processes. 32-bit operating systems might also be extending the limit in case of <a href="http://support.microsoft.com/kb/291988">4GB RAM Tuning</a> (which would typically be 3 GB of space for a process).</p>
<p><img class="alignnone size-full wp-image-1328" title="File Mappings with /LARGEADDRESSAWARE" src="http://alax.info/blog/wp-content/uploads/2011/12/Image003.png" alt="" width="462" height="426" /></p>
<p>Finally, 64-bit build of the application is free from virtual address space limit as the limit is <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa384271%28v=VS.85%29.aspx">8 terabytes</a>. The mapping is again instantaneous because actual RAM will be supplied on first request to mapped pages only.</p>
<p><img class="alignnone size-full wp-image-1329" title="File Mappings in 64-bit Code" src="http://alax.info/blog/wp-content/uploads/2011/12/Image004.png" alt="" width="462" height="426" /></p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/FileMappingVirtualAddress/Win32/Release/FileMappingVirtualAddress.exe">Win32</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/FileMappingVirtualAddress/Win32/Release%20with%20Large%20Address%20Aware/FileMappingVirtualAddress.exe">Win32 with /LARGEADDRESSAWARE</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/FileMappingVirtualAddress/x64/Release/FileMappingVirtualAddress.exe">x64</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/FileMappingVirtualAddress">partial source code</a> are available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1325/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hardware assisted memory corruption detection</title>
		<link>http://alax.info/blog/1319</link>
		<comments>http://alax.info/blog/1319#comments</comments>
		<pubDate>Sat, 19 Nov 2011 17:26:25 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[corruption]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[protection]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1319</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1319" title="Hardware assisted memory corruption detection"></a>So you got a memory corruption issue with a piece of software. It comes in a unique scenario along the line of having a huge pile of weird code running well most of the time and then, right out of &#8230;<p class="read-more"><a href="http://alax.info/blog/1319">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1319" title="Hardware assisted memory corruption detection"></a><p>So you got a memory corruption issue with a piece of software. It comes in a unique scenario along the line of having a huge pile of weird code running well most of the time and then, right out of the blue, a corruption takes place followed by unexpected code execution and unstable software state in general.</p>
<p>The biggest problem with memory corruption is that a fragment of code is modifying a memory block which it does not own, and it has no idea who actually is the owner of the block, while the real owner has no timely way to detect the modification. You only face the consequences being unable to capture the modification moment in first place.</p>
<p>To get back to the original cause, an engineer has to drop into a time machine, turn back time and step back to where the trouble took originally place. As developers are not actually given state-of-the-art time machines, the time turning step is speculative.</p>
<h4>CVirtualHeapPtr Class: Memory with Exception-on-Write access mode</h4>
<p>At the same time a Windows platform developer is or might be aware of <a href="http://msdn.microsoft.com/en-us/library/ms810627.aspx">virtual memory API</a> which among other things provides user mode application with capabilities to define memory protection modes. Having this on hands opens unique opportunity to apply read-only protection (PAGE_READONLY) onto a memory block and have exception raised at the very moment of unexpected memory modification, having call stack showing up a source of the problem. I refer to this mode of operation as &#8220;hardware assisted&#8221; because the access violation exception/condition would be generated purely in hardware without any need to additionally do any address comparison in code.</p>
<p>Needless to say that this way is completely convenient for the developer as he does not need to patch the monstrous application all around in order to compare access addresses against read-only fragment. Instead, a block defined as read-only will be immediately available as such for the whole process almost without any performance overhead.</p>
<p>As ATL provides a set of memory allocator templates (<a href="http://msdn.microsoft.com/en-us/library/3by29yh0%28v=vs.80%29.aspx">CHeapPtr</a> for heap backed memory blocks, allocated with <em>CCRTAllocator</em>, alternate options include <a href="http://msdn.microsoft.com/en-us/library/80zw33a6%28v=vs.80%29.aspx">CComHeapPtr</a> with <em>CComAllocator</em> wrapping <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms692727%28v=vs.85%29.aspx">CoTaskMemAlloc</a>/<em>CoTaskMemFree</em> API), let us make an alternate allocator option that mimic well-known class interface and would facilitate corruption detection.</p>
<p>Because virtual memory allocation unit is a page, and protection mode is defined for the whole page, this would be the allocation granularity. For a single allocated byte we would need to request <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958%28v=vs.85%29.aspx">SYSTEM_INFO::dwPageSize</a> bytes of virtual memory. Unlike normal memory heap manager, we have no way to share pages between allocations as we would be unable to effectively apply protection modes. This would definitely increase application pressure onto virtual memory, but is still acceptable for the sacred task of troubleshooting.</p>
<p>We define a <em>CVirtualAllocator</em> class to be compatible with ATL&#8217;s <em>CCRTAllocator</em>, however based on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx">VirtualAlloc</a>/<em>VirtualFree</em> API. The smart pointer class over memory pointer would be defined as follows:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">template</span> <span style="color: #800080;">&lt;</span><span style="color: #800000; font-weight: bold;">typename</span> T<span style="color: #800080;">&gt;</span>
<span style="color: #800000; font-weight: bold;">class</span> CVirtualHeapPtr <span style="color: #800080;">:</span>
    <span style="color: #800000; font-weight: bold;">public</span> CHeapPtr<span style="color: #800080;">&lt;</span>T<span style="color: #808030;">,</span> CVirtualAllocator<span style="color: #800080;">&gt;</span>
<span style="color: #800080;">{</span>
<span style="color: #800000; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: #696969;">// CVirtualHeapPtr</span>
    CVirtualHeapPtr<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throw</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">explicit</span> CVirtualHeapPtr<span style="color: #808030;">(</span>_In_ T<span style="color: #808030;">*</span> pData<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throw</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #603000;">VOID</span> SetProtection<span style="color: #808030;">(</span><span style="color: #603000;">DWORD</span> nProtection<span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        <span style="color: #696969;">// </span><span style="color: #ffffff; background: #808000;">TODO: ...</span>
    <span style="color: #800080;">}</span>
<span style="color: #800080;">}</span><span style="color: #800080;">;</span></pre>
<p>The <em>SetProtection</em> method is to define memory protection for the memory block. Full code for the classes <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.h#L9">is available on Trac here</a> (lines 9-132):</p>
<ul>
<li><em>CGlobalVirtualAllocator</em> class is a singleton querying operating system for virtual memory page size, and provides alignment method</li>
<li><em>CVirtualAllocator</em> class is a <em>CCRTAllocator</em>-compatible allocator class</li>
<li><em>CVirtualHeapPtr</em> class is smart template class wrapping a pointer to allocated memory</li>
</ul>
<p>Use case code will be as follows. &#8220;SetProtection(PAGE_READONLY)&#8221; enables protection on memory block and turns on exception generation at the moment memory block modification attempt. &#8220;SetProtection(PAGE_READWRITE)&#8221; would restore normal mode of memory operation.</p>
<pre style="color: #000000; background: #ffffff;">CVirtualHeapPtr<span style="color: #800080;">&lt;</span><span style="color: #603000;">BYTE</span><span style="color: #800080;">&gt;</span> p<span style="color: #800080;">;</span>
p<span style="color: #808030;">.</span>Allocate<span style="color: #808030;">(</span><span style="color: #008c00;">2</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x01</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READONLY<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #696969;">// NOTE: Compile with /EHa on order to catch the exception</span>
_ATLTRY
<span style="color: #800080;">{</span>
    p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x02</span><span style="color: #800080;">;</span>
    <span style="color: #696969;">// NOTE: We never reach here due to exception</span>
<span style="color: #800080;">}</span>
_ATLCATCHALL<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #696969;">// NOTE: Catching the access violation for now to be able to continue execution</span>
<span style="color: #800080;">}</span>
p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READWRITE<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x03</span><span style="color: #800080;">;</span></pre>
<p>Given the information what data gets corrupt, the pointer allocator provides an efficient opportunity to detect the violation attempt. The only thing remained is to keep memory read-only, and temporarily revert to write access when the &#8220;legal&#8221; memory modification code is about to be executed.</p>
<p><span id="more-1319"></span></p>
<h5>One-shot Read/Write Protection with Guard Pages</h5>
<p>Another option granted by memory protection modes is brought by PAGE_GUARD flag. MSDN says:</p>
<blockquote><p>A guard page provides a one-shot alarm for memory page access. This can be useful for an application that needs to monitor the growth of large dynamic data structures. For example, there are operating systems that use guard pages to implement automatic stack checking.</p></blockquote>
<p>Setting a guard page mode provides an additional option to trigger an exception with even read access to a protected memory block.</p>
<pre style="color: #000000; background: #ffffff;">p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READWRITE <span style="color: #808030;">|</span> PAGE_GUARD<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #603000;">BYTE</span> n <span style="color: #808030;">=</span> p<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #800080;">;</span></pre>
<h4>CDebugHeapPtr Class: More Options to Catch Memory Corruption Conditions</h4>
<p>While setting memory protection attributes on a memory block of interest provides unique troubleshooting opportunities, it still does not cover important typical problems with memory misuse scenarios. Those are writing immediately before the allocated block, and writing immediately after. Having array of N items, this would be writing to indices -1 and N respectively.</p>
<p>To address this scenarios of misuse we can extend CVirtualHeapPtr class so that it could additionally provide &#8220;sanity pages&#8221; with PAGE_NOACCESS protection at the boundary of allocation. Because virtual memory allocation is granular, we will have to have padding bytes that extend our block to the page boundary, however we have an option to put the padding bytes before or after the payload data block in order to capture after or before memory block writes respectively.</p>
<p>The figure below shows memory layout for the data:</p>
<p><img class="alignnone size-full wp-image-1320" title="CDebugHeapPtr Memory Layout" src="http://alax.info/blog/wp-content/uploads/2011/11/image.png" alt="" width="509" height="541" /></p>
<p>Source code for the <em>CDebugHeapPtr</em> class <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.h#L155">is available on Trac</a> (lines 155-). The sanity pages create a block of inaccessible addresses which immediately cause access violation exception on either read of write access attempt. Under debugger, those are shows with question marks:</p>
<p><img class="alignnone size-full wp-image-1321" title="PAGE_NOACCESS Data" src="http://alax.info/blog/wp-content/uploads/2011/11/Image0011.png" alt="" width="673" height="288" /></p>
<p>The padding space is pre-initialized with hardcoded value 0&#215;77, and the space is checked for integrity at release of memory block call.</p>
<h4>Catching the Exceptions</h4>
<p>Having the exceptions generated on run-time, they immediately alter application execution code path and are easy to track and catch. There is no need to bring the feature rich debugger, such as Visual Studio to the production site in order to catch the exception and environment, instead a way simpler tool such as <a href="http://alax.info/blog/1248">LogProcessExceptions</a> would be able to create a minidump file and write the state of the application. The minidump can be transferred into debugger-enabled environment for detailed check.</p>
<p>Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr">source code</a> is available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1319/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GPS Location/Coordinate Converter: Multiple Locations at Once</title>
		<link>http://alax.info/blog/1313</link>
		<comments>http://alax.info/blog/1313#comments</comments>
		<pubDate>Mon, 14 Nov 2011 18:06:26 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[rally]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1313</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1313" title="GPS Location/Coordinate Converter: Multiple Locations at Once"></a>Today&#8217;s update lets you convert multiple locations at once with a single click. Here is the story behind the update and use case scenario. In rally raid sport events (so called baja), a team gets a road book for the &#8230;<p class="read-more"><a href="http://alax.info/blog/1313">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1313" title="GPS Location/Coordinate Converter: Multiple Locations at Once"></a><p>Today&#8217;s update lets you convert multiple locations at once with a single click. Here is the story behind the update and use case scenario.</p>
<p>In <a href="http://en.wikipedia.org/wiki/Rally_raid">rally raid</a> sport events (so called <em>baja</em>), a team gets a road book for the next competition day in a few hours before actual start. The GPS coordinates are printed on one of the pages of the roadbook and are not available in any electronic format.</p>
<p>There were just a few times when the organizer also uploaded a copy of a file with the coordinates and shared a link to download from, but this was rather an exception. Another alternate option was a dedicated person to upload the coordinates (they were earlier full tracks, but at some point tracks were no longer available at all) to pilots&#8217; hardware, but in a state of pre-start рфыеу and variety of GPS hardware, formats, cable etc. this created lines of people. The most one can rely on is a sheet of paper with GPS coordinates. The mistery does not end even here as you don&#8217;t know whether you are to get Degrees only, or Degrees and Minutes, or Degrees, Minutes and Seconds. Everything depends on software the organizer uses.</p>
<p><img class="alignnone size-full wp-image-1314" title="A Page with GPS Locations in Road Book " src="http://alax.info/blog/wp-content/uploads/2011/11/Scan1.jpg" alt="" width="480" height="307" /></p>
<p>As soon as you get a hard copy of this, the idea is to upload it into device as quickly as possible because there are other things to do and the time is normally 11 PM when the race is to start 7 AM next day tens of miles away from you. The time interval will be shared by uploading data, sleeping and transfer to start location.</p>
<p>The utility is here to grant extra sleep time. Since it is capable to accept various separators on the input, a convenient way is to quickly type in the text in Microsoft Excel, check the data against the hardcopy, and copy into clipboard to transfer to the utility.</p>
<p><img class="alignnone size-full wp-image-1315" title="GPS Coordinates in Microsoft Excel" src="http://alax.info/blog/wp-content/uploads/2011/11/Image001.png" alt="" width="558" height="472" /></p>
<p>A hotkey with conversion transfers data into format of interest, and single &#8220;Find and Replace&#8221; operation creates a good OziExplorer waypoint file which is good for upload onto portable navigation device.<img class="alignnone size-large wp-image-1316" title="Visual Studio with OziExplorer Waypoint Data" src="http://alax.info/blog/wp-content/uploads/2011/11/Image002-687x600.png" alt="" width="620" height="541" /></p>
<p>The whole thing take a few minutes to do with minimal routine typing in.</p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/GpsLocationConverter/_Bin/Release/GpsLocationConverter.exe">Win32</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/GpsLocationConverter">partial source code</a> are available from SVN.</p>
<p>Bonus picture, rally raid Suzuki is on the way to score the victory and the rally promotional teaser:</p>
<p><a href="https://picasaweb.google.com/105209331201471192155/BajaPolygon4thRoundOfUkrainianCup2011InRallyRaidsNearNovomoskovskUkraine"><img class="alignnone" src="https://lh6.googleusercontent.com/--53JPr3B2Hw/TsD_WnIrE9I/AAAAAAAAFXo/PjmYMfS_WJI/s640/DSC04127a.jpg" alt="Rally Raid Suzuki Samurai on the Way" width="640" height="426" /></a></p>
<p><object width="640" height="360"><param name="movie" value="http://www.youtube.com/v/k-_-Re2PhQ8&#038;rel=0&#038;hl=en_US&#038;feature=player_embedded&#038;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/k-_-Re2PhQ8&#038;rel=0&#038;hl=en_US&#038;feature=player_embedded&#038;version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1313/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GPS Location/Coordinate Converter: Fractional Seconds, More Shortcuts</title>
		<link>http://alax.info/blog/1302</link>
		<comments>http://alax.info/blog/1302#comments</comments>
		<pubDate>Thu, 20 Oct 2011 17:48:42 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[coordinate]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1302</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1302" title="GPS Location/Coordinate Converter: Fractional Seconds, More Shortcuts"></a>This adds a small update to the recently published GPS Location/Coordinate Converter utility: Seconds in Degrees, Minutes &#38; Seconds notation are shown and are accepted as floating point numbers More shortcuts to popular online map services (note that only Google &#8230;<p class="read-more"><a href="http://alax.info/blog/1302">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1302" title="GPS Location/Coordinate Converter: Fractional Seconds, More Shortcuts"></a><p>This adds a small update to the recently published <a title="Permalink to Utility Clearance: GPS Location/Coordinate Converter" href="../1297">GPS Location/Coordinate Converter</a> utility:</p>
<ul>
<li>Seconds in <em>Degrees, Minutes &amp; Seconds</em> notation are shown and are accepted as floating point numbers</li>
<li>More shortcuts to popular online map services (note that only Google Maps and Yandex Maps are still accepted as input via clipboard):</li>
<ul>
<li style="text-align: left;">Bing Maps</li>
<li style="text-align: left;">Yahoo Maps</li>
<li style="text-align: left;">Open Street Map</li>
<li style="text-align: left;">WikiMapia</li>
</ul>
</ul>
<p>The latter makes the tool an easy to use converted between the services for a GPS POI.</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/10/Image0011.png"><img class="alignnone size-large wp-image-1303" title="GPS Location Converter" src="http://alax.info/blog/wp-content/uploads/2011/10/Image0011-800x316.png" alt="" width="620" height="244" /></a></p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/GpsLocationConverter/_Bin/Release/GpsLocationConverter.exe">Win32</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/GpsLocationConverter">partial source code</a> are available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1302/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utility Clearance: GPS Location/Coordinate Converter</title>
		<link>http://alax.info/blog/1297</link>
		<comments>http://alax.info/blog/1297#comments</comments>
		<pubDate>Wed, 12 Oct 2011 21:20:40 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[coordinate]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1297</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1297" title="Utility Clearance: GPS Location/Coordinate Converter"></a>This tool came up as a result of mess around GPS coordinate formatting and variety of popular formats and notations. Some use latitude and longitude degrees, some prefer degrees and minutes with fractional part. this does not take into account &#8230;<p class="read-more"><a href="http://alax.info/blog/1297">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1297" title="Utility Clearance: GPS Location/Coordinate Converter"></a><p>This tool came up as a result of mess around GPS coordinate formatting and variety of popular formats and notations. Some use latitude and longitude degrees, some prefer degrees and minutes with fractional part. this does not take into account choices for datum and file formats.</p>
<p>For instance, both <a href="http://maps.google.com">Google Maps</a> and <a href="http://maps.yandex.ru">Yandex Maps</a> accept latitude and longitude as <strong>ll=</strong> URL parameter with the value of latitude and longitude in degrees, however Google uses latitude coming first, while Yandex prefers the opposite.</p>
<p>Recently, a rally road book contained the following formatting of track points:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/10/Image001.png"><img class="alignnone size-large wp-image-1298" title="Rally Raid Service Book" src="http://alax.info/blog/wp-content/uploads/2011/10/Image001-800x418.png" alt="" width="620" height="323" /></a></p>
<p>Having to put a stop to the madness, this utility comes up as a handy assistant to convert and format GPS coordinates into reasonable presentation and separators.</p>
<p>The utility runs on background and monitors clipboard, and once it recognizes one of the following:</p>
<ul>
<li>GPS Point in Degrees</li>
<li>GPS Point in Degrees and Minutes</li>
<li>GPS Point in Degrees, Minutes and Seconds</li>
<li>Google Maps URL</li>
<li>Yandex Maps URL</li>
</ul>
<p>it flashes and updates its UI providing the choices of track point formatting:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/10/Image0021.png"><img class="alignnone size-full wp-image-1299" title="GPS Point Formatting and Options" src="http://alax.info/blog/wp-content/uploads/2011/10/Image0021.png" alt="" width="778" height="248" /></a></p>
<p>The utility is trying hard to accept various separators and formats, just one thing is important to keep in mind: if you are using minutes without seconds, the fractional part of minutes should be separated by decimal point (.).</p>
<p>The utility also responds to hotkeys <em>Ctrl+Shift+F1</em>, <em>Ctrl+Shift+F2</em> etc. and copies the formatted point location back into clipboard so that one could quickly re-format editable text in another application through clipboard updates. Alternatively, it is possible to click the formatted value and place it into clipboard.</p>
<p>The utility also provides clickable hyperlinks to open Google Maps and Yandex Maps into the point of interest.</p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/GpsLocationConverter/_Bin/Release/GpsLocationConverter.exe">Win32</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/GpsLocationConverter">partial source code</a> are available from SVN.</p>
<p>Note: you might need to run the tool &#8220;As Administrator&#8221; elevating UAC privileges in Vista/7 operating systems.</p>
<p>Bonus links on the topic:</p>
<ul>
<li><a href="http://boulter.com/gps/">GPS Coordinate Converter, Maps and Info</a></li>
<li><a href="http://www.benichou-software.com/index.php?option=com_content&amp;view=article&amp;id=4&amp;Itemid=2&amp;lang=en">ITN Converter</a> &#8211; converts for route file formats</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1297/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sample: Simultaneous Audio Playback via Waveform Audio (waveOut) API</title>
		<link>http://alax.info/blog/1291</link>
		<comments>http://alax.info/blog/1291#comments</comments>
		<pubDate>Wed, 12 Oct 2011 17:15:50 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[playback]]></category>
		<category><![CDATA[waveOut]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1291</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1291" title="Sample: Simultaneous Audio Playback via Waveform Audio (waveOut) API"></a>The minimalistic sample demonstrates support of [deprecated] Waveform Audio API for multiple playback streams. Depending on command line parameters, the application starts threads to open audio hardware using separate waveOutOpen call and stream one or more generated sine waves: 1,000 &#8230;<p class="read-more"><a href="http://alax.info/blog/1291">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1291" title="Sample: Simultaneous Audio Playback via Waveform Audio (waveOut) API"></a><p>The minimalistic sample demonstrates support of [deprecated] <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd757715%28v=VS.85%29.aspx">Waveform Audio API</a> for multiple playback streams.</p>
<p>Depending on command line parameters, the application starts threads to open audio hardware using separate <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd743866%28v=VS.85%29.aspx">waveOutOpen</a> call and stream one or more generated sine waves:</p>
<ul>
<li>1,000 Hz sine wave as 22,050 Hz, Mono, 16-bit PCM (command line parameter &#8220;a&#8221;)</li>
<li>5,000 Hz sine wave as 32,000 Hz, Mono, 16-bit PCM (command line parameter &#8220;b&#8221;)</li>
<li>15,000 Hz sine wave as 44,100 Hz, Mono, 16-bit PCM (command line parameter &#8220;c&#8221;)</li>
</ul>
<pre style="color: #000000; background: #ffffff;">Check<span style="color: #808030;">(</span><span style="color: #400000;">waveOutOpen</span><span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>hWaveOut<span style="color: #808030;">,</span> WAVE_MAPPER<span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>WaveFormatEx<span style="color: #808030;">,</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">,</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">,</span> CALLBACK_NULL<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
ATLASSERT<span style="color: #808030;">(</span>hWaveOut<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
WAVEHDR<span style="color: #808030;">*</span> pWaveHeader<span style="color: #800080;">;</span>
<span style="color: #603000;">HGLOBAL</span> hWaveHeader <span style="color: #808030;">=</span> <span style="color: #808030;">(</span>WAVEHDR<span style="color: #808030;">*</span><span style="color: #808030;">)</span> <span style="color: #400000;">GlobalAlloc</span><span style="color: #808030;">(</span>GMEM_MOVEABLE <span style="color: #808030;">|</span> GMEM_SHARE<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">sizeof</span> <span style="color: #808030;">*</span>pWaveHeader <span style="color: #808030;">+</span> WaveFormatEx<span style="color: #808030;">.</span>nAvgBytesPerSec <span style="color: #808030;">*</span> <span style="color: #008c00;">10</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
pWaveHeader <span style="color: #808030;">=</span> <span style="color: #808030;">(</span>WAVEHDR<span style="color: #808030;">*</span><span style="color: #808030;">)</span> <span style="color: #400000;">GlobalLock</span><span style="color: #808030;">(</span>hWaveHeader<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
ATLENSURE_THROW<span style="color: #808030;">(</span>pWaveHeader<span style="color: #808030;">,</span> E_OUTOFMEMORY<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>lpData <span style="color: #808030;">=</span> <span style="color: #808030;">(</span><span style="color: #603000;">LPSTR</span><span style="color: #808030;">)</span> <span style="color: #808030;">(</span><span style="color: #603000;">BYTE</span><span style="color: #808030;">*</span><span style="color: #808030;">)</span> <span style="color: #808030;">(</span>pWaveHeader <span style="color: #808030;">+</span> <span style="color: #008c00;">1</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>dwBufferLength <span style="color: #808030;">=</span> WaveFormatEx<span style="color: #808030;">.</span>nAvgBytesPerSec <span style="color: #808030;">*</span> <span style="color: #008c00;">10</span><span style="color: #800080;">;</span>
<span style="color: #696969;">//pWaveHeader-&gt;dwUser = </span>
pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>dwFlags <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>dwLoops <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">region Generate Actual Data</span>
<span style="color: #800080;">{</span>
    <span style="color: #603000;">SHORT</span><span style="color: #808030;">*</span> pnData <span style="color: #808030;">=</span> <span style="color: #808030;">(</span><span style="color: #603000;">SHORT</span><span style="color: #808030;">*</span><span style="color: #808030;">)</span> pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>lpData<span style="color: #800080;">;</span>
    SIZE_T nDataCount <span style="color: #808030;">=</span> pWaveHeader<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>dwBufferLength <span style="color: #808030;">/</span> <span style="color: #800000; font-weight: bold;">sizeof</span> <span style="color: #808030;">*</span>pnData<span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">for</span><span style="color: #808030;">(</span>SIZE_T nIndex <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span> nIndex <span style="color: #808030;">&lt;</span> nDataCount<span style="color: #800080;">;</span> nIndex<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #808030;">)</span>
    pnData<span style="color: #808030;">[</span>nIndex<span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #808030;">(</span><span style="color: #603000;">SHORT</span><span style="color: #808030;">)</span> <span style="color: #808030;">(</span><span style="color: #008c00;">32000</span> <span style="color: #808030;">*</span> <span style="color: #603000;">sin</span><span style="color: #808030;">(</span><span style="color: #008000;">1.0</span> <span style="color: #808030;">*</span> nIndex <span style="color: #808030;">/</span> WaveFormatEx<span style="color: #808030;">.</span>nSamplesPerSec <span style="color: #808030;">*</span> nFrequency <span style="color: #808030;">*</span> <span style="color: #008c00;">2</span> <span style="color: #808030;">*</span> M_PI<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>
<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">endregion </span>
Check<span style="color: #808030;">(</span>waveOutPrepareHeader<span style="color: #808030;">(</span>hWaveOut<span style="color: #808030;">,</span> pWaveHeader<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">sizeof</span> <span style="color: #808030;">*</span>pWaveHeader<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
Check<span style="color: #808030;">(</span>waveOutWrite<span style="color: #808030;">(</span>hWaveOut<span style="color: #808030;">,</span> pWaveHeader<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">sizeof</span> <span style="color: #808030;">*</span>pWaveHeader<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #400000;">GlobalUnlock</span><span style="color: #808030;">(</span>hWaveHeader<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>The operating system is supposed to mix the waves, which can be easily perceived taking place. It is possible to run the application with multiple waveforms within a process, e.g. &#8220;abc&#8221; command line parameter, and/or start multiple instances of the application.</p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/WaveOutMultiPlay/Release/WaveOutMultiPlay.exe">Win32</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/WaveOutMultiPlay">source code</a> are available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1291/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Utility Clearance: Enumerate Audio &#8216;MMDevice&#8217;s</title>
		<link>http://alax.info/blog/1279</link>
		<comments>http://alax.info/blog/1279#comments</comments>
		<pubDate>Tue, 30 Aug 2011 17:14:24 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Core Audio]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[IMMDevice]]></category>
		<category><![CDATA[MMDevice]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[WASAPI]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1279</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1279" title="Utility Clearance: Enumerate Audio &#039;MMDevice&#039;s"></a>The utility and code does straightforward enumeration of MMDevices (Vista+, check MSDN for MMDevice API availability), which correspond to MMDevice API, WASAPI, Core Audio API. The code itself is straightforward, with a ready to use binary to quickly lookup data &#8230;<p class="read-more"><a href="http://alax.info/blog/1279">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1279" title="Utility Clearance: Enumerate Audio &#039;MMDevice&#039;s"></a><p>The utility and code does straightforward enumeration of MMDevices (Vista+, check MSDN for <a href="http://msdn.microsoft.com/en-us/library/dd371399%28v=vs.85%29.aspx">MMDevice API availability</a>), which correspond to MMDevice API, WASAPI, Core Audio API. The code itself is straightforward, with a ready to use binary to quickly lookup data of interest:</p>
<p><img class="alignnone size-full wp-image-1280" title="Enumerate Audio Devices in Action" src="http://alax.info/blog/wp-content/uploads/2011/08/Image0012.png" alt="" width="541" height="363" /></p>
<p>The data is detailed well and in Excel-friendly format (via Copy/Paste):</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/08/Image002.png"><img class="alignnone size-large wp-image-1281" title="Enumerate Audio Device Output in Microsoft Excel" src="http://alax.info/blog/wp-content/uploads/2011/08/Image002-800x362.png" alt="" width="620" height="280" /></a></p>
<p>The code also automatically looks up for named Windows SDK constants, such as <a href="http://msdn.microsoft.com/en-us/library/dd316594%28v=vs.85%29.aspx">PKEY_Device_FriendlyName</a>:</p>
<pre>    Identifier    {0.0.1.00000000}.{4c1a7642-3f91-43e5-8fcf-b4b1e803d3f9}
    State    DEVICE_STATE_DISABLED    0x02
    Properties:
        {B3F8FA53-0004-438E-9003-51A46E139BFC}, 15    16 bytes of BLOB, DA 07 03 00 02 00 09 00 0E 00 39 00 16 00 C5 02    65
        PKEY_Device_DeviceDesc    Stereo Mix    31
        {B3F8FA53-0004-438E-9003-51A46E139BFC}, 6    Realtek High Definition Audio    31
        {B3F8FA53-0004-438E-9003-51A46E139BFC}, 2    {1}.HDAUDIO\FUNC_01&amp;VEN_10EC&amp;DEV_0888&amp;SUBSYS_80860034&amp;REV_1002\4&amp;37D44F2F&amp;0&amp;0201    31
        {83DA6326-97A6-4088-9453-A1923F573B29}, 3    oem29.inf:AzaliaManufacturerID.NTamd64.6.0:IntcAzAudModel:6.0.1.5964:hdaudio\func_01&amp;ven_10ec&amp;dev_0888    31
        PKEY_Device_BaseContainerId    {00000000-0000-0000-FFFF-FFFFFFFFFFFF}    72
        PKEY_Device_ContainerId    {00000000-0000-0000-FFFF-FFFFFFFFFFFF}    72
        PKEY_Device_EnumeratorName    HDAUDIO    31
        PKEY_AudioEndpoint_FormFactor    10    19
        PKEY_AudioEndpoint_JackSubType    {DFF21FE1-F70F-11D0-B917-00A0C9223196}    31
        PKEY_DeviceClass_IconPath    %windir%\system32\mmres.dll,-3018    31
        {840B8171-B0AD-410F-8581-CCCC0382CFEF}, 0    316 bytes of BLOB, 01 00 00 00 38 01 00 00 ... 00 00 00 00    65
        PKEY_AudioEndpoint_Association    {00000000-0000-0000-0000-000000000000}    31
        PKEY_AudioEndpoint_Supports_EventDriven_Mode    1    19
        {24DBB0FC-9311-4B3D-9CF0-18FF155639D4}, 3    0    11
        {24DBB0FC-9311-4B3D-9CF0-18FF155639D4}, 4    -1    11
        {9A82A7DB-3EBB-41B4-83BA-18B7311718FC}, 1    65536    19
        {233164C8-1B2C-4C7D-BC68-B671687A2567}, 1    {2}.\\?\hdaudio#func_01&amp;ven_10ec&amp;dev_0888&amp;subsys_80860034&amp;rev_1002#4&amp;37d44f2f&amp;0&amp;0201#{6994ad04-93ef-11d0-a3cc-00a0c9223196}\rtstereomixwave    31
        {5A9125B7-F367-4924-ACE2-0803A4A3A471}, 0    1610612916    19
        {B3F8FA53-0004-438E-9003-51A46E139BFC}, 0    3    19
<span style="color: #3366ff;"><strong>        PKEY_Device_FriendlyName    Stereo Mix (Realtek High Definition Audio)    31</strong></span>
        PKEY_DeviceInterface_FriendlyName    Realtek High Definition Audio    31
        PKEY_AudioEndpoint_GUID    {4C1A7642-3F91-43E5-8FCF-B4B1E803D3F9}    31</pre>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/EnumerateAudioDevices/_Bin/Win32/Release/EnumerateAudioDevices.exe">Win32</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/EnumerateAudioDevices/_Bin/x64/Release/EnumerateAudioDevices.exe">x64</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/EnumerateAudioDevices">source code</a> are available from SVN.</p>
<p>See also:</p>
<ul>
<li><a href="http://blogs.msdn.com/b/matthew_van_eerde/archive/2011/06/13/how-to-enumerate-audio-endpoint-immdevice-properties-on-your-system.aspx">A similar tool with source code</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/dd371400%28v=VS.85%29.aspx">IMMDeviceEnumerator::EnumAudioEndpoints</a> on MSDN</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1279/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

