<?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; ATL</title>
	<atom:link href="http://alax.info/blog/tag/atl/feed" rel="self" type="application/rss+xml" />
	<link>http://alax.info/blog</link>
	<description>// Software Production Line</description>
	<lastBuildDate>Fri, 03 Feb 2012 22:49:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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: 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>ATLENSURE_SUCCEEDED double failure</title>
		<link>http://alax.info/blog/1244</link>
		<comments>http://alax.info/blog/1244#comments</comments>
		<pubDate>Tue, 19 Jul 2011 17:18:13 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1244</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1244" title="ATLENSURE_SUCCEEDED double failure"></a>A colleague pointed out that code snippet in previous post is misusing ATL&#8217;s ATLENSURE_SUCCEEDED macro making it [possibly] evaluate its argument twice in case of failure, that is evaluating into failure HRESULT code. As it is defined like this: #define &#8230;<p class="read-more"><a href="http://alax.info/blog/1244">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1244" title="ATLENSURE_SUCCEEDED double failure"></a><p>A colleague pointed out that code snippet in <a href="http://alax.info/blog/1241">previous post</a> is misusing ATL&#8217;s ATLENSURE_SUCCEEDED macro making it [possibly] evaluate its argument twice in case of failure, that is evaluating into failure HRESULT code. As it is defined like this:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #004a43;">#</span><span style="color: #004a43;">define</span><span style="color: #004a43;"> ATLENSURE_SUCCEEDED</span><span style="color: #808030;">(</span><span style="color: #004a43;">hr</span><span style="color: #808030;">)</span><span style="color: #004a43;"> ATLENSURE_THROW</span><span style="color: #808030;">(</span><span style="color: #004a43;">SUCCEEDED</span><span style="color: #808030;">(</span><span style="color: #004a43;">hr</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span><span style="color: #004a43;"> hr</span><span style="color: #808030;">)</span></pre>
<p>It does things in a straightforward way, for a code line</p>
<pre style="color: #000000; background: #ffffff;">ATLENSURE_SUCCEEDED<span style="color: #808030;">(</span>pFilterGraph<span style="color: #808030;">.</span>CoCreateInstance<span style="color: #808030;">(</span>CLSID_FilterGraph<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>It is doing &#8220;let&#8217;s CoCreateInstance the thing and if it fails, let&#8217;s CoCreateInstance it again to find out error code&#8221;. Disassembly shows this clearly:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0031.png"><img class="alignnone size-full wp-image-1245" title="ATLENSURE_SUCCEEDED evaluates the argument twice" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0031.png" alt="" width="640" height="458" /></a></p>
<p>This is exactly another spin of the story previously happened with <a href="http://msdn.microsoft.com/en-us/library/ms680746%28VS.85%29.aspx">HRESULT_FROM_WIN32</a> macro and possibly a number of others. With it being originally a macro, SDK offered an option to override the definition by pre-defining INLINE_HRESULT_FROM_WIN32. This way a user might be explicitly requesting a safer definition while still leaving legacy code live with macro. See <a href="http://blogs.msdn.com/b/matthew_van_eerde/archive/2007/12/28/the-evolution-of-hresult-from-win32.aspx">more detailed story on this in Matthew&#8217;s blog</a>.</p>
<p>A tricky thing is that with successful execution the problem does not come up. In case of failure, it depends on the functions called, some with just repeat the error code, some will return a different code on second run, some might create less desired and expected consequences. So you can find yourself having written quite some code before you even suspect a problem.</p>
<p>Having identified the issue, there are a few solutions.</p>
<p>1. First of all, the original ATLENSURE_SUCCEEDED macro can still be used, provided that you don&#8217;t put expressions as arguments.</p>
<p>This is going to do just fine:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">const</span> HRESULT nCoCreateInstanceResult <span style="color: #808030;">=</span> pFilterGraph<span style="color: #808030;">.</span>CoCreateInstance<span style="color: #808030;">(</span>CLSID_FilterGraph<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
ATLENSURE_SUCCEEDED<span style="color: #808030;">(</span>nCoCreateInstanceResult<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>2. Second straightforward way is to replace the original ATL definition in ATL code (boo, woodenly)</p>
<p>3. As ATL code is checking for the macros to be already defined, and skipping its own definition in such case, it is possible to inject a safer private definition before including ATL headers (which would typically need one to do the define in stdafx.h):</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #004a43;">#</span><span style="color: #004a43;">define</span><span style="color: #004a43;"> ATLENSURE_SUCCEEDED</span><span style="color: #808030;">(</span><span style="color: #004a43;">x</span><span style="color: #808030;">)</span><span style="color: #808030;">{</span><span style="color: #004a43;"> const HRESULT nResult </span><span style="color: #808030;">=</span><span style="color: #808030;">(</span><span style="color: #004a43;">x</span><span style="color: #808030;">)</span><span style="color: #808030;">;</span><span style="color: #004a43;"> ATLENSURE_THROW</span><span style="color: #808030;">(</span><span style="color: #004a43;">SUCCEEDED</span><span style="color: #808030;">(</span><span style="color: #004a43;">nResult</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span><span style="color: #004a43;"> nResult</span><span style="color: #808030;">)</span><span style="color: #808030;">; </span><span style="color: #808030;">}</span>

<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: #800000;">&lt;</span><span style="color: #40015a;">atlbase.h</span><span style="color: #800000;">&gt;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: #800000;">&lt;</span><span style="color: #40015a;">atlstr.h</span><span style="color: #800000;">&gt;</span></pre>
<p>Pre-evaluating the argument into local variable is going to resolve the original multi-evaluation problem.</p>
<p>4. There might be a new inline function defined on top of the original macro, which will be used instead and which is free from the problem:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">inline</span> <span style="color: #603000;">VOID</span> ATLENSURE_INLINE_SUCCEEDED<span style="color: #808030;">(</span>HRESULT nResult<span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    ATLENSURE_SUCCEEDED<span style="color: #808030;">(</span>nResult<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span></pre>
<p>Either way, the correct code compiles into single argument evaluation and throws an exception with failure code immediately:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0041.png"><img class="alignnone size-full wp-image-1246" title="Corrected ATLENSURE_SUCCEEDED code" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0041.png" alt="" width="640" height="333" /></a></p>
<p>Also, <del datetime="2011-07-20T05:21:51+00:00"><a href="https://connect.microsoft.com/VisualStudio/feedback/details/679899/atlensure-succeeded-macro-is-unsafe-for-use-with-evaluatable-arguments#details">vote for the suggestion on Microsoft Connect</a></del>. The issue is marked as fixed in future version of Visual Studio.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1244/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LogProcessExceptions: Minidumps on User Request</title>
		<link>http://alax.info/blog/1248</link>
		<comments>http://alax.info/blog/1248#comments</comments>
		<pubDate>Tue, 19 Jul 2011 14:20:57 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.DMP]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[minidump]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1248</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1248" title="LogProcessExceptions: Minidumps on User Request"></a>An updated version of LogProcessExceptions utility is given an additional option to create minidump .DMP files for debugged process on user request. This is in particular useful in conjunction with flag choices (on the previous page of the wizard). Partial &#8230;<p class="read-more"><a href="http://alax.info/blog/1248">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1248" title="LogProcessExceptions: Minidumps on User Request"></a><p>An updated version of <a href="http://alax.info/blog/1211">LogProcessExceptions</a> utility is given an additional option to create <a href="http://msdn.microsoft.com/en-us/library/d5zhxt22.aspx">minidump .DMP files</a> for debugged process on user request. This is in particular useful in conjunction with flag choices (on the previous page of the wizard).</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0032.png"><img class="alignnone size-full wp-image-1249" title="Minidump Creation Link" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0032.png" alt="" width="513" height="400" /></a></p>
<p>Partial Visual C++ .NET 2010 source code is <a href="http://code.assembla.com/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions">available from SVN</a>, release binary included (<a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe?format=raw">Win32</a>, <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe?format=raw">x64</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process</title>
		<link>http://alax.info/blog/1211</link>
		<comments>http://alax.info/blog/1211#comments</comments>
		<pubDate>Wed, 18 May 2011 16:47:41 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.DMP]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[minidump]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1211</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1211" title="LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process"></a>LogProcessExceptions utility implements a very basic debugger which attaches (see DebugActiveProcess on MSDN) to a running process and monitors its exceptions. Once exception takes place the utility creates a minidump file for the process (see MiniDumpWriteDump on MSDN) so that &#8230;<p class="read-more"><a href="http://alax.info/blog/1211">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1211" title="LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process"></a><p>LogProcessExceptions utility implements a very basic debugger which attaches (see <a href="http://msdn.microsoft.com/en-us/library/ms679295%28VS.85%29.aspx">DebugActiveProcess</a> on MSDN) to a running process and monitors its exceptions. Once exception takes place the utility creates a minidump file for the process (see <a href="http://msdn.microsoft.com/en-us/library/ms680360%28VS.85%29.aspx">MiniDumpWriteDump</a> on MSDN) so that exception condition could be investigated off-site using debugger.</p>
<p>If you throw C++ exceptions in your C++ code in exceptional cases which indicate necessity to log the condition and possibly check it later, the utility will get the vital information for the application running at production location in environment without fully featured debugger (note that the utility is a simple download-and-run &#8220;wizard&#8221; style application, with no installation required), or will simply track the error letting the application continue execution without pretty much of an interruption.</p>
<p>The tool will prompt for debuggee process, and follow with minidump type choices and the debugging operation.</p>
<p><img class="alignnone size-full wp-image-1212" title="Log Process Exceptions - Processes" src="http://alax.info/blog/wp-content/uploads/2011/05/Image001.png" alt="" width="513" height="400" /></p>
<p><span id="more-1211"></span></p>
<p><img class="alignnone size-full wp-image-1213" title="Log Process Exceptions - Minidupm Type" src="http://alax.info/blog/wp-content/uploads/2011/05/Image0031.png" alt="" width="513" height="400" /> <img class="alignnone size-full wp-image-1214" title="Log Process Exceptions - Operation" src="http://alax.info/blog/wp-content/uploads/2011/05/Image0041.png" alt="" width="513" height="400" /></p>
<p>Minidump files are created automatically with the name file including:</p>
<ul>
<li>Excecutable Name</li>
<li>Process Identifier</li>
<li>Ordinal Number</li>
<li>Exception Code</li>
<li>C++/ATL Exception HRESULT Code (esp. for <a href="http://msdn.microsoft.com/en-us/library/9b1a94tx%28VS.80%29.aspx">CAtlException</a> exceptions)</li>
</ul>
<p>The utility is expected to work with Windows XP operating system and more recent.</p>
<p>Partial Visual C++ .NET 2010 source code is <a href="http://code.assembla.com/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions">available  from SVN</a>, release binary included (<a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe?format=raw">Win32</a>,  <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe?format=raw">x64</a>).</p>
<p>Artwork credits: <a href="http://cristoildiablo.deviantart.com/">http://cristoildiablo.deviantart.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1211/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Your ATL service C++ project might need some extra care after upgrade to Visual Studio 2010</title>
		<link>http://alax.info/blog/1198</link>
		<comments>http://alax.info/blog/1198#comments</comments>
		<pubDate>Wed, 11 May 2011 21:55:59 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[epic]]></category>
		<category><![CDATA[fail]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1198</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1198" title="Your ATL service C++ project might need some extra care after upgrade to Visual Studio 2010"></a>If you dare to convert your C++ ATL Service project created with an earlier version of Visual Studio to version 2010, as I recently did, you might find yourself surprised with why the hell the bloody thing does not work &#8230;<p class="read-more"><a href="http://alax.info/blog/1198">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1198" title="Your ATL service C++ project might need some extra care after upgrade to Visual Studio 2010"></a><p>If you dare to convert your C++ ATL Service project created with an earlier version of Visual Studio to version 2010, as I recently did, you might find yourself surprised with why the hell the bloody thing does not work anymore as a regular executable.</p>
<p>After passing compiler/linker and SDK update issues, which you possibly might have too, the started executable will stumble on ATL error/exception with CO_E_NOTINITIALIZED (0x800401F0 &#8220;CoInitialize has not been called.&#8221;). Luckily the error code is good enough for quickly locating the problem and the reason is that the one you trusted, that is ATL, introduced an small improvement which is good for running as service but it not initializing COM anymore if you run your .EXE in application mode.</p>
<p><a href="http://connect.microsoft.com/VisualStudio/feedback/details/582774/catlservicemodulet-winmain-coinitialize-not-called-800401f0">The bug is on MS Connect</a> since August 3, 2010 closed as if it is going to be fixed some time in future when the fix is propagated to end developers. If you are in a rush and would like to write some code before the event, here are the details.</p>
<p>Previously, COM was initialized right in module constructor, in CAtlExeModuleT. CAtlServiceModuleT class just inherited from there. Later on, someone smart decided that it was not so cool and moved initialization to a later point into CAtlExeModuleT::WinMain. Well, this makes sense as you might (a) end up not needing COM at all, or (b) you want to do some important things before even initializing COM.</p>
<p>Unfortunately, the fact that CAtlServiceModuleT is inherited and relies on base class was not paid too much attention. CAtlServiceModuleT is not getting COM initialization from constructor any longer, CAtlServiceModuleT::WinMain is overridden in full and does not receive initialization from new location either. So well, it does not receive it at all unless run as service, which code execution branch looks still heatlhy here and exhibits another issue later soon.</p>
<p>To resolve the problem, the fragment in CAtlServiceModuleT::Start needs the correction as shown below (within #pragma region):</p>
<pre style="color: #000000; background: #ffffff;">        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #800080;">::</span><span style="color: #400000;">StartServiceCtrlDispatcher</span><span style="color: #808030;">(</span>st<span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">)</span>
                m_status<span style="color: #808030;">.</span>dwWin32ExitCode <span style="color: #808030;">=</span> <span style="color: #400000;">GetLastError</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">return</span> m_status<span style="color: #808030;">.</span>dwWin32ExitCode<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>

        <span style="color: #696969;">// local server - call Run() directly, rather than</span>
        <span style="color: #696969;">// from ServiceMain()</span>
<span style="color: #004a43;">        #</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">region Run wrapped by InitializeCom/UninitializeCom</span>
        <span style="color: #696969;">// </span><span style="color: #ffffff; background: #808000;">FIX: See http://connect.microsoft.com/VisualStudio/feedback/details/582774/catlservicemodulet-winmain-coinitialize-not-called-800401f0</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">ifndef</span><span style="color: #004a43;"> _ATL_NO_COM_SUPPORT</span>
        HRESULT hr <span style="color: #808030;">=</span> E_FAIL<span style="color: #800080;">;</span>
        hr <span style="color: #808030;">=</span> T<span style="color: #800080;">::</span>InitializeCom<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>FAILED<span style="color: #808030;">(</span>hr<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #696969;">// Ignore RPC_E_CHANGED_MODE if CLR is loaded. Error is due to CLR initializing</span>
            <span style="color: #696969;">// COM and InitializeCOM trying to initialize COM with different flags.</span>
            <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>hr <span style="color: #808030;">!</span><span style="color: #808030;">=</span> RPC_E_CHANGED_MODE <span style="color: #808030;">|</span><span style="color: #808030;">|</span> <span style="color: #400000;">GetModuleHandle</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">Mscoree.dll</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">)</span>
                <span style="color: #800000; font-weight: bold;">return</span> hr<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span> <span style="color: #800000; font-weight: bold;">else</span>
            m_bComInitialized <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">true</span><span style="color: #800080;">;</span>
        m_status<span style="color: #808030;">.</span>dwWin32ExitCode <span style="color: #808030;">=</span> pT<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>Run<span style="color: #808030;">(</span>nShowCmd<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>m_bComInitialized<span style="color: #808030;">)</span>
            T<span style="color: #800080;">::</span>UninitializeCom<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">else</span>
        m_status<span style="color: #808030;">.</span>dwWin32ExitCode <span style="color: #808030;">=</span> pT<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>Run<span style="color: #808030;">(</span>nShowCmd<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">endif</span>
<span style="color: #004a43;">        #</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">endregion </span>

        <span style="color: #800000; font-weight: bold;">return</span> m_status<span style="color: #808030;">.</span>dwWin32ExitCode<span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span></pre>
<p>Going further from there, the introduced optimization also removed COM initialization from main process thread module Run function. Provided there earlier too through module constructor it is not longer there. So if you are doing something in application&#8217;s run when the application is set to run as service and is executed in application (where you might want to start application as a sort of a helper, or otherwise in specific mode), you need COM initialization there too.</p>
<p><span id="more-1198"></span>It might be something like this:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">class</span> CFooModule <span style="color: #800080;">:</span>
    <span style="color: #800000; font-weight: bold;">public</span> CAtlServiceModuleT<span style="color: #800080;">&lt;</span>CFooModule<span style="color: #800080;">&gt;</span>
<span style="color: #800080;">{</span>
<span style="color: #696969;">// [...]</span>
    HRESULT Run<span style="color: #808030;">(</span><span style="color: #603000;">INT</span> nShowCommand <span style="color: #808030;">=</span> SW_HIDE<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: #696969;">// [...]</span>
            <span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>m_bServiceHelper<span style="color: #808030;">)</span>
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// NOTE: Starting with Visual Studio 2010 service helper's Run receives no automatic COM initialization any longer</span>
                nResult <span style="color: #808030;">=</span> InitializeCom<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>SUCCEEDED<span style="color: #808030;">(</span>nResult<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
                <span style="color: #800080;">{</span>
                    nResult <span style="color: #808030;">=</span> __super<span style="color: #800080;">::</span>Run<span style="color: #808030;">(</span>nShowCommand<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                    UninitializeCom<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800080;">}</span>
            <span style="color: #800080;">}</span> <span style="color: #800000; font-weight: bold;">else</span>
                nResult <span style="color: #808030;">=</span> __super<span style="color: #800080;">::</span>Run<span style="color: #808030;">(</span>nShowCommand<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #696969;">// [...]</span>
    <span style="color: #800080;">}</span>
<span style="color: #696969;">// [...]</span>
<span style="color: #800080;">}</span><span style="color: #800080;">;</span></pre>
<p>Having done that we are really closer but there is still another breaking bug in ATL caused by Visual Studio 2010 changes. You start a service and its endlessly shown as being started.</p>
<p>It appears that ATL is failing to call <a href="http://msdn.microsoft.com/en-us/library/ms686241%28VS.85%29.aspx">SetServiceStatus</a>(SERVICE_RUNNING) to indicated startup completion and service healthy state. Again, the bug is related to change in base class behavior. This time, the problem is caused by the fact that SetServiceStatus call was moved from CAtlServiceModuleT::Run into CAtlServiceModuleT::PreMessageLoop. However, it was moved into the section conditionally compiled with definition of _ATL_FREE_THREADED. If you are and old school guy with _ATL_APARTMENT_THREADED instead and is just converting and older project that worked before, you have a problem here again: you have to add missing call.</p>
<p>To fix this, the end of CAtlServiceModuleT::PreMessageLoop should be looking like this:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #004a43;">#</span><span style="color: #004a43;">else</span>
        hr <span style="color: #808030;">=</span> CAtlExeModuleT<span style="color: #800080;">&lt;</span>T<span style="color: #800080;">&gt;</span><span style="color: #800080;">::</span>PreMessageLoop<span style="color: #808030;">(</span>nShowCmd<span style="color: #808030;">)</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 Missing SetServiceStatus</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>m_bService<span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            LogEvent<span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">Service started/resumed</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #400000;">SetServiceStatus</span><span style="color: #808030;">(</span>SERVICE_RUNNING<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>
<span style="color: #004a43;">#</span><span style="color: #004a43;">endif</span><span style="color: #004a43;"> </span><span style="color: #696969;">// _ATL_FREE_THREADED</span>

<span style="color: #004a43;">#</span><span style="color: #004a43;">endif</span><span style="color: #004a43;">    </span><span style="color: #696969;">// _ATL_NO_COM_SUPPORT</span>

        ATLASSERT<span style="color: #808030;">(</span>SUCCEEDED<span style="color: #808030;">(</span>hr<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">return</span> hr<span style="color: #800080;">;</span></pre>
<p>Well, this is embarrassing.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1198/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utility Clearance: Rasterize Font</title>
		<link>http://alax.info/blog/1192</link>
		<comments>http://alax.info/blog/1192#comments</comments>
		<pubDate>Sun, 24 Apr 2011 17:05:32 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[character]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[rasterize]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[ttf]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[WTL]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1192</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1192" title="Utility Clearance: Rasterize Font"></a>RasterizeFont utility takes a font on the input (such as Windows .TTF &#8211; True-Type Font) and paints individual characters into bitmaps. Utility output includes separate bitmap (.BMP) files for requested characters and C++ source code of the bimap arrays (this &#8230;<p class="read-more"><a href="http://alax.info/blog/1192">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1192" title="Utility Clearance: Rasterize Font"></a><p><img class="size-full wp-image-1193 alignleft" title="RasterizeFont Output" src="http://alax.info/blog/wp-content/uploads/2011/04/Character-005a.png" alt="" width="48" height="72" /> RasterizeFont utility takes a font on the input (such as Windows .TTF &#8211; <a href="http://en.wikipedia.org/wiki/TrueType">True-Type Font</a>) and paints individual characters into bitmaps. Utility output includes separate bitmap (.BMP) files for requested characters and C++ source code of the bimap arrays (this was included into microcontroller project).</p>
<p>A configuration .INI file defines rasterizer parameters:</p>
<pre>[General]
Width=48
Height=72
Horizontal Adjustment=1
Vertical Adjustment=0
Outline=1

[Font]
Face=Times New Roman
Height=48
Weight=1024

[Bitmaps]
PathTemplate=Character-%04x.bmp</pre>
<p>And a list of characters of interest is passed as a command line argument.</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">const</span> <span style="color: #603000;">BYTE</span> pnCharacter007a<span style="color: #808030;">[</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #696969;">// 0x007a z</span>
<span style="color: #800080;">{</span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 01 01 01 01 01 01 01 01 </span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 01 01 01 01 01 01 01 01 </span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 01 01 01 01 01 01 01 01 </span>
    <span style="color: #008000;">0x54</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x00</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x05</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 00 00 00 00 00 00 00 01 01 </span>
    <span style="color: #008000;">0x52</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xaa</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xa1</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 00 10 10 10 10 10 10 10 00 01 </span>
    <span style="color: #008000;">0x52</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x00</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xa1</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 00 10 00 00 00 00 10 10 00 01 </span>
    <span style="color: #008000;">0x54</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x42</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x85</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 00 01 00 00 10 10 00 01 01 </span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x2a</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x15</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 00 10 10 10 00 01 01 01 </span>
    <span style="color: #008000;">0x54</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xa0</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x45</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 00 10 10 00 00 01 00 01 01 </span>
    <span style="color: #008000;">0x52</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x80</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x21</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 00 10 10 00 00 00 00 10 00 01 </span>
    <span style="color: #008000;">0x52</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xaa</span><span style="color: #808030;">,</span> <span style="color: #008000;">0xa1</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 00 10 10 10 10 10 10 10 00 01 </span>
    <span style="color: #008000;">0x54</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x00</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x05</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 00 00 00 00 00 00 00 01 01 </span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 01 01 01 01 01 01 01 01 </span>
    <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span> <span style="color: #008000;">0x55</span><span style="color: #808030;">,</span>  <span style="color: #696969;">// 01 01 01 01 01 01 01 01 01 01 01 01 </span>
<span style="color: #800080;">}</span><span style="color: #800080;">;</span></pre>
<p>A binary [<a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/RasterizeFont/_Bin/Win32/Release/RasterizeFont.exe?format=raw">Win32</a>] and Visual C++ .NET 2008 source code <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/RasterizeFont">are  available from SVN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1192/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Utility Clearance: Export AVI Resources</title>
		<link>http://alax.info/blog/1190</link>
		<comments>http://alax.info/blog/1190#comments</comments>
		<pubDate>Sun, 24 Apr 2011 16:10:58 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[AVI]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1190</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1190" title="Utility Clearance: Export AVI Resources"></a>ExportAviResources walks through AVI video clips attached as resources to a binary file and exports them into separate files. Such clips can be used with Animation Controls for GUI animations. You might want to run the utility against SYSTEM32/SYSWOW64 folders &#8230;<p class="read-more"><a href="http://alax.info/blog/1190">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1190" title="Utility Clearance: Export AVI Resources"></a><p>ExportAviResources walks through AVI video clips attached as resources to a binary file and exports them into separate files.</p>
<p>Such clips can be used with <a href="http://msdn.microsoft.com/en-us/library/bb761881%28VS.85%29.aspx">Animation Controls</a> for GUI animations. You might want to run the utility against SYSTEM32/SYSWOW64 folders to see if any of stock animations are good for you:</p>
<pre>D:\&gt;for %i in (C:\Windows\system32\*.dll) do "..\Utilities\ExportAviResources\x64\Release\ExportAviResources.exe" "%i"</pre>
<p>A binary [<a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/ExportAviResources/_Bin/Win32/Release/ExportAviResources.exe?format=raw">Win32</a>, <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/ExportAviResources/_Bin/x64/Release/ExportAviResources.exe?format=raw">x64</a>] and Visual C++ .NET 2010 source code <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/ExportAviResources">are  available from SVN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1190/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utility Clearance: Logical Processor Information</title>
		<link>http://alax.info/blog/1188</link>
		<comments>http://alax.info/blog/1188#comments</comments>
		<pubDate>Sun, 24 Apr 2011 15:45:16 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[GetLogicalProcessorInformation]]></category>
		<category><![CDATA[msdn]]></category>
		<category><![CDATA[processor]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[WTL]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1188</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1188" title="Utility Clearance: Logical Processor Information"></a>LogicalProcessorInformation is a fronend GUI around GetLogicalProcessorInformation API and reveals CPU configuration of the system. If you are fine tuning stuff, you might want to know what sort of CPUs are powering the applications: how many? fully featured cores or &#8230;<p class="read-more"><a href="http://alax.info/blog/1188">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1188" title="Utility Clearance: Logical Processor Information"></a><p>LogicalProcessorInformation is a fronend GUI around <a href="http://msdn.microsoft.com/en-us/library/ms683194%28VS.85%29.aspx">GetLogicalProcessorInformation</a> API and reveals CPU configuration of the system. If you are fine tuning stuff, you might want to know what sort of CPUs are powering the applications:</p>
<ul>
<li>how many?</li>
<li>fully featured cores or <a href="http://www.intel.com/info/hyperthreading/">HyperThreading</a> technology?</li>
<li>mutli-processor configuration and how exactly physical processors are distributed over affinity mask</li>
</ul>
<p><img class="alignnone size-full wp-image-1189" title="Logical Processor Information" src="http://alax.info/blog/wp-content/uploads/2011/04/Image001.png" alt="" width="520" height="396" /></p>
<p>API and the application gets you the data as a user-friendly dump of GetLogicalProcessorInformation output and a summary of records at the bottom:</p>
<p><span id="more-1188"></span></p>
<pre>Logical Processors:

Mask: 0x00000003
Relationship: RelationProcessorCore (0x0)
ProcessorCore.Flags: 0x01

Mask: 0x00000003
Relationship: RelationCache (0x2)
Cache.Level: 1
Cache.Associativity: 8
Cache.LineSize: 64 (0x40)
Cache.Size: 32768 (0x8000)
Cache.Type: CacheData (0x2)

Mask: 0x00000003
Relationship: RelationCache (0x2)
Cache.Level: 1
Cache.Associativity: 4
Cache.LineSize: 64 (0x40)
Cache.Size: 32768 (0x8000)
Cache.Type: CacheInstruction (0x1)

Mask: 0x00000003
Relationship: RelationCache (0x2)
Cache.Level: 2
Cache.Associativity: 8
Cache.LineSize: 64 (0x40)
Cache.Size: 262144 (0x40000)
Cache.Type: CacheUnified (0x0)

Mask: 0x0000000c
Relationship: RelationProcessorCore (0x0)
ProcessorCore.Flags: 0x01

...

Mask: 0x000000ff
Relationship: RelationProcessorPackage (0x3)

...

Mask: 0x000000ff
Relationship: RelationNumaNode (0x1)
NumaNode.NodeNumber: 0x0

Record Count per Relationship:
 RelationProcessorCore (0x0): 4
 RelationNumaNode (0x1): 1
 RelationCache (0x2): 13
 RelationProcessorPackage (0x3): 1
 RelationGroup (0x4): 0</pre>
<p>A binary [<a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/LogicalProcessorInformation/_Bin/Win32/Release/LogicalProcessorInformation.exe?format=raw">Win32</a>, <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/LogicalProcessorInformation/_Bin/x64/Release/LogicalProcessorInformation.exe?format=raw">x64</a>] and Visual C++ .NET 2010 source code <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/LogicalProcessorInformation">are  available from SVN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1188/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Booo SRW Locks</title>
		<link>http://alax.info/blog/1185</link>
		<comments>http://alax.info/blog/1185#comments</comments>
		<pubDate>Sun, 27 Mar 2011 16:13:04 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[slim]]></category>
		<category><![CDATA[SRW]]></category>
		<category><![CDATA[synchronization]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1185</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1185" title="Booo SRW Locks"></a>Windows Vista introduced new synchronization API: slim reader/writer (SRW) locks. Being already armed with critical sections, one perhaps would not desperately need an SRW lock, but still it offers a great option to provide both exclusive (critical section alike) mode &#8230;<p class="read-more"><a href="http://alax.info/blog/1185">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1185" title="Booo SRW Locks"></a><p>Windows Vista introduced new synchronization API: slim reader/writer (SRW) locks. Being already armed with critical sections, one perhaps would not desperately need an SRW lock, but still it offers a great option to provide both exclusive (critical section alike) mode and shared mode when 2+ threads can enter protected section simultaneously. Some time earlier, I already touched <a href="http://alax.info/blog/1163">SRW lock recursion issue</a>.</p>
<p>This time it is more about performance. Let us suppose we have a code fragment:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">static</span> <span style="color: #800000; font-weight: bold;">const</span> SIZE_T g_nCount <span style="color: #808030;">=</span> <span style="color: #008c00;">100000000</span><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> g_nCount<span style="color: #800080;">;</span> nIndex<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    AcquireSRWLockShared<span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>m_Lock<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    ReleaseSRWLockShared<span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>m_Lock<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span></pre>
<p>How fast is this? Provided that execution took <strong>1.6 ms</strong> on an idle lock variable, how fast it is going to be with another shared &#8220;reader&#8221; on another thread who acquired once access in shared mode? This part comes up confusing: it appears almost twice as slow: <strong>2.9 ms</strong>, with also a number of contentions (and context switches) on the way.</p>
<p>SRW lock is advertised as lightweight and fast API, no recursion, no extra features, no fool proof checks. So it could be assumed to get you top performance, but 50 lines of code class can definitely outperform it.</p>
<p>A perhaps simplest SRW lock can be backed on a LONG (or LONGLONG) volatile variable accessed with <a href="http://msdn.microsoft.com/en-us/library/ms684122%28VS.85%29.aspx">interlocked API functions</a>. The rules are simple:</p>
<ul>
<li>initial value of zero means idle state</li>
<li>shared &#8220;reader&#8221; enters incrementing by one, positive values indicates one ore more readers</li>
<li>exclusive &#8220;writer&#8221; enters decrementing by a hundred (well, this needs to be any value big enough to be less than minus maximal amount of simultaneous concurrent readers)</li>
<li>releasing acquired state the value is decremented (incremented) back</li>
<li>in case of contention thread <a href="http://msdn.microsoft.com/en-us/library/ms686352%28VS.85%29.aspx">yields execution</a> to continue later with possibly better luck with shared resource (option: implement spic count for several attempts in a row, which is better suitable for multi-processor systems)</li>
</ul>
<p>It is clear that concurrent readers touch value only once with a single increment only, leaving no opportunity to yield execution due to contention. How fast it can be?</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">static</span> <span style="color: #800000; font-weight: bold;">const</span> SIZE_T g_nCount <span style="color: #808030;">=</span> <span style="color: #008c00;">100000000</span><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> g_nCount<span style="color: #800080;">;</span> nIndex<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #800000; font-weight: bold;">while</span><span style="color: #808030;">(</span><span style="color: #400000;">InterlockedIncrement</span><span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>m_nNativeLock<span style="color: #808030;">)</span> <span style="color: #808030;">&lt;</span> <span style="color: #008c00;">0</span><span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        <span style="color: #400000;">InterlockedDecrement</span><span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>m_nNativeLock<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #400000;">SwitchToThread</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>
    <span style="color: #400000;">InterlockedDecrement</span><span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>m_nNativeLock<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span></pre>
<p>It is <strong>1.3 ms</strong> regardless whether there are any shared readers on concurrent threads. It appears that a simple custom SRW lock class is going to be superior to the API:</p>
<ul>
<li>faster due to zero API overhead (inline compiled code versus WINAPI convention functions)</li>
<li>faster in shared mode due to not being subject to additional overhead and contentions</li>
<li>flexible spin counts possible to tune performance for specific use</li>
<li>extensibility:
<ul>
<li>recursion is allowed shared mode</li>
<li>easy to implement upgrades and downgrades between shared and exclusive modes</li>
</ul>
</li>
</ul>
<p>Sample code is Visual Studio 2010 C++ project accessible from <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/SrwLockTest02">SVN repository</a>.</p>
<p><strong>Update</strong>: Critical Section Test. This is how SRW lock API performance compares to entry/leaving critical section (provided that critical section is never locked at entry time). Critical section is about 15% slower to enter and leave.</p>
<pre>C:\Projects\...\SrwLockTest02\x64\Release&gt;SrwLockTest02.exe
API: 100M iterations, 1575 ms
API: 100M iterations, 2839 ms
Native: 100M iterations, 1311 ms
Native: 100M iterations, 1326 ms
<span style="text-decoration: underline;">Critical Section: 100M iterations, 1841 ms</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1185/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

