<?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>Code for Concinnity &#187; windows</title>
	<atom:link href="http://cfc.kizzx2.com/index.php/tag/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://cfc.kizzx2.com</link>
	<description>beautiful and elegant solutions</description>
	<lastBuildDate>Fri, 10 Feb 2012 13:24:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dissecting obscure COM errors</title>
		<link>http://cfc.kizzx2.com/index.php/dissecting-obscure-com-errors/</link>
		<comments>http://cfc.kizzx2.com/index.php/dissecting-obscure-com-errors/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 07:40:16 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[E_INVALIDARG]]></category>
		<category><![CDATA[global interface table]]></category>
		<category><![CDATA[marshaling]]></category>
		<category><![CDATA[RPC_E_WRONG_THREAD]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=244</guid>
		<description><![CDATA[Here&#8217;s how I solved these hair-pulling COM errors: 0x800401fd CO_E_OBJNOTCONNECTED Object is not connected to server. 0x8001010e RPC_E_WRONG_THREAD The application called an interface that was marshalled for a different thread. And this one when I called IGlobalInterfaceTable::GetInterfaceFromGlobal (through CComGITPtr): 0&#215;80070057 E_INVALIDARG The parameter is incorrect. The basic framework To make sense of the following code [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdissecting-obscure-com-errors%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdissecting-obscure-com-errors%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Here&#8217;s how I solved these hair-pulling COM errors:</p>

<ul>
<li>0x800401fd <code>CO_E_OBJNOTCONNECTED</code> Object is not connected to server.</li>
<li>0x8001010e <code>RPC_E_WRONG_THREAD</code> The application called an interface that was marshalled for a different thread.</li>
</ul>

<p>And this one when I called <code>IGlobalInterfaceTable::GetInterfaceFromGlobal</code> (through <code>CComGITPtr</code>):</p>

<ul>
<li>0&#215;80070057 <code>E_INVALIDARG</code> The parameter is incorrect.</li>
</ul>

<h2>The basic framework</h2>

<p>To make sense of the following code segments, here&#8217;s the whole program&#8217;s framework to use for the following scenarios:</p>

<p>We use the boost library for threading to make the code less noisy of plumbing work. You don&#8217;t really need to grasp them to understand what&#8217;s going on.</p>

<p>I have chosen to use the <code>SHDocVw::IShellWindowsPtr</code> interface because it should be present in every Windows environment and simplifies our example without polluting it with the IDL of an example object.</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#import &quot;C:/Windows/system32/shdocvw.dll&quot;</span><br />
<br />
<span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #339900;">#include &lt;Windows.h&gt;</span><br />
<span style="color: #339900;">#include &lt;comutil.h&gt;</span><br />
<br />
<span style="color: #339900;">#include &lt;boost/thread.hpp&gt;</span><br />
<span style="color: #339900;">#include &lt;boost/ref.hpp&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// Convenient error checking -- these will fail our </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// program with nicely printed error message in case of </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// any COM error</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> PrintHR<span style="color: #008000;">&#40;</span>HRESULT hr<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">wcerr</span> <span style="color: #000080;">&lt;&lt;</span> L<span style="color: #FF0000;">&quot;0x&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">hex</span> <span style="color: #000080;">&lt;&lt;</span> hr <span style="color: #000080;">&lt;&lt;</span> L<span style="color: #FF0000;">&quot; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _com_error<span style="color: #008000;">&#40;</span>hr<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">ErrorMessage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> EnsureSuccess<span style="color: #008000;">&#40;</span>HRESULT hr<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>FAILED<span style="color: #008000;">&#40;</span>hr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrintHR<span style="color: #008000;">&#40;</span>hr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">exit</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">EXIT_FAILURE</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// A simple wrapper with integrated error checking</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HRESULT hr <span style="color: #000080;">=</span> CoInitialize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; EnsureSuccess<span style="color: #008000;">&#40;</span>hr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// This method can be called in a thread to demonstrate </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// multi-threading issues</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CreateShellWindow<span style="color: #008000;">&#40;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> <span style="color: #000040;">&amp;</span> retval<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Because we might be in a different thread, we need </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// to call CoInitialize() just to make sure </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HRESULT hr <span style="color: #000080;">=</span> retval.<span style="color: #007788;">CreateInstance</span><span style="color: #008000;">&#40;</span>__uuiof<span style="color: #008000;">&#40;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">ShellWindows</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; EnsureSuccess<span style="color: #008000;">&#40;</span>hr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// This should print a numeric number</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// On my machine it always printed &quot;1&quot;. The meaning of that </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// is not really important.</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> UseShellWindow<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> <span style="color: #000040;">&amp;</span> ptr<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ptr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Count <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Our example code segments would go here...</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">try</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">catch</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> _com_error <span style="color: #000040;">&amp;</span> e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PrintHR<span style="color: #008000;">&#40;</span>e.<span style="color: #007788;">Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">EXIT_FAILURE</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>0x800401fd <code>CO_E_OBJNOTCONNECTED</code> Object is not connected to server.</h2>

<p>This one is pretty straight forward. You get this when you try to use a COM object <em>after you have called</em> <code>CoUninitialize()</code>:</p>

<h3>How to reproduce</h3>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CreateShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; UseShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ka-boom!</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>0x8001010e <code>RPC_E_WRONG_THREAD</code> The application called an interface that was marshalled for a different thread.</h2>

<h3>How to reproduce</h3>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; CreateShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// This will use another thread to call `UseShellWindow()`</span><br />
&nbsp; &nbsp; boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>UseShellWindow, ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ka-boom</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h3>What happened</h3>

<p>The error message is pretty obvious &#8212; we need to marshal the pointer for thread A if the pointer was created in thread B, in order to use it in thread A.</p>

<h3>How to fix</h3>

<p>If you use ATL, one very convenient way to marshal them is to use the GIT (Global Interface Pointer) through <code>CComGITPtr</code>.</p>

<p>(The below serves as a quick fix and introduction. Please look into Global Interface Pointer and stuffs if you want to understand what&#8217;s going on).</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// A GIT cookie is something you can use to retrieve a pointer </span><br />
<span style="color: #666666;">// from the GIT</span><br />
<span style="color: #0000ff;">void</span> UseShellWindowFromCookie<span style="color: #008000;">&#40;</span>DWORD cookie<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CComGITPtr<span style="color: #000080;">&lt;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindows</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">CopyTo</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>fred<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; UseShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CreateShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// This will register our pointer in the GIT and </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// retrieve a GIT cookie in one line</span><br />
&nbsp; &nbsp; DWORD cookie <span style="color: #000080;">=</span> CComGITPtr<span style="color: #000080;">&lt;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindows</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Detach</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Spawn a new thread of the GIT aware version of </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// UseShellWindow()</span><br />
&nbsp; &nbsp; boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>UseShellWindowFromCookie, cookie<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">join</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>0&#215;80070057 <code>E_INVALIDARG</code> The parameter is incorrect.</h2>

<p>After discovering how easy it is to marshal COM objects across threads, you may sometimes get <code>E_INVALIDARG</code> when inside <code>IGlobalInterfaceTable::GetInterfaceFromGlobal()</code></p>

<p>Now this one is really really obscure:</p>

<h3>How to reproduce</h3>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> CreateShellWindowCookie<span style="color: #008000;">&#40;</span>DWORD <span style="color: #000040;">&amp;</span> retval<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CreateShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; retval <span style="color: #000080;">=</span> CComGITPtr<span style="color: #000080;">&lt;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindows</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Detach</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Instead of using a different thread for UseShellWindow()</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// This time around we use a different thread to </span><br />
&nbsp; &nbsp; <span style="color: #666666;">// create the COM pointer</span><br />
&nbsp; &nbsp; DWORD cookie<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>CreateShellWindowCookie, boost<span style="color: #008080;">::</span><span style="color: #007788;">ref</span><span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">join</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Let's try to use it from the current thread</span><br />
&nbsp; &nbsp; UseShellWindowFromCookie<span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ka-boom!</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h3>What happened</h3>

<p>This is similar to what happened with <code>CO_E_OBJNOTCONNECTED</code>, only a lot more subtle and obscure this time around.</p>

<p>To understand this, keep this in mind: <strong>A COM object is thread aware. A COM object can only ever live in one single thread, even though it can be accessed from another thread</strong> (through marshaling).</p>

<p>What that simply means is that when the thread holding the COM object terminates, the COM object becomes invalid. In our example above, <code>CreateShellWindowCookie()</code> was run from a separate thread and <strong>finished</strong> before we even tried to use it. The actual COM object <em>went down with the thread</em>. Even though we had a cookie, the GIT would fail to find the actual COM object when it tries to do <code>GetInterfaceFromGlobal()</code>. Too bad that it gives such a confusing error message.</p>

<p>To summarize, here are the things to check when you get <code>E_INVALIDARG</code> from <code>GetInterfaceFromGlobal()</code>:</p>

<ul>
<li>Maybe the originating thread that called <code>CoCreateInstance()</code> has ended</li>
<li>Maybe the originating thread has already called <code>CoUninitialize()</code></li>
<li>Maybe the actual object has already been terminated by someone <code>Release</code>ing it.</li>
<li>Maybe your GIT cookie has already been used. Strangely enough, it seems like the GIT cookie can only be used to get the interface back <em>once</em>.</li>
</ul>

<h3>How to fix</h3>

<p>There are 2 basic approaches:</p>

<ul>
<li>Do not create any COM objects from threads other than the main thread (which is guaranteed to last through the program&#8217;s life time)</li>
<li>Have an elaborate inter-thread communication so that the creator-thread is kept-alive until the COM object&#8217;s users are finished with it (This can get really complicated).</li>
</ul>

<p>To demonstrate, here&#8217;s the code that would work:</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> CreateShellWindowCookie<span style="color: #008000;">&#40;</span>DWORD <span style="color: #000040;">&amp;</span> retval<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindowsPtr</span> ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; CreateShellWindow<span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; retval <span style="color: #000080;">=</span> CComGITPtr<span style="color: #000080;">&lt;</span>SHDocVw<span style="color: #008080;">::</span><span style="color: #007788;">IShellWindows</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Detach</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Sleep 10 seconds</span><br />
&nbsp; &nbsp; Sleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">void</span> Example<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; MyCoInitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Create a cookie from another thread</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// We do not wait for it to finish</span><br />
&nbsp; &nbsp; DWORD cookie<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span> t<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>CreateShellWindowCookie, boost<span style="color: #008080;">::</span><span style="color: #007788;">ref</span><span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// We wait 3 seconds</span><br />
&nbsp; &nbsp; Sleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Now try to use it from cookie</span><br />
&nbsp; &nbsp; UseShellWindowFromCookie<span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK!</span><br />
<br />
&nbsp; &nbsp; CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>There you go</h2>

<p>Hopefully this saved you from some hair-pulling experiences <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/dissecting-obscure-com-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting memory leaks with WinDBG the modern (and free) way</title>
		<link>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/</link>
		<comments>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 16:36:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[crtdbg]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[windbg]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=215</guid>
		<description><![CDATA[This information is surprisingly scarce on the Internet. I should have realized earlier that the world has simply moved on to more modern languages than those who tax us to deal with mundane things like memory leaks. Anyway, for those who&#8217;re still stuck, this should be your savior. This is almost as good as Valgrind [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdetecting-memory-leaks-with-windbg-the-modern-and-free-way%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdetecting-memory-leaks-with-windbg-the-modern-and-free-way%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>This information is surprisingly scarce on the Internet. I should have realized earlier that the world has simply moved on to more modern languages than those who tax us to deal with mundane things like memory leaks.</p>

<p>Anyway, for those who&#8217;re still stuck, this should be your savior. This is almost as good as Valgrind on nix</p>

<h3>Minimal example</h3>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #339900;">#include &lt;crtdbg.h&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> leak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">::</span><span style="color: #007788;">operator</span> <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">double</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _CrtSetDbgFlag<span style="color: #008000;">&#40;</span>_CRTDBG_ALLOC_MEM_DF <span style="color: #000040;">|</span> _CRTDBG_LEAK_CHECK_DF<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; leak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<p>Running the above code will get you something like this in the Debug output:</p>

<pre><code>Detected memory leaks!
Dumping objects -&gt;
{67} normal block at 0x00573FC8, 8000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
{66} normal block at 0x005713E8, 1000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
</code></pre>

<p>This is nice and good, but it isn&#8217;t very useful since we (pretend) don&#8217;t know where the leak came from!</p>

<h3>And then WinDBG comes in</h3>

<p>(Obviously, add WinDBG to your <code>PATH</code> first)</p>

<p>First, we&#8217;ll turn on heap stack tracing for our program. <code>gflags.exe</code> needs to be run with elevated privileges:</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> gflags /i leak.exe +ust</div></td></tr></tbody></table></div>

<p>Now we&#8217;re ready to go!</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> windbg -g leak.exe</div></td></tr></tbody></table></div>

<p>At the debug output in WinDBG, we&#8217;ll see our leak report:</p>

<pre><code>Detected memory leaks!
Dumping objects -&gt;
{80} normal block at 0x003B4378, 8000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
{79} normal block at 0x003B3F50, 1000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
</code></pre>

<p>At WinDBG, we can trace down the originating functions</p>

<pre><code>000:0&gt; !heap -p -a 0x003B4378

address 004b4378 found in
_HEAP @ 4b0000
  HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state
    004b4340 03f0 0000  [00]   004b4358    01f64 - (busy)
    Trace: cfba8
    7753dd4d ntdll!RtlAllocateHeap+0x00000274
    6fa87743 MSVCR100D!_heap_alloc_base+0x00000053
    6fa95d8c MSVCR100D!_heap_alloc_dbg_impl+0x000001fc
    6fa95b2f MSVCR100D!_nh_malloc_dbg_impl+0x0000001f
    6fa95adc MSVCR100D!_nh_malloc_dbg+0x0000002c
    6fa9906b MSVCR100D!malloc+0x0000001b
    6fa871b1 MSVCR100D!operator new+0x00000011
    6fa872ef MSVCR100D!operator new[]+0x0000000f
</code></pre>

<p><strong><pre style="background-color: InfoBackground">    dc13d5 leak!leak+0x00000035</pre></strong></p>

<pre><code>    dc1447 leak!main+0x00000037
    dc19df leak!__tmainCRTStartup+0x000001bf
    dc180f leak!mainCRTStartup+0x0000000f
    75f43677 kernel32!BaseThreadInitThunk+0x0000000e
    774f9d42 ntdll!__RtlUserThreadStart+0x00000070
    774f9d15 ntdll!_RtlUserThreadStart+0x0000001b 
</code></pre>

<p>There you go! The highlighted line above is the culprit. For those unfamiliar with WinDBG, here&#8217;s a little more hand holding: Click on the source code view, <code>Ctrl+G</code> and then type <code>leak+0x35</code>.</p>

<p>Remember to turn gflags off afterwards. The gflags is a registry and won&#8217;t clear even if you delete the file itself.</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> gflags /i leak.exe -ust</div></td></tr></tbody></table></div>

<h3>32/64 bit</h3>

<p>To debug 32 bit applications in a 64 bit system, you need to use the 32 bit version of WinDBG, which is not installed by default. You can find <code>dbg_x86_*.msi</code> somewhere in your &#8220;Microsoft SDKs&#8221; installation directory.</p>

<h3>More advanced usage</h3>

<p>Heap debugging is not restricted to detecting leaks. If you find your application allocating a lot of memory in some places, you can easily trace down where they come from with WinDBG and the <code>!heap</code> extension. <a href="http://www.codeproject.com/KB/cpp/MemoryLeak.aspx">This CodeProject article</a> describes how to do just that in depth.</p>

<p>However, for doing that, I find a more modern and less intrusive way to be using <a href="http://msdn.microsoft.com/en-us/performance/default.aspx">XPerf</a> tool. That combined with WinDBG and you&#8217;ve got a debugging power-house. (Resources on how to do it is extremely scarce. If you thought this WinDBG technique is obscure, you ain&#8217;t seen nothing :p I may write an post about that some time later when I feel like it.)</p>

<h3>Wrong way of doing it</h3>

<p><a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3(VS.80).aspx">MSDN</a> suggests something like this</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #339900;">#define _CRTDBG_MAP_ALLOC</span><br />
&nbsp; &nbsp; <span style="color: #339900;">#include &lt;stdlib.h&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339900;">#include &lt;crtdbg.h&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// At the end of your program</span><br />
&nbsp; &nbsp; _CrtDumpMemoryLeaks<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p>This solution is inadequate for a couple of reasons:</p>

<ul>
<li>It only lists source code location for <code>malloc</code>. What&#8217;s the point?</li>
<li>Calling <code>_CrtDumpMemoryLeaks()</code> at the end of <code>main()</code> would mistakenly report memory allocated by static objects as leaks.</li>
</ul>

<p>The only good thing about it is that you don&#8217;t need to leave your comfort zone of Visual Studio (that&#8217;s probably the entire programmer life for many people, unfortunately).</p>

<h3>Some other good resources</h3>

<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3(VS.80).aspx">Memory Leak Detection Enabling</a></li>
<li><a href="http://windbg.info/doc/1-common-cmds.html">Common WinDbg Commands (Thematically Grouped)</a></li>
<li><a href="http://hacksoflife.blogspot.com/2009/06/heap-debugging-memoryresource-leak-with.html">The Hacks of Life: Heap Debugging (Memory/Resource Leak with WinDbg)</a></li>
<li><a href="http://www.codeproject.com/KB/cpp/MemoryLeak.aspx">Memory Leak Detection Using Windbg &#8211; CodeProject</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading a Unicode (UTF16) file in Windows (C++)</title>
		<link>http://cfc.kizzx2.com/index.php/reading-a-unicode-utf16-file-in-windows-c/</link>
		<comments>http://cfc.kizzx2.com/index.php/reading-a-unicode-utf16-file-in-windows-c/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 16:15:55 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Interesting things]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf16]]></category>
		<category><![CDATA[wifstream]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[wstringstream]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=180</guid>
		<description><![CDATA[I can&#8217;t believe it&#8217;s so convoluted. This is a step-by-step guide from how I discovered the Elegant Way to read a Unicode file in Windows &#8212; it&#8217;s only 4 lines! (The Unicode file I&#8217;m referring here is what you get when you save a file as Unicode in Notepad &#8212; that&#8217;s little Endian UTF16) For [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Freading-a-unicode-utf16-file-in-windows-c%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Freading-a-unicode-utf16-file-in-windows-c%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>I can&#8217;t believe it&#8217;s so convoluted. This is a step-by-step guide from how I discovered the Elegant Way to read a Unicode file in Windows &#8212; it&#8217;s only 4 lines!</p>

<p>(The Unicode file I&#8217;m referring here is what you get when you save a file as Unicode in Notepad &#8212; that&#8217;s little Endian UTF16)</p>

<h2>For the impatient</h2>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wstring ReadUTF16<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string <span style="color: #000040;">&amp;</span> filename<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; ifstream file<span style="color: #008000;">&#40;</span>filename.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; stringstream ss<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ss <span style="color: #000080;">&lt;&lt;</span> file.<span style="color: #007788;">rdbuf</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> wstring<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">wchar_t</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>ss.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>The problem</h2>

<p>To read UTF16, one would expect to use the widechar variants from <code>iostream</code>:</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// Failed attemp</span><br />
wifstream file<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;file.utf8.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
wstringstream ss<span style="color: #008080;">;</span><br />
ss <span style="color: #000080;">&lt;&lt;</span> file.<span style="color: #007788;">rdbuf</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
wstring result <span style="color: #000080;">=</span> ss.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p>However, the above code will give you bogus text.</p>

<h2>The investigation</h2>

<p>Why? If you debug it and look at memory, you will see <code>ss.str().c_str()</code> evaluates to something like this:</p>

<pre><code>0x00367738  ff 00 fe 00 60 00 4f 00 7d 00 59 00
</code></pre>

<p>The first 4 bytes look strikingly suspicious! <code>0xfffe</code> is the magic sequence (<a href="http://unicode.org/faq/utf_bom.html">BOM</a>) for little endian Unicode, yet it&#8217;s been broken up by <code>ifstream</code> into two wide-chars (4 bytes).</p>

<p>It turns out that <code>iostream</code> will try <strong>very hard</strong> to conform to the current codepage &#8212; something that&#8217;s so obscure about C++ that apparently nobody talks about (from my search). In most cases, the code page will be something Latin. <code>iostream</code> goes the extra mile to <strong>break up</strong> bytes into widechars, because it assumes all files it reads to be single-byte.</p>

<p>I heard you can actually tell <code>iostream</code> to treat the file as UTF16 instead. There is a setting somewhere probably related to <em>(Click it, look 5 seconds and then come back)</em> <a href="http://www.cplusplus.com/reference/std/locale/codecvt/">codecvt</a>. OK, back? That&#8217;s definitely not something you want to touch <strong>just to read a damn file</strong>.</p>

<h2>The solution</h2>

<p>It turns to be quite anti-climatic, let&#8217;s look at our solution again:</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wstring ReadUTF16<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string <span style="color: #000040;">&amp;</span> filename<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; ifstream file<span style="color: #008000;">&#40;</span>filename.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; stringstream ss<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; ss <span style="color: #000080;">&lt;&lt;</span> file.<span style="color: #007788;">rdbuf</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> wstring<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">wchar_t</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>ss.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<p>You see, basically what the above does is to tell <code>iostream</code> to read the file as a single byte file, but don&#8217;t break up the bytes into widechars (so things remain nicely packed) and we&#8217;ll do a hardcore conversion ourself. Problem solved.</p>

<h2>Caveats</h2>

<p>I didn&#8217;t bother to trace down where the <code>0xfffe</code> went after the conversion &#8212; it just worked and I was done with it. I also suspect some garbage might be appended at the end of the text stream. Again, Worked For Me. In any case, just do some substring to crop out the parts you don&#8217;t want.</p>

<h2>Why is it marked &#8220;Windows&#8221;?</h2>

<p>This is marked Windows because the code above is not portable &#8212; many *nix platforms have <code>sizeof(wchar_t) == 4</code>. In general, most modern POSIX supports UTF8 natively through plain old <code>char</code>. You can also use the excellent <code>libiconv</code> which converts everything to everything in one function call &#8212; making all this fuss irrelevant.</p>

<h2>Alternatives</h2>

<p>If you deal with UTF8, you might want to check out <a href="http://utfcpp.sourceforge.net/">UTF8-CPP</a>. It probably also supports reading UTF16 but I couldn&#8217;t find a quick way to make it work. It&#8217;s less heavyweight than <a href="http://icu-project.org/">ICU</a> &#8212; but who can beat 4 lines when you just want to read a darn file?</p>

<p><em>(UTF8-CPP does support reading UTF-16. Unfortunately it only supports converting it to UTF-8, which is something Windows can&#8217;t do conveniently)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/reading-a-unicode-utf16-file-in-windows-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>cmd-recycle: Delete files from Windows command line</title>
		<link>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/</link>
		<comments>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 19:30:48 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[recycle-bin]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=78</guid>
		<description><![CDATA[Download links Direct download browse on GitHub I wrote another tool to do just this a while ago, but that one stopped working since I migrated to Windows 7 x64. Microsoft said the SHFileOpearation interface (which the old tool used) had been replaced by the IFileOperation interface. So I figured I would dig up my [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcmd-recycle-delete-files-from-windows-command-line%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcmd-recycle-delete-files-from-windows-command-line%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><h2>Download links</h2>

<ul>
<li><a href="http://cloud.github.com/downloads/kizzx2/cmd-recycle/recycle.exe"><strong>Direct download</strong></a></li>
<li><a href="http://github.com/kizzx2/cmd-recycle">browse on GitHub</a></li>
</ul>

<p>I wrote <a href="http://kizzx2.com/blog/index.php/2008/09/27/windows-delete-to-recycle-bin-from-the-command-line/">another tool</a> to do just this a while ago, but that one stopped working since I migrated to Windows 7 x64. Microsoft said the <code>SHFileOpearation</code> interface (which the old tool used) <a href="http://msdn.microsoft.com/en-us/library/bb775771(VS.85).aspx">had been replaced by the <code>IFileOperation</code> interface</a>. So I figured I would dig up my rusty C# again to update it.</p>

<p>Damn, I can&#8217;t believe it took a couple of hours, but the result worked amazingly. This one also supports wildcards:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">recycle file1 file2 supports-wildcards\*.tmp</div></td></tr></tbody></table></div>

<p>I&#8217;ve gone the extra mile and set up a proper repository this time around <a href="http://github.com/kizzx2/cmd-recycle">on GitHub</a>. Probably some Windows gurus will mock me left and right with some obscure one-liners or something, lol.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting Windows&#8217; Network and Sharing Center from command prompt</title>
		<link>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/</link>
		<comments>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 13:21:46 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[network-and-sharing-center]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=76</guid>
		<description><![CDATA[It has always looked not-so-cool to have to type &#8220;Sharing&#8221; at the start menu or worse yet, navigate through the Control Panel just to start it. Today I finally dug out how to do it from TechNet: 1control /name Microsoft.NetworkAndSharingCenter Looks like Microsoft decided to go down the verbose path. Maybe I&#8217;ll just stick with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstarting-windows-network-and-sharing-center-from-command-prompt%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstarting-windows-network-and-sharing-center-from-command-prompt%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>It has always looked not-so-cool to have to type &#8220;Sharing&#8221; at the start menu or worse yet, navigate through the Control Panel just to start it. Today I finally dug out how to do it from <a href="http://technet.microsoft.com/en-us/library/cc733147(WS.10).aspx">TechNet</a>:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">control /name Microsoft.NetworkAndSharingCenter</div></td></tr></tbody></table></div>

<p>Looks like Microsoft decided to go down the verbose path. Maybe I&#8217;ll just stick with the old ways :p</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gvim &#8212; E303: Unable to open swap file for &#8220;[No Name]&#8220;, recovery impossible</title>
		<link>http://cfc.kizzx2.com/index.php/gvim-e303-unable-to-open-swap-file-for-no-name-recovery-impossible/</link>
		<comments>http://cfc.kizzx2.com/index.php/gvim-e303-unable-to-open-swap-file-for-no-name-recovery-impossible/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 19:58:56 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[e303]]></category>
		<category><![CDATA[gvim]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=60</guid>
		<description><![CDATA[vim is my editor of choice and I install it in Windows. All is working quite good except one little annoyance &#8212; when I open the editor fresh and started editing by going into insert mode, it will give this error message: 1E303: Unable to open swap file for &#34;[No Name]&#34;, recovery impossible This is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fgvim-e303-unable-to-open-swap-file-for-no-name-recovery-impossible%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fgvim-e303-unable-to-open-swap-file-for-no-name-recovery-impossible%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p><a href="http://vim.org">vim</a> is my editor of choice and I install it in Windows. All is working quite good except one little annoyance &#8212; when I open the editor fresh and started editing by going into <em>insert mode</em>, it will give this error message:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">E303: Unable to open swap file for &quot;[No Name]&quot;, recovery impossible</div></td></tr></tbody></table></div>

<p>This is usually harmless, as soon as I save the file everything would be great, it&#8217;s just that 1 &#8211; 2 seconds of pause that doesn&#8217;t make me feel good, so today I tried to pin down the problem and here&#8217;s how it went:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">:help E303<br />
(So it's related to the swap file. Let's see where our swap file fails to be created.)<br />
:set directory?<br />
directory=.;c:\tmp;c:\temp<br />
(Oh, those are the default %TEMP% which I moved to another drive! But anyway, the first priority should be the current directory. So let's see where we're now)<br />
:pwd<br />
C:\Windows\system32</div></td></tr></tbody></table></div>

<h2>Solution</h2>

<p>OK it&#8217;s pretty obvious now. It probably needs a little bit of manual fixing. Let&#8217;s put this in our <code>.vimrc</code></p>

<div class="codecolorer-container vim default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="vim codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;">&quot; Thanks for an anonymous guest to provide this generic, better solution</span><br />
<span style="color: #804040;">set</span> <span style="color: #668080;">directory</span>=<span style="color: #000000;">.</span>,<span style="color: #000000;">$</span>TEMP<span style="color: #adadad; font-style: italic;"><br />
<br />
&quot; This is line I used at first. The above line is better than this</span><br />
<span style="color: #804040;">set</span> <span style="color: #668080;">directory</span>=<span style="color: #000000;">.</span>,d<span style="color: #000000;">:</span>\temp</div></td></tr></tbody></table></div>

<p>Great, problem solved!</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/gvim-e303-unable-to-open-swap-file-for-no-name-recovery-impossible/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

