<?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; Seriously</title>
	<atom:link href="http://alax.info/blog/category/seriously/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>LogProcessExceptions: Log Service Process Exceptions</title>
		<link>http://alax.info/blog/1359</link>
		<comments>http://alax.info/blog/1359#comments</comments>
		<pubDate>Sun, 29 Apr 2012 19:10:59 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[WTL]]></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[service]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1359</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1359" title="LogProcessExceptions: Log Service Process Exceptions"></a>One of the nasty issues with LogProcessExceptions utility was that it was unable to attach to service processes and track them to catch their exceptions. The actual problem was that the processes were not listed in first place, so there &#8230;<p class="read-more"><a href="http://alax.info/blog/1359">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1359" title="LogProcessExceptions: Log Service Process Exceptions"></a><p>One of the nasty issues with <a href="http://alax.info/blog/1211">LogProcessExceptions</a> utility was that it was unable to attach to service processes and track them to catch their exceptions.</p>
<p>The actual problem was that the processes were not listed in first place, so there was nothing to attach to. Access and security requirements necessary for a process to debug another process are listed in MSDN <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms679295%28v=vs.85%29.aspx"><code>DebugActiveProcess</code></a> article:</p>
<blockquote><p>The debugger must have appropriate access to the target process, and it must be able to open the process for <code>PROCESS_ALL_ACCESS</code>. <code>DebugActiveProcess</code> can fail if the target process is created with a security descriptor that grants the debugger anything less than full access. If the debugging process has the <code>SE_DEBUG_NAME</code> privilege granted and enabled, it can debug any process.</p></blockquote>
<p>The utility did enable the <code>SE_DEBUG_NAME</code> privilege, however it was doing it prior to starting debugging session and after the process of interest was already pointed to by user.</p>
<p>This was insufficient because <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682629%28v=vs.85%29.aspx"><code>EnumProcesses</code></a> only lists service processes (not actually exactly services, but processes running in different security context) in case debug privilege is already enable by the time of the API call. The utility now enabled the privilege well in advance and list the services, so can be effectively applied to those.</p>
<p><img class="alignnone size-full wp-image-1360" title="LogProcessExceptions - Process List" src="http://alax.info/blog/wp-content/uploads/2012/04/Image0011.png" alt="" width="513" height="400" /></p>
<p>Download links:</p>
<ul>
<li>Partial Visual C++ .NET 2010 source code: <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/LogProcessExceptions">Trac</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/">Subversion</a></li>
<li>Binaries: Win32 – <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a>, x64 – <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1359/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oops, AMAP_3D_TARGET</title>
		<link>http://alax.info/blog/1335</link>
		<comments>http://alax.info/blog/1335#comments</comments>
		<pubDate>Mon, 13 Feb 2012 18:43:44 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[DirectDraw]]></category>
		<category><![CDATA[DirectShow]]></category>
		<category><![CDATA[mixing]]></category>
		<category><![CDATA[renderer]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1335</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1335" title="Oops, AMAP_3D_TARGET"></a>Unfortunately, AMAP_3D_TARGET appears to be useless. Internally, surface allocation comes up with the following capabilities for the surface: DDSCAPS_VIDEOMEMORY &#124; DDSCAPS_LOCALVIDMEM &#124; DDSCAPS_OFFSCREENPLAIN &#124; DDSCAPS_3DDEVICE. Sadly, DirectDraw responds (might respond?) with E_NOTIMPL. While one can advance one step further by &#8230;<p class="read-more"><a href="http://alax.info/blog/1335">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1335" title="Oops, AMAP_3D_TARGET"></a><p>Unfortunately, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd407394%28v=vs.85%29.aspx">AMAP_3D_TARGET</a> appears to be useless. Internally, surface allocation comes up with the following capabilities for the surface: <strong>DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE</strong>. Sadly, DirectDraw responds (might respond?) with <strong>E_NOTIMPL</strong>.</p>
<p>While one can advance one step further by removing <strong>DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM</strong>, another problem is hit that you are no longer able to create a surface with is both FourCC enabled and in the same time has 3D rendering capabilities. Remove your YUV four character code (such as NV12, YV12, YUY2 you would normally have as codec&#8217;s output) or fail miserably with <strong>DDERR_INVALIDPIXELFORMAT</strong>.</p>
<p>Back to the original problem, the <strong>AMAP_3D_TARGET</strong> flag failing to work makes it impossible to allocate 3D-enabled DirectDraw surface with DirectShow <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd407343%28v=vs.85%29.aspx">Video Mixing Renderer</a>. While it might sound as deprecated technology, it is yet a supported Windows SDK component, the most efficient video rendering component, the least hardware sensitive video rendering component, and &#8211; the most immediately important &#8211; the only way an SDK I am working with can deliver its video overlay.</p>
<p>So, there is no easy way to request a 3D enabled surface through a custom allocator-presenter, with a hook in the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd390521%28v=vs.85%29.aspx">IVMRSurfaceAllocator::AllocateSurface</a> updating the allocation flags. It does not make much sense to replace the whole allocator-presenter either: while an earlier DirectShow/Windows SDK provides a decent base for custom allocator-presenter, it is going to eventually hit the same problems mentioned above. And you cannot blit from 3D surface into a plain offscreen surface either without getting <strong>E_FAIL</strong>.</p>
<p>What appears to still be possible is allocating an addition 3D enabled surface, such as within <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd390446%28v=vs.85%29.aspx">IVMRImagePresenter::StartPresenting</a> and using it as a replacement surface within <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd390445%28v=vs.85%29.aspx">IVMRImagePresenter::PresentImage</a>. Having a custom allocator-presenter receive video frame, you can blit the picture into 3D-enabled surface, do your thing and pass the updated <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd407392%28v=vs.85%29.aspx">VMRPRESENTATIONINFO</a> to default allocator-presenter so that it presents the additional update surface, not the original one.</p>
<p><span id="more-1335"></span></p>
<p><img class="alignnone size-full wp-image-1336" title="3D-enabled DirectDraw Surface with a Video Mixing Renderer through Custom Allocator-Presenter" src="http://alax.info/blog/wp-content/uploads/2012/02/Image001.png" alt="" width="576" height="423" /></p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1335/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>The copyright violation vanguard</title>
		<link>http://alax.info/blog/1267</link>
		<comments>http://alax.info/blog/1267#comments</comments>
		<pubDate>Sat, 30 Jul 2011 21:10:59 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[piracy]]></category>
		<category><![CDATA[Russian]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1267</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1267" title="The copyright violation vanguard"></a>Below is the chart for distribution of software piracy cases for product X by workstation locale. Russia (more exactly, Russian speaking community) is not the primary market for the product, nor it is the place where it is specifically popular &#8230;<p class="read-more"><a href="http://alax.info/blog/1267">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1267" title="The copyright violation vanguard"></a><p>Below is the chart for distribution of software piracy cases for product X by workstation locale. Russia (more exactly, Russian speaking community) is not the primary market for the product, nor it is the place where it is specifically popular or well-known for a reason.</p>
<p>Welcome to the land of the brave, and guys who don&#8217;t pay. Low life standards, unemployment across educated people, neglect for copyright and intellectual property &#8211; all together make the leader of the world.</p>
<p><img class="aligncenter size-full wp-image-1268" title="Violations by Locale" src="http://alax.info/blog/wp-content/uploads/2011/07/Chart1.png" alt="" width="591" height="386" /></p>
<p>Update: same source data against <a href="http://www.maxmind.com/app/geolitecity">MaxMind&#8217;s GeoLiteCity database</a> and geolocation IP-to-country lookup:</p>
<p><img class="aligncenter size-full wp-image-1269" title="Violations per Country" src="http://alax.info/blog/wp-content/uploads/2011/07/Chart2.png" alt="" width="591" height="386" /></p>
<p>Russia, Argentina, Iran and Ukraine &#8211; the four attribute to almost 60% of violations (identified by straightforward search, which includes mostly keygen-based installations). The rest of the countries have shares under 5%.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1267/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A tricky EVR bug was caught up: input pin may falsely report disconnected state</title>
		<link>http://alax.info/blog/1262</link>
		<comments>http://alax.info/blog/1262#comments</comments>
		<pubDate>Tue, 26 Jul 2011 17:13:38 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[evr]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1262</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1262" title="A tricky EVR bug was caught up: input pin may falsely report disconnected state"></a>Crime An application which builds a DirectShow graph unexpectedly started failing with VFW_E_NOT_CONNECTED (0&#215;80040209) error code. Scene The problem takes place during DirectShow graph building, yet in stopped state. Specific call which appeared to be giving out the error in &#8230;<p class="read-more"><a href="http://alax.info/blog/1262">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1262" title="A tricky EVR bug was caught up: input pin may falsely report disconnected state"></a><h4>Crime</h4>
<p>An application which builds a DirectShow graph unexpectedly started failing with VFW_E_NOT_CONNECTED (0&#215;80040209) error code.</p>
<h4>Scene</h4>
<p>The problem takes place during DirectShow graph building, yet in stopped state. Specific call which appeared to be giving out the error in first place appears to be EVR input pin&#8217;s <a href="http://connect.microsoft.com/VisualStudio/feedback/details/680766/enhanced-video-renderer-is-incorrectly-reporting-vfw-e-not-connected-0x80040209-for-a-connected-pin">IPin::ConnectionMediaType</a>, and the problem is also specific to <a href="http://msdn.microsoft.com/en-us/library/ms694916%28VS.85%29.aspx">Enhanced video Renderer</a> (Windows 7, but not necessarily only this version).</p>
<h4>Investigation</h4>
<p>The problem does not appear to be persistent. On the contrary, it is taking place for just a few milliseconds after pin connection. After the problem is gone, it does not seem to ever come up again unless the filter graph is built again from the beginning.</p>
<p>EVR pin connection is always reporting success, so the following error code stating <a href="http://msdn.microsoft.com/en-us/library/ms919477.aspx">VFW_E_NOT_CONNECTED</a> &#8220;The operation cannot be performed because the pins are not connected.&#8221; goes against documented behavior, and is thus a bug.</p>
<p>Depending on time between pin connection and media type polling, the call can reach EVR:</p>
<ul>
<li>before it starts showing the problem &#8211; stage A</li>
<li>at the time the call fails &#8211; stage B</li>
<li>after the failure time interval, when the call is successful from then on &#8211; stage C</li>
</ul>
<p>Thus, the problem is limited to specific use cases:</p>
<ul>
<li>the application should care about media type on EVR input</li>
<li>unexpected failure takes place when the call reaches in stage B</li>
<li>also found: the clipping window for the EVR has to belong to a non-primary monitor</li>
</ul>
<p>If an application keep polling for media type in a loop, the result may be about the following:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #603000;">UINT</span> nStageA <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">,</span> nStageB <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">,</span> nStageC <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
<span style="color: #696969;">// [...]</span>
<span style="color: #800000; font-weight: bold;">for</span><span style="color: #808030;">(</span><span style="color: #800080;">;</span> <span style="color: #800080;">;</span> <span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    AM_MEDIA_TYPE MediaType<span style="color: #800080;">;</span>
    <span style="color: #400000;">ZeroMemory</span><span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>MediaType<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">sizeof</span> MediaType<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">const</span> HRESULT nConnectionMediaTypeResult <span style="color: #808030;">=</span> pInputPin<span style="color: #808030;">-</span><span style="color: #808030;">&gt;</span>ConnectionMediaType<span style="color: #808030;">(</span><span style="color: #808030;">&amp;</span>MediaType<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>nConnectionMediaTypeResult<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>nStageB<span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            nStageC<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">break</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span> <span style="color: #800000; font-weight: bold;">else</span>
            nStageA<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span> <span style="color: #800000; font-weight: bold;">else</span>
    <span style="color: #800080;">{</span>
        ATLASSERT<span style="color: #808030;">(</span>nConnectionMediaTypeResult <span style="color: #808030;">=</span><span style="color: #808030;">=</span> VFW_E_NOT_CONNECTED<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        nStageB<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>
    CoTaskMemFree<span style="color: #808030;">(</span>MediaType<span style="color: #808030;">.</span>pbFormat<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>
<span style="color: #696969;">// [...]</span>
<span style="color: #603000;">CString</span> sMessage<span style="color: #800080;">;</span>
sMessage<span style="color: #808030;">.</span>Format<span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">Bingo!</span><span style="color: #0f69ff;">\r</span><span style="color: #0f69ff;">\n</span><span style="color: #0f69ff;">\r</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span> _T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">nStageA </span><span style="color: #0f69ff;">%d</span><span style="color: #0000e6;">, nStageB </span><span style="color: #0f69ff;">%d</span><span style="color: #0000e6;"> - 0x</span><span style="color: #0f69ff;">%08x</span><span style="color: #0000e6;">, nStageC </span><span style="color: #0f69ff;">%d</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> nStageA<span style="color: #808030;">,</span> nStageB<span style="color: #808030;">,</span> nResult<span style="color: #808030;">,</span> nStageC<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
AtlMessageBox<span style="color: #808030;">(</span>m_hWnd<span style="color: #808030;">,</span> <span style="color: #808030;">(</span><span style="color: #603000;">LPCTSTR</span><span style="color: #808030;">)</span> sMessage<span style="color: #808030;">,</span> _T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">Result</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> MB_ICONERROR<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p><img class="alignnone size-full wp-image-1263" title="EVR Input Pin IPin::ConnectedMediaType Stages" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0034.png" alt="" width="384" height="184" /></p>
<h5>Workaround</h5>
<p>An obvious straightforward workaround is to follow EVR connection with a wait for Stage B to pass, or timeout &#8211; whichever takes place first.</p>
<p>Also, <a href="http://connect.microsoft.com/VisualStudio/feedback/details/680766/enhanced-video-renderer-is-incorrectly-reporting-vfw-e-not-connected-0x80040209-for-a-connected-pin">vote for the bug on Microsoft Connect</a>.</p>
<h5>More Details</h5>
<p>Video renderer filter are notorious for re-agreeing media type and being fretful as for memory allocators and media types (for a good reason though!). So it makes sense to suggest that the problem takes place when the filter is doing something related, such as it starts background activity immediately after connection in order to discover upstream peer capabilities.</p>
<p>In order to possibly get details on this, it is possible to raise an exception as soon as Stage B is detected and take a look at thread states using a debugger. Indeed, on of the background threads is engaged in EVR reconnection activity:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0023.png"><img class="alignnone size-large wp-image-1264" title="EVR Background Thread State" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0023-800x429.png" alt="" width="620" height="332" /></a></p>
<p>Yes it does the reconnection, but nevertheless it is expected to do the things undercover and transparently, it still allows a failure on the outer API.</p>
<pre>     evr.dll!GetSourceRectFromMediaType() + 0x37 bytes    
     evr.dll!CEVRInputPin::CheckMediaType() + 0x81 bytes    
     evr.dll!CBasePin::ReceiveConnection() + 0x61 bytes    
     evr.dll!CEVRInputPin::ReceiveConnection() + 0x1fc2d bytes    
     quartz.dll!CBasePin::AttemptConnection() - 0x21 bytes    
     quartz.dll!CBasePin::TryMediaTypes() + 0x60 bytes    
     quartz.dll!CBasePin::AgreeMediaType() + 0x54 bytes    
     quartz.dll!CBasePin::Connect() + 0x46 bytes    
     quartz.dll!CFilterGraph::ConnectDirectInternal() + 0x83 bytes    
     quartz.dll!CFilterGraph::ConnectRecursively() + 0x2c bytes    
     quartz.dll!CFilterGraph::ConnectInternal() + 0xde bytes    
     quartz.dll!CFilterGraph::Connect() + 0x17 bytes    
     quartz.dll!CFGControl::WorkerDisplayChanged() + 0xf1 bytes    
     quartz.dll!CFGControl::CGraphWindow::OnReceiveMessage() + 0x2e2a bytes    
&gt;    quartz.dll!WndProc() + 0x3e bytes    
     user32.dll!_InternalCallWinProc@20() + 0x23 bytes    
     user32.dll!_UserCallWinProcCheckWow@32() + 0xb7 bytes    
     user32.dll!_DispatchMessageWorker@8() + 0xed bytes    
     user32.dll!_DispatchMessageW@4() + 0xf bytes    
     quartz.dll!ObjectThread() + 0x65 bytes</pre>
<p>A test Visual C++ .NET 2010 application <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/EvrInputPinConnectionBug">is available from SVN</a>. The code requires a media file, and refers to <em>352&#215;288 I420.avi</em>, which is included into ZIP file attached to <a href="http://connect.microsoft.com/VisualStudio/feedback/details/680766/enhanced-video-renderer-is-incorrectly-reporting-vfw-e-not-connected-0x80040209-for-a-connected-pin#details">MS Connect Feedback</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1262/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another VFW/DirectShow Pain in the Ass: picapmk4.dll</title>
		<link>http://alax.info/blog/1250</link>
		<comments>http://alax.info/blog/1250#comments</comments>
		<pubDate>Thu, 21 Jul 2011 13:05:33 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[codec]]></category>
		<category><![CDATA[compatibility]]></category>
		<category><![CDATA[crapware]]></category>
		<category><![CDATA[DirectShow]]></category>
		<category><![CDATA[issue]]></category>
		<category><![CDATA[picapmk4]]></category>
		<category><![CDATA[vfw]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1250</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1250" title="Another VFW/DirectShow Pain in the Ass: picapmk4.dll"></a>A customer complained on video playback problems which had symptoms of software compatibility issue. At certain operation modes software froze leaving the only option to kill process and restart. I appeared that the system has a third party DLL installed, &#8230;<p class="read-more"><a href="http://alax.info/blog/1250">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1250" title="Another VFW/DirectShow Pain in the Ass: picapmk4.dll"></a><p>A customer complained on video playback problems which had symptoms of software compatibility issue. At certain operation modes software froze leaving the only option to kill process and restart.</p>
<p>I appeared that the system has a third party DLL installed, a new hero name&#8217;s &#8211; <strong>picapmk4.dll</strong>. The DLL registers itself as Video for Windows codec and is clearly indicating capabilities it does not really support.</p>
<p>The DLL itself has no indication of its source, no valid version info. Instead it has GPL v2 embedded, XviD logo and about box resource template showing up as &#8220;PMK4 Video Codec&#8221;.</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0011.png"><img class="alignnone size-full wp-image-1251" title="XviD logo" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0011.png" alt="" width="468" height="211" /></a></p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0021.png"><img class="alignnone size-full wp-image-1252" title="PMK4 Video Codec" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0021.png" alt="" width="467" height="371" /></a></p>
<p>Looks like crapware, which is hostile to DirectShow environment, with possibly GPL compliance issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1250/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). Download &#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>Download links:</p>
<ul>
<li>Partial Visual C++ .NET 2010 source code: <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/LogProcessExceptions">Trac</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/">Subversion</a></li>
<li>Binaries: Win32 &#8211; <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a>, x64 &#8211; <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a></li>
</ul>
]]></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>Download links:</p>
<ul>
<li>Partial Visual C++ .NET 2010 source code: <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/LogProcessExceptions">Trac</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/">Subversion</a></li>
<li>Binaries: Win32 &#8211; <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a>, x64 &#8211; <a href="http://www.alax.info/svn/public/trunk/Utilities/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe">LogProcessExceptions.exe</a></li>
</ul>
<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>If your application is looking for network adapters, it may be blind to see</title>
		<link>http://alax.info/blog/1195</link>
		<comments>http://alax.info/blog/1195#comments</comments>
		<pubDate>Thu, 28 Apr 2011 17:17:41 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[network]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1195</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1195" title="If your application is looking for network adapters, it may be blind to see"></a>A really long and annoying troubleshooting of a problem finally ended with a bug found in GetAdaptersInfo/GetAdaptersAddresses API. It may unexpectedly fail under the following conditions: 32-bit application 64-bit operating system or /3GB feature enabled on 32-bit operating system hosting &#8230;<p class="read-more"><a href="http://alax.info/blog/1195">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1195" title="If your application is looking for network adapters, it may be blind to see"></a><p>A really long and annoying troubleshooting of a problem finally ended with a bug found in <a href="http://msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx">GetAdaptersInfo</a>/<a href="http://msdn.microsoft.com/en-us/library/aa365915%28VS.85%29.aspx">GetAdaptersAddresses</a> API.</p>
<p>It may unexpectedly fail under the following conditions:</p>
<ul>
<li>32-bit application</li>
<li>64-bit operating system or /3GB feature enabled on 32-bit operating system</li>
<li>hosting process is linked with /LARGEADDRESSAWARE flag or has otherwise set it in binary header</li>
<li>over 2GB of RAM (in terms of Private Bytes/Virtual Size performance counters) consumed by the hosting process</li>
</ul>
<p>The application might unexpectedly start getting ERROR_NO_DATA (232) or ERROR_NOACCESS (998) error codes instead of list of adapters. Supposedly, the responsible Microsoft component iplhlpapi.dll or one of the underlying components/APIs are incompatible with such environments (such as for example, treating 32-bit pointer with the most significant bit set as invalid or compares such pointers as signed values).</p>
<p><img class="alignnone size-full wp-image-1196" title="GetAdaptersInfo Failure" src="http://alax.info/blog/wp-content/uploads/2011/04/Image0011.png" alt="" width="301" height="214" /></p>
<p>A test Visual C++ .NET 2010 application <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/GetAdaptersAddressesTest">is  available from SVN</a>. More bug details on <a href="http://connect.microsoft.com/VisualStudio/feedback/details/665383/getadaptersaddresses-api-incorrectly-returns-no-adapters-for-a-process-with-high-memory-consumption">Microsoft Connect</a>.</p>
<p>NOTE 1: You need to run &#8220;GetAdaptersAddressesTest /regserver&#8221; once to register the app before running it for the test.</p>
<p>NOTE 2: The problem does not seem to take place with 32-bit operating systems with /3GB tuning enabled.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1195/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

