<?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; bug</title>
	<atom:link href="http://alax.info/blog/tag/bug/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>So, how many EVRs you can do?</title>
		<link>http://alax.info/blog/1342</link>
		<comments>http://alax.info/blog/1342#comments</comments>
		<pubDate>Sat, 03 Mar 2012 21:52:12 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[direct3d]]></category>
		<category><![CDATA[evr]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1342</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1342" title="So, how many EVRs you can do?"></a>Direct3D based DirectShow video renderers – Video Mixing Renderer 9 and Enhanced Video Renderer – have been notoriously known for consuming resources in a way that you can run at most X simultaneously. There has been no comment published on &#8230;<p class="read-more"><a href="http://alax.info/blog/1342">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1342" title="So, how many EVRs you can do?"></a><p>Direct3D based DirectShow video renderers – <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd407344%28v=vs.85%29.aspx">Video Mixing Renderer 9</a> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms694916%28v=vs.85%29.aspx">Enhanced Video Renderer</a> – have been notoriously known for consuming resources in a way that you can run at most X simultaneously. There has been no comment published on the topic and questions (e.g. like this: <a href="http://social.msdn.microsoft.com/Forums/en/windowsdirectshowdevelopment/thread/1debfc45-caa3-4dcb-ab1d-c7162adaa1f7">How many VMR 9 can a PC support concurrently</a>) remain unanswered for a long time. Video Mixing Renderer 7 was a good alternative for some time in past until it was cut down to be unable to support hardware scaling (thanks Microsoft!).</p>
<p>The trendy way to render video nowadays is using Enhanced Video Renderer, a Media Foundation subsystem with an interface into DirectShow to take over state of the art video rendering capabilities. So, how many EVRs one can run simultaneously? Chances are that it is less than one could suppose.</p>
<p>The interesting part is that there is no obvious evidence on type of resource running out, which causing next EVR instance to fail to run. And not even run, the failure seem to be coming up at an earlier stage of just connecting pins in stopped state. The failure might be accompanied with errors like E_INVALIDARG, ERROR_FILE_NOT_FOUND, E_UNEXPECTED. The actual limit appear to be loosely correlating to parameters of video output, such as resolution and bitness.</p>
<p><img class="alignnone size-full wp-image-1344" title="Stress EVR" src="http://alax.info/blog/wp-content/uploads/2012/03/Image001.png" alt="" width="742" height="374" /></p>
<p>Desperately waiting for clarification, I am sharing the tool to estimate the limit:</p>
<ul>
<li>multiple EVR instances at once, hosted by multiple windows, which can be distributed across multiple monitors</li>
<li>choices of resolutions and formats</li>
<li>a double click on an individual renderer pops up property page set displaying effective frame rate</li>
<li>32- and 64-bit versions</li>
</ul>
<p><a href="http://alax.info/blog/wp-content/uploads/2012/03/Image002.png"><img class="alignnone size-large wp-image-1345" title="Stress EVR Live" src="http://alax.info/blog/wp-content/uploads/2012/03/Image002-800x391.png" alt="" width="620" height="303" /></a></p>
<p>A binary [<a href="http://www.alax.info/svn/public/trunk/Utilities/StressEvr/_Bin/Win32/Release%20Trace/StressEvr.exe">Win32</a>, <a href="http://www.alax.info/svn/public/trunk/Utilities/StressEvr/_Bin/x64/Release%20Trace/StressEvr.exe">x64</a>] and partial Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/StressEvr">partial source code</a> are available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1342/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Controls: Versions, Compatibility, WTL</title>
		<link>http://alax.info/blog/1306</link>
		<comments>http://alax.info/blog/1306#comments</comments>
		<pubDate>Sun, 30 Oct 2011 19:55:52 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[WTL]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[compatibility]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1306</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1306" title="Common Controls: Versions, Compatibility, WTL"></a>An application appears to be not working in Windows XP in a weird way: rebar control appeared to fail showing up. Where the application is expected to look much nicer with rebar control as a container for menu (implemented as &#8230;<p class="read-more"><a href="http://alax.info/blog/1306">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1306" title="Common Controls: Versions, Compatibility, WTL"></a><p>An application appears to be not working in Windows XP in a weird way: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb774373%28v=vs.85%29.aspx">rebar control</a> appeared to fail showing up.</p>
<p><img class="alignnone size-full wp-image-1310" title="Rebar Failed" src="http://alax.info/blog/wp-content/uploads/2011/10/Image003.png" alt="" width="640" height="238" /></p>
<p>Where the application is expected to look much nicer with rebar control as a container for menu (implemented as command bar WTL control) and toolbar with buttons:</p>
<p><img class="alignnone size-full wp-image-1309" title="Rebar Succeeded" src="http://alax.info/blog/wp-content/uploads/2011/10/Image0022.png" alt="" width="640" height="238" /></p>
<p>A WTL sample project generated by a Visual Studio wizard would never give such effect, and the bug was a combination of factors:</p>
<ol>
<li>An application built with a newer version of Windows SDK, which includes support for features (Windows Vista+ Common Controls) that are more recent than production environment (Windows XP); the application targets to Windows Vista+ environment too (<em>_WIN32_WINNT</em> &gt;= <em>0&#215;0600</em>)</li>
<li>Compatibility issues of Common Controls library</li>
<li>WTL version (7.5), which did not yet include a workaround for the problem</li>
</ol>
<p>The problem, which caused the bug directly was the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb774393%28v=vs.85%29.aspx">REBARBANDINFO structure</a> and its use as an argument with Common Controls API. As MSDN shows, the structure was amended twice with additional fields.</p>
<p>One of the way to support multiple versions of the structure definition, and to resolve compatibility issues, is to embed structure size into structure payload. In fact, <em>REBARBANDINFO::cbSize</em> member is there exactly for this reason.</p>
<p>The application is normally filling <em>cbSize</em> with the maximal known structure size and fills the rest of the fields respectively. The API is expected to be checking <em>cbSize</em> member and be detecting API version compatibility scenarios:</p>
<ol>
<li><em>cbSize</em> holds exactly the value the API expects (that is, the maximal value known/defined to the API) &#8211; the simplest scenario where the API and the application are on the same page, both are using the same versions of the &#8220;protocol&#8221;/interface.</li>
<li><em>cbSize</em> is smaller than API can support &#8211; the API sees that it is dealing with a sort of legacy application which cannot utilize all available features, and the API acts respectively supporting the older part of the protocol, and keeping defaults or &#8220;old look&#8221; for the rest of implementation. This addresses backward compatibility: the newer API works with apps designed for older version of the API</li>
<li><em>cbSize</em> is greater then API can support &#8211; the API sees that the application is already aware of newer version API and is possibly requesting some of the missing features. The API might be ignoring the unsupported part in assumption that API evolution tool place keeping some compatibility in mind, and still do the best it can with the existing implementation. Or, the API might just fail to work.</li>
</ol>
<p>The latter item #3 is the scenario here with rebar control. The application is using Windows Vista version of <em>REBARBANDINFO</em> structure and Windows XP implementation choses to completely fail.</p>
<p>While it does not seem to be directly a bug, this attitude is definitely not developer friendly: there is no reason for the control to not work in its best and default way. Having API acting this way, each developer using the API needs to take care of the situation explicitly: whenever Windows Vista enabled application needs to be able to run in Windows XP system, the code around <em>REBARBANDINFO</em> would look like this:</p>
<pre style="color: #000000; background: #ffffff;">REBARBANDINFO BandInformation <span style="color: #808030;">=</span> <span style="color: #800080;">{</span> <span style="color: #800000; font-weight: bold;">sizeof</span> BandInformation<span style="color: #808030;">,</span> RBBIM_LPARAM <span style="color: #800080;">}</span><span style="color: #800080;">;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">if</span><span style="color: #004a43;"> _WIN32_WINNT </span><span style="color: #808030;">&gt;</span><span style="color: #808030;">=</span><span style="color: #004a43;"> 0x0600</span>
<span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>GetOsVersion<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #808030;">&lt;</span> <span style="color: #008000;">0x00060000</span> <span style="color: #808030;">|</span><span style="color: #808030;">|</span> GetCommCtrlVersion<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #808030;">&lt;</span> <span style="color: #008000;">0x00060000</span><span style="color: #808030;">)</span> <span style="color: #696969;">// pre-Vista, Common Controls pre-6.0</span>
    BandInformation<span style="color: #808030;">.</span>cbSize <span style="color: #808030;">=</span> REBARBANDINFO_V6_SIZE<span style="color: #800080;">;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43;">endif</span><span style="color: #696969;">// _WIN32_WINNT &gt;= 0x0600</span>
<span style="color: #800000; font-weight: bold;">const</span> <span style="color: #603000;">BOOL</span> bGetBandInfoResult <span style="color: #808030;">=</span> Rebar<span style="color: #808030;">.</span>GetBandInfo<span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>BandInformation<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>If the API was nicer to developers, the code would be plain and simple:</p>
<pre style="color: #000000; background: #ffffff;">REBARBANDINFO BandInformation <span style="color: #808030;">=</span> <span style="color: #800080;">{</span> <span style="color: #800000; font-weight: bold;">sizeof</span> BandInformation<span style="color: #808030;">,</span> RBBIM_LPARAM <span style="color: #800080;">}</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">const</span> <span style="color: #603000;">BOOL</span> bGetBandInfoResult <span style="color: #808030;">=</span> Rebar<span style="color: #808030;">.</span>GetBandInfo<span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>BandInformation<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>To address this problem, <a href="http://wtl.sourceforge.net/">WTL</a> 8.0 comes up with <em>RunTimeHelper</em> namespace and its <em>SizeOf_REBARBANDINFO</em> function. It takes care of details for the developer choosing the proper size of the structure on runtime. The code is being taken back to a simpler shape:</p>
<pre style="color: #000000; background: #ffffff;">REBARBANDINFO BandInformation <span style="color: #808030;">=</span> <span style="color: #800080;">{</span> RunTimeHelper<span style="color: #800080;">::</span>SizeOf_REBARBANDINFO<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> RBBIM_LPARAM <span style="color: #800080;">}</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">const</span> <span style="color: #603000;">BOOL</span> bGetBandInfoResult <span style="color: #808030;">=</span> Rebar<span style="color: #808030;">.</span>GetBandInfo<span style="color: #808030;">(</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>BandInformation<span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>All in all:</p>
<ul>
<li>be aware of compatibility issues (same scenario exists with other SDK structures: <em>LVGROUP</em>, <em>LVTILEINFO</em>, <em>MCHITTESTINFO</em>, <em>NONCLIENTMETRICS</em> and other).</li>
<li>use latest version of WTL to have things worked around for you where Microsoft developers were not kid enough to provide perfect API</li>
<li>be aware and take advantage of WTL&#8217;s <em>RunTimeHelper</em> class</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1306/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft DTV-DVD Video Decoder at High Resolutions</title>
		<link>http://alax.info/blog/1283</link>
		<comments>http://alax.info/blog/1283#comments</comments>
		<pubDate>Wed, 21 Sep 2011 20:51:23 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[DirectShow]]></category>
		<category><![CDATA[H.264]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1283</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1283" title="Microsoft DTV-DVD Video Decoder at High Resolutions"></a>It is really great news that Windows 7 offers quite some goodies for MPEG-4 support right out of the box, but some things appear to be made not so accurate. The component responsible for video decoding is Microsoft DTV-DVD Video &#8230;<p class="read-more"><a href="http://alax.info/blog/1283">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1283" title="Microsoft DTV-DVD Video Decoder at High Resolutions"></a><p>It is really great news that Windows 7 offers quite some goodies for MPEG-4 support right out of the box, but some things appear to be made not so accurate.</p>
<p>The component responsible for video decoding is <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd390679%28v=vs.85%29.aspx">Microsoft DTV-DVD Video Decoder</a> (also known as Microsoft MPEG-2 Video Decoder), available with DirectShow interface. It offers video decoding capabilities for video resolutions up to 1920&#215;1088 (Full HD), including DXVA version 2 mode. The decoder is a &#8220;preferred decoder&#8221; and is given a first chance to be taken when it comes for <a href="http://msdn.microsoft.com/en-us/library/dd390342%28v=VS.85%29.aspx">Intelligent Connect</a> to render a video stream.</p>
<p>Technology went ahead though, stepped behind 1920&#215;1088 in particular. Microsoft DTV-DVD Video Decoder DirectShow filter still remains the first take in H.264 video decoding, and what however is taking place &#8211; the decoder accepts the format (media type) which is clearly cannot decode, neither according to specification, nor in real life. As soon as pin connection succeeds, DirectShow is not trying to look for another decoder, which can truly decode the feed. Result: failure to decode video.</p>
<p><img class="alignnone size-full wp-image-1284" title="Incorrect Media Type on Microsoft DTV-DVD Decoder Output" src="http://alax.info/blog/wp-content/uploads/2011/09/Image001.png" alt="" width="1034" height="624" /></p>
<p>The problem takes place in a quite surprising way. Given 5 megapixel video (2560&#215;1592, H.264, Baseline profile, from <a href="http://en.wikipedia.org/wiki/RTSP">RTSP</a>-enabled IP camera), the decoder advertises 720&#215;480 NV12 output media type. While consuming input data, the decoder outputs nothing and keeps video window black.</p>
<p>It does not issue any filter graph events, no. The filter is persistent and works hard in assumption that a new piece of knowledge will come from Mars and provide new capabilities to start decoding video, or filter&#8217;s input will give up and provide a new <a href="http://en.wikipedia.org/wiki/Network_Abstraction_Layer#Parameter_Sets">SPS</a> to lower resolution down to something less complex.</p>
<p>Well then, so is this advanced technology yet to be discovered by science? It appears that no. CoreAVC Video Decoder, applied to the same H.264 video decodes it well and nice:</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/09/Image002.png"><img class="alignnone size-large wp-image-1287" title="CoreAVC H.264 Video Decoder with 5 MP Video from StarDot SDH500BN IP Camera" src="http://alax.info/blog/wp-content/uploads/2011/09/Image002-505x600.png" alt="" width="505" height="600" /></a></p>
<p><span style="text-decoration: underline;">Update 2011-09-26</span>: According to Microsoft classification, this is <a href="http://connect.microsoft.com/VisualStudio/feedback/details/689926/dtv-dvd-video-decoder-directshow-filter-negotiates-decoding-at-high-resolutions-but-fails-to-actually-decode">a bug on a released product</a>. That is, if anyone is experiencing severe issues as a result of this behavior, one might need to contact Microsoft Support directly in search of a solution.</p>
<p>Actually, as resolutions over Full HD (1920&#215;1080) are not so much common at the moment, the issue side effects are not yet likely. Still moving on with time, I think the issues are likely to come up, without being severe though.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1283/feed</wfw:commentRss>
		<slash:comments>4</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>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>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>
		<item>
		<title>You cannot remove RBBS_NOGRIPPER flag from rebar band</title>
		<link>http://alax.info/blog/1187</link>
		<comments>http://alax.info/blog/1187#comments</comments>
		<pubDate>Thu, 31 Mar 2011 16:28:12 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Source]]></category>
		<category><![CDATA[WTL]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[rebar]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1187</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1187" title="You cannot remove RBBS_NOGRIPPER flag from rebar band"></a>It is as stupid as it sounds: you can add RBBS_NOGRIPPER flag to a rebar band to implement &#8220;lock controls&#8221; feature, but as soon as you start removing it &#8211; you miserably fail. A related thread on WTL group is &#8230;<p class="read-more"><a href="http://alax.info/blog/1187">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1187" title="You cannot remove RBBS_NOGRIPPER flag from rebar band"></a><p>It is as stupid as it sounds: you can add RBBS_NOGRIPPER flag to a rebar band to implement &#8220;lock controls&#8221; feature, but as soon as you start removing it &#8211; you miserably fail.</p>
<p><a href="http://tech.dir.groups.yahoo.com/group/wtl/message/13979">A related thread on WTL group</a> is dated year 2006. You can find the same dated 2003. Now it&#8217;s 2011 and it&#8217;s still here.</p>
<p>OK, it might be some bug in controlling code etc, but here is <a href="http://msdn.microsoft.com/en-us/library/bb773165%28VS.85%29.aspx">Control Spy 2.0</a>. Here you go:</p>
<ul>
<li>rebar</li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb774451%28VS.85%29.aspx">RB_GETBANDINFO</a>, <a href="http://msdn.microsoft.com/en-us/library/bb774393%28VS.85%29.aspx">RBBIM_STYLE</a></li>
<li>add RBBS_NOGRIPPER, Send</li>
<li>remove RBBS_NOGRIPPER, Send</li>
<li>ouch!</li>
</ul>
<p>WTL does a cool trick to fool the bastard:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">void</span> LockBands<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">bool</span> bLock<span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #800000; font-weight: bold;">int</span> nBandCount <span style="color: #808030;">=</span> GetBandCount<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">for</span><span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">int</span> i <span style="color: #808030;">=</span><span style="color: #008c00;">0</span><span style="color: #800080;">;</span> i <span style="color: #808030;">&lt;</span> nBandCount<span style="color: #800080;">;</span> i<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        REBARBANDINFO rbbi <span style="color: #808030;">=</span> <span style="color: #800080;">{</span> RunTimeHelper<span style="color: #800080;">::</span>SizeOf_REBARBANDINFO<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #800080;">}</span><span style="color: #800080;">;</span>
        rbbi<span style="color: #808030;">.</span>fMask <span style="color: #808030;">=</span> RBBIM_STYLE<span style="color: #800080;">;</span>
        <span style="color: #603000;">BOOL</span> bRet <span style="color: #808030;">=</span> GetBandInfo<span style="color: #808030;">(</span>i<span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>rbbi<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        ATLASSERT<span style="color: #808030;">(</span>bRet<span style="color: #808030;">)</span><span style="color: #800080;">;</span>

        <span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span><span style="color: #808030;">(</span>rbbi<span style="color: #808030;">.</span>fStyle <span style="color: #808030;">&amp;</span> RBBS_GRIPPERALWAYS<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>
        <span style="color: #800080;">{</span>
            rbbi<span style="color: #808030;">.</span>fStyle <span style="color: #808030;">|</span><span style="color: #808030;">=</span> RBBS_GRIPPERALWAYS<span style="color: #800080;">;</span>
            bRet <span style="color: #808030;">=</span> SetBandInfo<span style="color: #808030;">(</span>i<span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>rbbi<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            ATLASSERT<span style="color: #808030;">(</span>bRet<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            rbbi<span style="color: #808030;">.</span>fStyle <span style="color: #808030;">&amp;</span><span style="color: #808030;">=</span> <span style="color: #808030;">~</span>RBBS_GRIPPERALWAYS<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
        <span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>bLock<span style="color: #808030;">)</span>
            rbbi<span style="color: #808030;">.</span>fStyle <span style="color: #808030;">|</span><span style="color: #808030;">=</span> RBBS_NOGRIPPER<span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">else</span>
            rbbi<span style="color: #808030;">.</span>fStyle <span style="color: #808030;">&amp;</span><span style="color: #808030;">=</span> <span style="color: #808030;">~</span>RBBS_NOGRIPPER<span style="color: #800080;">;</span>

        bRet <span style="color: #808030;">=</span> SetBandInfo<span style="color: #808030;">(</span>i<span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>rbbi<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        ATLASSERT<span style="color: #808030;">(</span>bRet<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>
<span style="color: #800080;">}</span></pre>
<p>Pretty cool and works&#8230; if you have one rebar only. If you have another one in the same window (e.g. mine is at bottom) &#8211; you fail again.</p>
<p>A nasty workaround is to nudge hosting window by resizing it, followed by a full cycle of internal layout updates:</p>
<pre style='color:#000000;background:#ffffff;'><span style='color:#603000; '>CRect</span> Position<span style='color:#800080; '>;</span>
ATLVERIFY<span style='color:#808030; '>(</span><span style='color:#400000; '>GetWindowRect</span><span style='color:#808030; '>(</span>Position<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
Position<span style='color:#808030; '>.</span>bottom<span style='color:#808030; '>+</span><span style='color:#808030; '>+</span><span style='color:#800080; '>;</span>
ATLVERIFY<span style='color:#808030; '>(</span><span style='color:#400000; '>MoveWindow</span><span style='color:#808030; '>(</span>Position<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
Position<span style='color:#808030; '>.</span>bottom<span style='color:#808030; '>-</span><span style='color:#808030; '>-</span><span style='color:#800080; '>;</span>
ATLVERIFY<span style='color:#808030; '>(</span><span style='color:#400000; '>MoveWindow</span><span style='color:#808030; '>(</span>Position<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1187/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ATL ActiveX control hosting memory leak, _freea assertion failure and more</title>
		<link>http://alax.info/blog/1166</link>
		<comments>http://alax.info/blog/1166#comments</comments>
		<pubDate>Fri, 04 Mar 2011 20:13:28 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1166</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1166" title="ATL ActiveX control hosting memory leak, _freea assertion failure and more"></a>If you were adding and removing ActiveX controls programmatically with ATL, you might have noticed a nasty memory leak after final release of the control. Control added, removed and a leaky block is left om heap. Tracking simple memory leaks &#8230;<p class="read-more"><a href="http://alax.info/blog/1166">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1166" title="ATL ActiveX control hosting memory leak, _freea assertion failure and more"></a><p>If you were adding and removing ActiveX controls programmatically with ATL, you might have noticed a nasty memory leak after final release of the control. Control added, removed and a leaky block is left om heap.</p>
<p>Tracking simple memory leaks is not a rocket science with Microsoft C++ runtime, by adding _CRTDBG_MAP_ALLOC define, as described in <a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3.aspx">Memory Leak Detection Enabling on MSDN</a>:</p>
<pre><span style="color: blue;">#if</span> defined(_DEBUG)
<span style="color: blue;">#define</span> _CRTDBG_MAP_ALLOC
#<span style="color: blue;">include</span> &lt;stdlib.h&gt;
#<span style="color: blue;">include</span> &lt;crtdbg.h&gt;
<span style="color: blue;">#endif</span> <span style="color: green;">// defined(_DEBUG)</span></pre>
<p>Having that done, the problem was isolated to an extra instance of CAxHostWindow, which was excessively instantiated and never freed. Luckily, having this done you already have sufficient amount of keywords on hands to locate relevant articles on Microsoft knowledge base:</p>
<ul>
<li><a href="http://support.microsoft.com/KB/229904">PRB: CAxWindow Members Can Cause a Memory Leak</a></li>
<li><a href="How to add ActiveX controls to an ATL composite control programmatically in Visual C++">How to add ActiveX controls to an ATL composite control programmatically in Visual C++</a></li>
</ul>
<p>Basically, what is taking place is the behavior by design. That is, AtlAxCreateControlEx API is leaky by design, one gotta just be aware.</p>
<pre>CComPtr&lt;IUnknown&gt; pContainerUnkonwn, pControlUnknown;
<span style="color: blue;">static</span> LPCTSTR g_pszControlClass = _T(<span style="color: maroon;">"{8856F961-340A-11D0-A96B-00C04FD705A2}"</span>);
<span style="color: green;">// NOTE: This results in two instances of CAxWindowHost being created, one of which appears to be leaked (see below), </span>
<span style="color: green;">//       see http://support.microsoft.com/kb/218442</span>
<span style="color: green;">// </span>
<span style="color: green;">// Detected memory leaks!</span>
<span style="color: green;">// Dumping objects -&gt;</span>
<span style="color: green;">// {81} normal block at 0x00128928, 244 bytes long.</span>
<span style="color: green;">// Data: &lt;x .       .     &gt; 78 09 2E 01 01 00 00 00 0C 10 2E 01 00 00 00 00 </span>
<span style="color: green;">// Object dump complete.</span>
ATLENSURE_SUCCEEDED(AtlAxCreateControlEx(CT2COLE(g_pszControlClass), m_hWnd, <span style="color: blue;">NULL</span>, &amp;pContainerUnkonwn, &amp;pControlUnknown));</pre>
<p><span id="more-1166"></span>A workaround here is suggested by Q218442 and is a replacement of AtlAx* API call with a CAxWindow class:</p>
<pre>CComPtr&lt;IUnknown&gt; pContainerUnkonwn, pControlUnknown;
<span style="color: blue;">static</span> LPCTSTR g_pszControlClass = _T(<span style="color: maroon;">"{8856F961-340A-11D0-A96B-00C04FD705A2}"</span>);
CAxWindow AxWindow;
AxWindow.Attach(m_hWnd);
ATLENSURE_SUCCEEDED(AxWindow.CreateControlEx(CT2COLE(g_pszControlClass), <span style="color: blue;">NULL</span>, &amp;pContainerUnkonwn, &amp;pControlUnknown));</pre>
<p>The happiness of leak resolution here might be somewhat diluted by a weird assertion failure coming out from the deepness of ATL. The problem comes from ~CAutoStackPtr destructor (undocumented) trying to <a href="http://msdn.microsoft.com/en-us/library/k8984a8h.aspx">_freea</a> a memory block allocated by caller&#8217;s <a href="http://msdn.microsoft.com/en-us/library/5471dc8s.aspx">_malloca</a>. While caller&#8217;s _malloca is converted to plain heap allocation (_malloc_dbg) for debugging purposes, _freea is still looking for security cookie which never existed. Hence, assertion failure:</p>
<pre>_ASSERTE((<span style="color: maroon;">"Corrupted pointer passed to _freea"</span>, <span style="color: maroon;">0</span>));</pre>
<p>Unfortunately, it appears that _CRTDBG_MAP_ALLOC macro is not compatible with _malloca/_freea, the problem exists in versions of Visual C++ 2008 (at least) through 2010 SP1.</p>
<p>Vote for the problem on MS Connect: <a href="https://connect.microsoft.com/VisualStudio/feedback/details/649364/malloca-freea-mismatch-with-malloc-debug-builds-crtdbg-map-alloc">_malloca/_freea mismatch with malloc debug builds (_CRTDBG_MAP_ALLOC)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1166/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Microsoft.Jet.OLEDB.4.0 ProgID is not available (Windows 7 but probably not only)</title>
		<link>http://alax.info/blog/1086</link>
		<comments>http://alax.info/blog/1086#comments</comments>
		<pubDate>Fri, 30 Oct 2009 08:56:38 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[ole]]></category>
		<category><![CDATA[oledb]]></category>
		<category><![CDATA[registry]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1086</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1086" title="Microsoft.Jet.OLEDB.4.0 ProgID is not available (Windows 7 but probably not only)"></a>It was a sort of ridiculous problem: an attempt to instantiate a Microsoft.Jet.OLEDB.4.0 object failed with error. Still some applications are still running without problems connecting to Jet databases, how comes? There has been a number of posts on Internet, &#8230;<p class="read-more"><a href="http://alax.info/blog/1086">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1086" title="Microsoft.Jet.OLEDB.4.0 ProgID is not available (Windows 7 but probably not only)"></a><p>It was a sort of ridiculous problem: an attempt to instantiate a Microsoft.Jet.OLEDB.4.0 object failed with error. Still some applications are still running without problems connecting to Jet databases, how comes?</p>
<p>There has been <a href="http://www.google.com/search?q=Microsoft.Jet.OLEDB.4.0">a number of posts on Internet</a>, but none of the top ones appeared to be relevant.</p>
<p>The problem is reproduced extremely simple:</p>
<pre style="background: #ffffff none repeat scroll 0% 0%; color: #000000; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"><span style="color: #800000; font-weight: bold;">int</span> _tmain<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">int</span> argc<span style="color: #808030;">,</span> _TCHAR<span style="color: #808030;">*</span> argv<span style="color: #808030;">[</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    ATLVERIFY<span style="color: #808030;">(</span>SUCCEEDED<span style="color: #808030;">(</span>CoInitialize<span style="color: #808030;">(</span><span style="color: #7d0045;">NULL</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">{</span>
        <span style="color: #696969;">//CoLoadLibrary(L"C:\\Program Files (x86)\\Common Files\\System\\Ole DB\\oledb32.dll", TRUE);</span>
        CComPtr<span style="color: #800080;">&lt;</span>IDBInitialize<span style="color: #800080;">&gt;</span> pDbInitialize<span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">const</span> HRESULT nResult <span style="color: #808030;">=</span> pDbInitialize<span style="color: #808030;">.</span>CoCreateInstance<span style="color: #808030;">(</span><span style="color: #800000;">L"</span><span style="color: #0000e6;">Microsoft.Jet.OLEDB.4.0</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">,</span> CLSCTX_INPROC_SERVER<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        _tprintf<span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">nResult 0x</span><span style="color: #0f69ff;">%08x</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> nResult<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>
    CoUninitialize<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
<span style="color: #800080;">}</span></pre>
<p>Oops, the code gives error REGDB_E_CLASSNOTREG 0&#215;80040154 &#8220;Class not registered&#8221;. It looked like system was unable to locate one of the internally used DLLs &#8211; oledb32.dll, and if we help by uncommenting the line commented in the code snippet above, the error changes to ERROR_MOD_NOT_FOUND 0x8007007e &#8220;The specified module could not be found&#8221;.</p>
<p>The problem appears to be that one of the system components, which is involved, &#8220;Microsoft OLE DB Data Conversion Library&#8221; is registered with the system using a <a href="http://msdn.microsoft.com/en-us/library/ms724884%28VS.85%29.aspx">REG_EXPAND_SZ</a> value, to be located using path &#8220;%CommonProgramFiles(x86)%\System\Ole DB\oledb32.dll&#8221;. It is obvious that <strong>CommonProgramFiles(x86)</strong> is placeholder to be expanded, but does the expansion succeed?</p>
<p><span id="more-1086"></span></p>
<p>On my system there was no such environment string, so expansion failed. There however was <strong>CommonProgramFiles</strong>, and it pointed to correct location. So, this hot fix makes it work right:</p>
<pre><span style="color: #603000;">BOOL</span> UpdateEnvironmentVariables<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #603000;">CString</span> sCommonProgramFilesX86<span style="color: #800080;">;</span>
    sCommonProgramFilesX86<span style="color: #808030;">.</span><span style="color: #400000;">GetEnvironmentVariable</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">CommonProgramFiles(x86)</span><span style="color: #800000;">"</span><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>sCommonProgramFilesX86<span style="color: #808030;">.</span>IsEmpty<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        <span style="color: #603000;">CString</span> sCommonProgramFiles<span style="color: #800080;">;</span>
        sCommonProgramFiles<span style="color: #808030;">.</span><span style="color: #400000;">GetEnvironmentVariable</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">CommonProgramFiles</span><span style="color: #800000;">"</span><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><span style="color: #808030;">!</span>sCommonProgramFiles<span style="color: #808030;">.</span>IsEmpty<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            ATLVERIFY<span style="color: #808030;">(</span><span style="color: #400000;">SetEnvironmentVariable</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">CommonProgramFiles(x86)</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> sCommonProgramFiles<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">return</span> TRUE<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
    <span style="color: #800080;">}</span>
    <span style="color: #800000; font-weight: bold;">return</span> FALSE<span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>

<span style="color: #800000; font-weight: bold;">int</span> _tmain<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">int</span> argc<span style="color: #808030;">,</span> _TCHAR<span style="color: #808030;">*</span> argv<span style="color: #808030;">[</span><span style="color: #808030;">]</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    ATLVERIFY<span style="color: #808030;">(</span>SUCCEEDED<span style="color: #808030;">(</span>CoInitialize<span style="color: #808030;">(</span><span style="color: #7d0045;">NULL</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">{</span>
        UpdateEnvironmentVariables<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        CComPtr<span style="color: #800080;">&lt;</span>IDBInitialize<span style="color: #800080;">&gt;</span> pDbInitialize<span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">const</span> HRESULT nResult <span style="color: #808030;">=</span> pDbInitialize<span style="color: #808030;">.</span>CoCreateInstance<span style="color: #808030;">(</span><span style="color: #800000;">L"</span><span style="color: #0000e6;">Microsoft.Jet.OLEDB.4.0</span><span style="color: #800000;">"</span><span style="color: #808030;">,</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">,</span> CLSCTX_INPROC_SERVER<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        _tprintf<span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">nResult 0x</span><span style="color: #0f69ff;">%08x</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> nResult<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>
    CoUninitialize<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
<span style="color: #800080;">} </span></pre>
<p>I am under impression that the problem is not specific to Windows 7. I remember similar symptoms on Windows XP system, but they were not critical and not investigated to solution. It rather seems that OLE DB component is registering itself using a reference to environment variable which is not present on a number of systems.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 81px; width: 1px; height: 1px;">
<h3 class="r"><a class="l" onmousedown="return rwt(this,'','','res','1','AFQjCNEiQBNPN2MLUciPFN3dRVUU-1FyJA','&amp;sig2=GPxd_FHW5MyBRM2Op0Da1A','0CAoQFjAA')" href="http://support.software602.com/kb/view.aspx?articleID=987">Error code 80040154. <em>Class not registered</em></a></h3>
</div>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1086/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

