<?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>Marius Bancila's Blog</title>
	<atom:link href="http://mariusbancila.ro/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Sun, 08 Aug 2010 09:36:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Colors Game Redux</title>
		<link>http://mariusbancila.ro/blog/2010/08/06/colors-game-redux/</link>
		<comments>http://mariusbancila.ro/blog/2010/08/06/colors-game-redux/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 09:50:36 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=655</guid>
		<description><![CDATA[Two days ago I posted a simple implementation of a game of colors. Though it was intended only as an exercise, someone has criticizes the use of an int** to hold the grid information, mainly for two reasons: the footprint on 64-bit platforms can get nasty the explicitly allocated memory, instead of using a std::vector [...]]]></description>
			<content:encoded><![CDATA[<p>Two days ago I <a href="http://mariusbancila.ro/blog/2010/08/04/colors-game/">posted</a> a simple implementation of a game of colors. Though it was intended only as an exercise, someone has criticizes the use of an <strong>int**</strong> to hold the grid information, mainly for two reasons:</p>
<ul>
<li>the footprint on 64-bit platforms can get nasty</li>
<li>the explicitly allocated memory, instead of using a std::vector</li>
</ul>
<p>So this is the code:</p>
<pre class="prettyprint">
int** m_pCells; 

void Create()
{
   m_pCells = new int*[m_nSize];
   for(int i = 0; i < m_nSize; ++i)
      m_pCells[i] = new int[m_nSize];
}
</pre>
<p>Let's see how much memory it takes. The total size should be:</p>
<pre class="prettyprint">
totalsize = sizeof(m_pCells) + sizeof(m_pCells[0]) * m_nSize + m_nSize * m_nSize * sizeof(int);
</pre>
<p>On 32-bit platforms, the size of a pointer is the same with the size of int and is 4 bytes. For the maximum size allowed for my grid, which is 50, the total size in bytes for the grid is: 4 + 4*50 + 50*50*4 = 10204.</p>
<p>On 64-bit platforms, the size of a pointer is 8 bytes, but the size of int is still 4 bytes. So for a grid with 50 rows and columns it needs: 8 + 8*50 + 50*50*4 = 10408 bytes. That's a 2% increase of required memory.</p>
<p>The memory footprint was the last thing I had in mind when I wrote this simple exercise. Well, of course there is a way to require only 4 more bytes on 64-bit platforms. And that is using an <strong>int*</strong> allocating m_nSize*m_nSize elements.</p>
<pre class="prettyprint">
int* m_pCells;

void Create()
{
   m_pCells = new int[m_nSize * m_nSize];
}

void Destroy()
{
   delete [] m_pCells;
   m_pCells = NULL;
}
</pre>
<p>With this implementation, when you need to access the element at row i and column j, you have to use <strong>m_pCells[i * m_nSize + j]</strong>.</p>
<p>As for the second argument, that explicitly using operator new[] to allocate memory instead of using a vector of vectors, well, what could I say? Sure, why not. Different people use different programming styles. As long as all implementation are correct and achieve the same goal with similar performance, I guess everyone is entitle to code as he/she wants. But if we go back to the memory footprint, I would also guess that the use of vectors would take more memory than pointers to int, because the size of a vector is several times the size of a pointer. But I wouldn't say that's an important issue here. </p>
<p>Anyway, these arguments remember me the joke (or maybe it's serious) about the rules of optimization:</p>
<ol>
<li>Don't optimize.</li>
<li>Don't optimize yet (for experts only).</li>
</ol>
<p>(Of course there are super experts that can ignore these rules.)</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/08/06/colors-game-redux/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Colors Game</title>
		<link>http://mariusbancila.ro/blog/2010/08/04/colors-game/</link>
		<comments>http://mariusbancila.ro/blog/2010/08/04/colors-game/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 14:08:04 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[games]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=649</guid>
		<description><![CDATA[One of the games I like the most on my new phone is about covering a grid formed by cells of different colors with a single color within a limited number of moves. After playing it again and again for a week, I decided to write my own game for the PC. The rules are: [...]]]></description>
			<content:encoded><![CDATA[<p>One of the games I like the most on my new phone is about covering a grid formed by cells of different colors with a single color within a limited number of moves. After playing it again and again for a week, I decided to write my own game for the PC.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/08/colorsgame.png" title="Colors Game" class="alignnone" width="773" height="596" /></p>
<p>The rules are:</p>
<ul>
<li>the grid has an equal number of rows and columns, that can vary from 5 to 50</li>
<li>cells are colored with six colors</li>
<li>coloring the grid always starts from the top left cell</li>
<li>adjacent cells having the same color form a single shape; game ends when this shape covers the entire grid</li>
<li>to change the color of the growing shape, use the six buttons available on the right side of the grid</li>
<li>grid must be covered within a number of moves; if you exceed that number of moves you lose</li>
<li>when you win, you automatically get to the next level</li>
<li>you can change the size of the grid from the menu</li>
</ul>
<p>Here are the available downloads: <a href="http://www.mariusbancila.ro/blog/wp-content/uploads/2010/08/ColorsGame_src.zip">the sources</a>, and <a href="http://www.mariusbancila.ro/blog/wp-content/uploads/2010/08/ColorsGame_binary.zip">the executable</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/08/04/colors-game/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resources for the F# Presentation at Ronua Roadshow</title>
		<link>http://mariusbancila.ro/blog/2010/07/18/resources-for-the-f-presentation-at-ronua-roadshow/</link>
		<comments>http://mariusbancila.ro/blog/2010/07/18/resources-for-the-f-presentation-at-ronua-roadshow/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 08:51:49 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[ronua]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=641</guid>
		<description><![CDATA[For those that attended my last evening presentation about F# at Ronua Roadshow in Timisoara (but not only), here is the demo I&#8217;ve shown, and one that I planned to show but didn&#8217;t due to lack of time. The purpose of these demos was to shown simple Windows Forms applications written in F#. Mandelbrot Fractal [...]]]></description>
			<content:encoded><![CDATA[<p>For those that attended my last evening presentation about F# at Ronua Roadshow in Timisoara (but not only), here is the demo I&#8217;ve shown, and one that I planned to show but didn&#8217;t due to lack of time. The purpose of these demos was to shown simple Windows Forms applications written in F#.</p>
<p><strong>Mandelbrot Fractal</strong><br />
A Mandelbrot set is a set of points in the complex plane, whose boundary forms a fractal. The fractal, known as Mandelbrot fractal, is obtain by associating a color with each point in the complex plane (or rather a subset of it). The color is chosen based on the result of computing the value of the complex quadratic polynomial Z(n+1) = Z(n)^2 + c for a number of iterations (100, 200, etc.). You can read more about it on <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Wikipedia</a>.</p>
<p>The program that I shown exhibits traits of both functional (for computing the fractal) and object oriented (for displaying the fractal) paradigms. It is a variation of the program available <a href="http://secretgeek.net/fsharp_mandelbrot2.asp">here</a>, for which I kept the functional part (computing the Mandelbrot set is not very fast, I must warn you), but redone the user interface part. You can use the mouse to drag the fractal and the wheel to zoom in and out.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/07/fsharp_mandelbrot.png" title="Mandelbrot fractal" class="alignnone" width="600" height="414" /></p>
<p>You can download it from <a href="http://www.mariusbancila.ro/blog/wp-content/uploads/2010/07/mandelbrot.zip">here</a>.</p>
<p><strong>Game of Life</strong><br />
I <a href="http://mariusbancila.ro/blog/2008/05/08/game-of-life-in-f/">blogged</a> about this two years ago, when F# was still far from a final release. In the meantime, syntax has changed, classes have changed, so if you try to run that implementation of mine you&#8217;ll run into some errors. I have updated the code to run correctly with Visual Studio 2010.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/07/fsharp_gameoflife.png" title="Game of Life" class="alignnone" width="641" height="483" /></p>
<p>You can download it from <a href="http://www.mariusbancila.ro/blog/wp-content/uploads/2010/07/mandelbrot.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/07/18/resources-for-the-f-presentation-at-ronua-roadshow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Find the Source of Memory Leaks</title>
		<link>http://mariusbancila.ro/blog/2010/07/12/how-to-find-the-source-of-memory-leaks/</link>
		<comments>http://mariusbancila.ro/blog/2010/07/12/how-to-find-the-source-of-memory-leaks/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 08:36:02 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=638</guid>
		<description><![CDATA[When you run your (unmanaged/C++) application in debugger, you see at the end a report of memory leaks (if any are detected). Something like this: Detected memory leaks! Dumping objects -> f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {381} normal block at 0x001FFC30, 54 bytes long. Data: < x > 0C 00 B9 78 12 00 00 00 12 00 [...]]]></description>
			<content:encoded><![CDATA[<p>When you run your (unmanaged/C++) application in debugger, you see at the end a report of memory leaks (if any are detected). Something like this:</p>
<blockquote><p>Detected memory leaks!<br />
Dumping objects -><br />
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {381} normal block at 0x001FFC30, 54 bytes long.<br />
Data: < x > 0C 00 B9 78 12 00 00 00 12 00 00 00 01 00 00 00<br />
d:\marius\vc++\memoryleakstest\memoryleakstestdlg.cpp(163) : {380} normal block at 0x001FFBF0, 4 bytes long.<br />
Data: <@ > 40 FC 1F 00<br />
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp(306) : {374} client block at 0x001FFA38, subtype c0, 68 bytes long.<br />
a CWinThread object at $001FFA38, 68 bytes long<br />
Object dump complete. </p></blockquote>
<p>Some of them can be fixed immediately, because when you double click on them Visual Studio will take you to the line where the allocation was made. Some of them are harder to spot, because Visual Studio is not able to do the same. Question is how do you find the source of those allocations? Luckily, there is this global variable called <strong>_crtBreakAlloc</strong>, that can be used to force the debugger to stop the execution when a certain block is allocated.</p>
<p>In order to use that, you should follow several steps:</p>
<ul>
<li>First, you have to find a reproducible sequence that produces the same memory allocation number. When memory blocks are allocated, they are identified with a number, called allocation number. This number is reported by Visual Studio in brackets when it lists the memory leaks (e.g. {381}).</li>
<li>Second, you have to put a breakpoint somewhere in your program to force a stop at the beginning of the execution. That means you could use function main(), your CWinApp derived class constructor, function InitInstance() or other, depending on your application type and how early in the execution of the program the block that leaks is allocated. The smaller the allocation number is, the earlier in the execution the allocation occurs.</li>
<li>Run your program in debugger.</li>
<li>When the program stops (at the first breakpoint) open the Watch window and add the following expression: <strong>{,,msvcr90d.dll}_crtBreakAlloc</strong> in the Name column. In the Value column (which by default should have the value -1) write the allocation number. Take notice that msvcr90d.dll (which is the DLL that contains the C++ runtime library) is specific to Visual Studio 2008 (and is the debug version). If you use another version of Visual Studio, you have to use the appropriate DLL.</li>
<li>Continue debugging.</li>
<li>When the block identified by the allocation number set in the Watch window is allocated, the debugger will stop the execution and jump to a line from dbgheap.c.<br />
<img alt="" src="/blog/wp-content/uploads/2010/07/vcdeb_memoryleak1.png" title="Set a breakpoint and add _crtBreakAlloc in Watch window" class="alignnone" width="638" height="247" /><br />
In order to see the line in your code that triggered the allocation, open the Call Stack window and find, from top down, the first function from your own code.<br />
<img alt="" src="/blog/wp-content/uploads/2010/07/vcdeb_memoryleak2.png" title="Find the source of the allocation" class="alignnone" width="722" height="537" /><br />
That will lead you to the source of the memory leak.</li>
</ul>
<p>To read more about this topic see:<br />
<a href="http://msdn.microsoft.com/en-us/library/w2fhc9a3%28VS.80%29.aspx">How to: Set Breakpoints on a Memory Allocation Number</a><br />
<a href="http://support.microsoft.com/kb/151585">How to use _crtBreakAlloc to debug a memory allocation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/07/12/how-to-find-the-source-of-memory-leaks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>My Prediction About Psychic Octopus Paul</title>
		<link>http://mariusbancila.ro/blog/2010/07/07/my-prediction-about-psychic-octopus-paul/</link>
		<comments>http://mariusbancila.ro/blog/2010/07/07/my-prediction-about-psychic-octopus-paul/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 16:10:55 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[football]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=628</guid>
		<description><![CDATA[If you&#8217;re a football fan you must have heard about the octopus called Paul that has predicted the outcome of all Germany&#8217;s matches at the World Cup 2010. She lives in the Sea Life Aquarium in the western city of Oberhausen. Before each match she was given two jars with a mussel and a national [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a football fan you must have heard about the octopus called Paul that has predicted the outcome of all Germany&#8217;s matches at the World Cup 2010. She lives in the Sea Life Aquarium in the western city of Oberhausen. Before each match she was given two jars with a mussel and a national flag inside. And every time she picked the mussel from the jar with the flag of the correct winner. Yesterday she <a href="http://news.bbc.co.uk/2/hi/world/europe/10521867.stm">picked Spain over Germany</a>.</p>
<p>Now, I want to make a prediction about her current and next predictions: she got the result of tonight&#8217;s match wrong, Germany will win. And then, she&#8217;ll have to chose between Netherlands and Germany and will pick the Netherlands. Now, the prediction is not that much about who wins (it doesn&#8217;t matter actually who wins), <em>it&#8217;s about how Paul makes the picks</em>.</p>
<p>How did I do this prediction? Well, I see patterns. <img src='http://mariusbancila.ro/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  And here is Paul&#8217;s prediction pattern. She is giving two jars, arranged by the teams order:<br />
<strong>Germany</strong> &#8211; Australia<br />
Germany &#8211; <strong>Serbia</strong><br />
Ghana &#8211; <strong>Germany</strong><br />
<strong>Germany</strong> &#8211; England<br />
Argentina &#8211; <strong>Germany</strong><br />
Germany &#8211; <strong>Spain</strong></p>
<p>She&#8217;s picking left, right, right, left, right, right. The next match will be between Netherlands and Germany and she will pick left, thus Netherlands.</p>
<p>Let&#8217;s see how good I am with predictions.</p>
<p>UPDATE: OK, psychic Paul was right once again. <img src='http://mariusbancila.ro/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  But I still predict that it will pick left, thus, Uruguay for Germany&#8217;s next match.</p>
<p>SECOND UPDATE: so Paul has picked Germany over Uruguay, and Spain over Netherlands. That completely ruins my prediction. <img src='http://mariusbancila.ro/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/07/07/my-prediction-about-psychic-octopus-paul/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>COM Apartments</title>
		<link>http://mariusbancila.ro/blog/2010/06/28/com-apartments/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/28/com-apartments/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 16:39:51 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[COM]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apartments]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[MTA]]></category>
		<category><![CDATA[NTA]]></category>
		<category><![CDATA[STA]]></category>
		<category><![CDATA[thread-safe]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=571</guid>
		<description><![CDATA[If you work with COM, apartments is one of the concepts you must comprehend, because it&#8217;s an important topic. Before explaining what apartments are let&#8217;s think about classes and objects regardless of COM. When you build a class, you know (or you should) whether objects of that class will be used from a single thread, [...]]]></description>
			<content:encoded><![CDATA[<p>If you work with COM, apartments is one of the concepts you must comprehend, because it&#8217;s an important topic. Before explaining what apartments are let&#8217;s think about classes and objects regardless of COM. When you build a class, you know (or you should) whether objects of that class will be used from a single thread, or from multiple threads. In the later case, if those threads might access shared data at the same time, you must synchronize the access to that data (using critical sections, mutexes, or others). So when you create your class you either make it thread-safe or not. If it&#8217;s not thread safe, objects of that class can only be accessed from one thread at a time, if it&#8217;s thread-safe, then objects of that class can be accessed from different threads at the same time.</p>
<p>Now, the same rule applies in the COM world. Your coclasses can either be thread-safe or not. If the are thread-safe, you can access one object from different threads at a time, otherwise not. Here enter the apartments. So what is an apartment? An apartment is an environment in which COM objects can live. It&#8217;s not a thread, nor a process, but it handles access from COM clients to COM objects. There are several types of apartments: single-threaded apartments (STA), multi-threaded apartments (MTA) and neutral-threaded apartments (NTA).</p>
<p><strong>Single-Threaded Apartments</strong><br />
An STA allows only one thread at a time to access a COM object. This is  achieved using a hidden window with a message pump. Calls from clients living in different threads are queued with the message pump. Only when the current call from a thread ends, the next call can proceed.</p>
<p>Assume you had a COM object called CoUserGroups that implements an interface IUserGroups that provides two methods: one called Add that adds a new user group, and one called Delete that deletes a user group. Since both methods work on the same list of user groups, adding and deleting is not possible at the same time without synchronizing access. But since such synchronization was not implemented, the COM object specifies that it can leave only in an STA, and let the apartment do the synchronization of calls from clients living in different threads.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_sta.png" title="COM STA" class="alignnone" width="688" height="326" /></p>
<p><strong>Multi-Threaded Apartments</strong><br />
An MTA allows any number of threads to access a COM objects. However, the COM objects must be thread-safe, otherwise your application will behave unexpectedly and even crash.</p>
<p>Going back to the previous example, if CoUserGroups was implemented in a thread-safe manner, then it would be possible for clients living in different threads to access it. In this case there would be no need for an apartment level synchronization. The COM coclass specifies that it can live in an MTA and when simultaneous calls from different threads are received they are directed immediately to the COM object. This situation is shown in the next image.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_mta.png" title="COM MTA" class="alignnone" width="688" height="327" /></p>
<p><strong>Neutral-Threaded Apartments</strong><br />
NTAs, like MTAs, allow multiple threads to enter one apartment, but once a thread entered the apartment it acquires an apartment wide lock that will block the other threads, until the current thread exists the apartment. Calls into NTA (from STA or MTA in the same process) do not generate context switches; the thread leaves the apartment in which it executes and enters the NTA without any context switch, which increases performance. This model was introduced with COM+ (in Windows 2000) and is meant for components with no user interface. </p>
<p>A process can contain several apartments:</p>
<ul>
<li>zero or one MTA</li>
<li>zero or one NTA</li>
<li>zero, one or several STAs; the first STA created for a process is called the main STA</li>
</ul>
<p>As a COM client, you specify the apartment you want to join with a call to CoInitializeEx(). This methods must be called from each thread.</p>
<pre class="prettyprint">
HRESULT CoInitializeEx(void * pvReserved, DWORD dwCoInit);
</pre>
<p>The second parameters is a set of flags specifying the initialization options for the thread. To join the unique MTA, use COINIT_MULTITHREADED. To join a new or existing STA, use COINIT_APARTMENTTHREADED. Function CoInitialize() calls CoInitializeEx() specifying COINIT_APARTMENTTHREADED for the flags. </p>
<p><strong>How to specify the threading model allowed for a coclass?</strong><br />
A coclass can specify the type of apartment it can join. If you&#8217;re using ATL you can specify that when you create the coclass. The next image shows the available options:</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_atlapartment.png" title="ATL apartment choices" class="alignnone" width="615" height="520" /><br />
What they mean:</p>
<ul>
<li><strong>Single</strong>: object wants to join the main STA (the first STA created into the process)</li>
<li><strong>Apartment</strong>: object wants to join one of the STAs in the process</li>
<li><strong>Both</strong>: object wants to join either an STA or the MTA</li>
<li><strong>Free</strong>: object wants to join the MTA</li>
<li><strong>Neutral</strong>: object wants to join the NTA</li>
</ul>
<p>ATL adds the appropriate value to the registry script it creates for your coclass. COM depends entirely on the registry, and the threading model is also specified in the registry. Here is an example:</p>
<pre class="prettyprint">
HKCR
{
	NoRemove CLSID
	{
		ForceRemove {80CFA233-86CC-44E3-9A62-BC498D8F2A0E} = s 'CoUserGroups Class'
		{
			ForceRemove Programmable
			InprocServer32 = s '%MODULE%'
			{
				val ThreadingModel = s 'Apartment'
			}
			TypeLib = s '{B8965B61-2A5A-4F34-B9F8-D8859452D345}'
			Version = s '1.0'
		}
	}
}
</pre>
<p>When that is merged into the Windows Registry, it looks like this:<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/com_regthreadmodel.png" title="Threading model in Registry" class="alignnone" width="793" height="370" /></p>
<p>The possible values in registry are:</p>
<ul>
<li>no value specified: equivalent of ATL &#8216;Single&#8217;</li>
<li><strong>Apartment</strong>: equivalent of ATL &#8216;Apartment&#8217;</li>
<li><strong>Free</strong>: equivalent of ATL &#8216;Free&#8217;</li>
<li><strong>Both</strong>: equivalent of ATL &#8216;Both&#8217;</li>
<li><strong>Neutral</strong>: equivalent of ATL &#8216;Neutral&#8217;</li>
</ul>
<p>If you want to read more about COM apartments I suggests articles like this <a href="http://www.codeguru.com/cpp/com-tech/activex/apts/article.php/c5529">one</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/28/com-apartments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I Just Want to Delete the Cache</title>
		<link>http://mariusbancila.ro/blog/2010/06/15/i-just-want-to-delete-the-cache/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/15/i-just-want-to-delete-the-cache/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 12:31:58 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Windows Programming]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=565</guid>
		<description><![CDATA[I recently read an excerpt from Joel Spolsky&#8217;s book User Interface Design For Programmers available on his (former) blog. This is a great book about designing user interfaces, with examples of bad and good ideas. I&#8217;m getting the printed version and I recommend this to all building UIs. Now, yesterday I had a problem with [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read an excerpt from Joel Spolsky&#8217;s book User Interface Design For Programmers <a href="http://www.joelonsoftware.com/uibook/fog0000000249.html">available on his (former) blog</a>. This is a great book about designing user interfaces, with examples of bad and good ideas. I&#8217;m getting the printed version and I recommend this to all building UIs.</p>
<p>Now, yesterday I had a problem with Google Documents (which no longer worked on my WinXP machine with any browser, as it continuously unsuccessfully tried to download a 237 bytes file called download.gz) and I though that deleting the cache might help. So I opened Google Chrome options dialog and then the Clear Browsing Data dialog. It looks like this.</p>
<p><img class="alignnone" title="Google Chrome - Clear Browsing Data" src="/blog/wp-content/uploads/2010/06/chromeobliterate.png" alt="" width="384" height="353" /></p>
<p>I was stunned to see the dialog started with the phrase &#8220;Obliterate the following items&#8221;. For God&#8217;s sake what is obliterating? I want to delete the cache, I don&#8217;t care about obliterating, and whatever that means. I don&#8217;t want to fetch a dictionary and look-up for the meaning of the word. Excuse me Mr. Google Programmer, that I (probably like many of the people of this world who are not native English speakers) don&#8217;t know what &#8220;obliterate&#8221; means. Yes, it does sound archaic, it does sound like J.R.R. Tolkien but it stops me doing my work. All I wanted to do was selecting what to delete and delete. Instead I spent time figuring whether that means to select what to keep or select what to delete (luckily it didn&#8217;t say purge). I had doubts so I had to search for the meaning of the word. Sure, now I know one more (fancy) English word, but overall, I lost one minute doing anything else but deleting the cache.</p>
<p>So, my recommendation to you Mr. Google Programmer is to read Joel Spolsky&#8217;s book. I&#8217;m sure there are things you can learn from it. And start using simple words and sentences that everyone understands.</p>
<p>As for what &#8220;obliterate&#8221; means, here it is:</p>
<ul>
<li>Mark for deletion, rub off, or erase</li>
<li>Make undecipherable or imperceptible by obscuring or concealing</li>
<li>Remove completely from recognition or memory</li>
<li>Do away with completely, without leaving a trace</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/15/i-just-want-to-delete-the-cache/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Office 2010 and How the Story Ends</title>
		<link>http://mariusbancila.ro/blog/2010/06/08/office-2010-and-how-the-story-ends/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/08/office-2010-and-how-the-story-ends/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 06:25:31 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Office]]></category>
		<category><![CDATA[Office2010]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=561</guid>
		<description><![CDATA[A week ago I was reporting about the problems I had experienced with Office 2010. That was not the end of the story, and it&#8217;s probably fair enough to tell how it ends. After installing Office 2010 64 bit and then 32 bit and both crashing every other 5 minutes, I returned to Office 2007. [...]]]></description>
			<content:encoded><![CDATA[<p>A week ago I was reporting about the <a href="http://mariusbancila.ro/blog/2010/05/31/a-series-of-misfortunate-events-with-office-2010/">problems I had experienced with Office 2010</a>. That was not the end of the story, and it&#8217;s probably fair enough to tell how it ends. </p>
<p>After installing Office 2010 64 bit and then 32 bit and both crashing every other 5 minutes, I returned to Office 2007. But then I got the help of Microsoft Support who provided a couple of scripts to clean-up the Windows Registry of anything related to Office 2007 and Office 2010. (The script for Office 2007 is available <a href="http://support.microsoft.com/kb/971179/">here</a>; the one for Office 2010 is not yet published). I have then installed Office 2010 32 bit on a clean Registry and now all applications in the package work like a charm. No hanging, no crashing whatsoever. And I must say I was impressed by the support people, how they helped, how they called to check if everything was working, etc. The kind of support I wish to see everywhere.</p>
<p>I also learned that installing the 64 bit version is not recommended unless you really have some demands, like working with huge files, over 2GB (though I&#8217;m not sure what are the scenarios when one has Excel files bigger than 2GB). Add-ins for Office 32 bit might not work with the 64 bit version. (Though the add-ins I had in 2007 worked without any update when I installed Office 2010 32 bit.) More information about the 64 bit of Office 2010 can be found in this <a href="http://technet.microsoft.com/en-us/library/ee681792.aspx">TechNet article</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/08/office-2010-and-how-the-story-ends/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VSBuildStatusAddin with Auto Show and Auto Hide</title>
		<link>http://mariusbancila.ro/blog/2010/06/02/vsbuildstatusaddin-with-auto-show-and-auto-hide/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/02/vsbuildstatusaddin-with-auto-show-and-auto-hide/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 14:29:24 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[addin]]></category>
		<category><![CDATA[VS2005]]></category>
		<category><![CDATA[VS2008]]></category>
		<category><![CDATA[VS2010]]></category>
		<category><![CDATA[VSBuildStatus]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=557</guid>
		<description><![CDATA[A new version (1.3) of VSBuildStatus add-in for Visual Studio 2005, 2008 and 2010 is available. It allows you to configure the add-in window to automatically show up when a build/clean/deploy process starts, and/or automatically close when the operation ends. To enable the automatic show of the add-in window when a build/clean/deploy operation starts, check [...]]]></description>
			<content:encoded><![CDATA[<p>A new version (1.3) of <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/2A2293B4-1808-44AA-B030-661F6803D8A1">VSBuildStatus</a> add-in for Visual Studio 2005, 2008 and 2010 is available. It allows you to configure the add-in window to automatically show up when a build/clean/deploy process starts, and/or automatically close when the operation ends. </p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/VSBuildStatusAddin1.png" title="Addin window" class="alignnone" width="786" height="375" /></p>
<ul>
<li>To enable the automatic show of the add-in window when a build/clean/deploy operation starts, check <strong>Pop-out automatically when starting a build</strong></li>
<li>To enable the automatic hiding of the add-in window when the build/clean/deploy operation ends, check <strong>Auto hide when the build ends</strong>
<ul>
<li>you can set a delay interval for the hiding, varing from 0 to 300 seconds; if the delay is 0, the window is hidden immediatelly after the build ends</li>
<li>to keep the window shown when error(s) occurred during the build/clean/deploy operation, check <strong>DO NOT auto hide when an error occurs</strong></li>
</ul>
</li>
</ul>
<p>Here is a screen short of the properties window. It opens from the Settings button.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/VSBuildStatusAddin3.png" title="Properties window" class="alignnone" width="300" height="300" /></p>
<p>The add-in is available on the <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/2A2293B4-1808-44AA-B030-661F6803D8A1">Visual Studio Gallery</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/02/vsbuildstatusaddin-with-auto-show-and-auto-hide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>COM and Registry</title>
		<link>http://mariusbancila.ro/blog/2010/06/01/com-and-registry/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/01/com-and-registry/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 09:27:21 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[COM]]></category>
		<category><![CDATA[Windows Programming]]></category>
		<category><![CDATA[registry]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=546</guid>
		<description><![CDATA[If you are working with COM there are several registry entries that are important and that you need to understand. I will try in this post to clarify them. But before that, let&#8217;s enumerate the three possible COM server scenarios. (As a side note, a COM server is a DLL or EXE can contains one [...]]]></description>
			<content:encoded><![CDATA[<p>If you are working with COM there are several registry entries that are important and that you need to understand. I will try in this post to clarify them. But before that, let&#8217;s enumerate the three possible COM server scenarios. (As a side note, a COM server is a DLL or EXE can contains one or more COM objects; a client is an entity that uses a COM object, which means a COM server can also be the client of another COM server.)</p>
<ul>
<li><strong>inproc</strong>: the COM server is loaded into the client process; in this case accessing the COM methods is as simple as using an interface pointer when the client and the in-proc server are in the same thread</li>
<li><strong>local</strong>: the COM server and the client are loaded in different processes on the same machine; communication is achieved with the use of proxies and stubs via Lightweight RPC</li>
<li><strong>remote</strong>: the COM server and the client are loaded in different processes on different machines; communication is achieved with the use of proxies and stubs via RPC</li>
</ul>
<p>In order for the COM Library to be able to locate and instantiate the COM objects correctly, different information is stored in the Windows Registry. The most important information is located under the keys CLSID, TypeLib, Interface and AppID from HKEY_CLASSES_ROOT. The images below show examples for IIS.</p>
<p><strong>HKEY_CLASSES_ROOT\CLSID</strong><br />
Every coclass from the COM server has at least an entry under this key, specifying the CLSID and the ProgID. The CLSID is a GUID that uniquely identifies the class, and the ProgID (programmatic identifier) is a string that identifies the class in a more human readable form. Each ProgID is mapped to a CLSID. The following image shows the mapping for the IIS ProgID.<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_clsid1.png" title="IIS ProdID" class="alignnone" width="795" height="296" /></p>
<p>For each COM class, there must be a key under HKEY_CLASSES_ROOT\CLSID, with the CLSID as the name. The most important sub-keys here are:</p>
<ul>
<li><strong>ProgID</strong>: the programmatic identifier, mapped on the CLSID; cam contain a version number (as a suffix)</li>
<li><strong>VersionIndependentProgID</strong>: a programmatic identifier without the version information</li>
<li><strong>InprocServer32</strong>: specifies the path of the DLL for the inproc servers</li>
<li><strong>LocalServer32</strong>: specifies the path of the COM executable for the local (out-of-process) servers</li>
<li><strong>TypeLib</strong>: indicates the LIBID of the type library that contains the coclass</li>
</ul>
<p>Here are several screenshots from registry for IIS CLSID.<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_clsid2.png" title="IIS InprocServer32" class="alignnone" width="795" height="296" /><br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_clsid3.png" title="IIS ProgID" class="alignnone" width="797" height="295" /><br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_clsid4.png" title="IIS TypeLib" class="alignnone" width="795" height="296" /></p>
<p><strong>HKEY_CLASSES_ROOT\TypeLib</strong><br />
Each type library has a key under HKEY_CLASSES_ROOT\TypeLib, with the name of the LIBID. The sub-keys provide information about the physical location of the type library file (.tlb file) and others, such as flags (FLAGS key) the directory that contains the help file for the type library (HELPDIR key). A LIBID is a GUID that uniquely identifies a type library. A *.TLB file is a binary version of an IDL file. This is used by languages such as VB, Java, Javascript and many others in order to be able to use the COM objects.</p>
<p>The following image shows the type lib information for IIS.<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_typelib.png" title="IIS Typelib" class="alignnone" width="795" height="296" /></p>
<p><strong>HKEY_CLASSES_ROOT\Interface</strong><br />
Information for all interfaces defined in an IDL file (type library) must be added to the registry. Under HKEY_CLASSES_ROOT\Interface must be a key with the IID of the interface. The IID (interface identifier) is a GUID that uniquely identifies an interface. The most important sub-keys are:</p>
<ul>
<li><strong>TypeLib</strong>: the LIBID of the type library containing the interface</li>
<li><strong>ProxyStubClsid32</strong>: the CLSID of the marshaller used to marshal the interface; value <em>{00020424-0000-0000-C000-000000000046}</em> identifies oleaut32.dll, which is the universal marshaler.</li>
</ul>
<p>The following images show the registry entry for the interface IISBaseObject.<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_interface1.png" title="IIS interfaces" class="alignnone" width="795" height="296" /><br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_interface2.png" title="IIS interfaces" class="alignnone" width="795" height="296" /></p>
<p><strong>HKEY_CLASSES_ROOT\AppID</strong><br />
An AppID (application identifier) is a GUID that uniquely identifies a COM server and is used to describe security and activation settings; it is used for out-of-proc (local or remote) scenarios. Usually the AppID is the same with a CLSID of a coclass from the COM server (without the risk of collision, because the CLSID and the AppID server different purposes). The most important settings are:</p>
<ul>
<li><strong>AccessPermission</strong>: defines the ACL of users that can access the server</li>
<li><strong>AuthenticationLevel</strong>: defines the authentication level</li>
<li><strong>DllSurrogate</strong>: identifies the surrogate that can host the DLL; if no value is specified, the default dllhost.exe surrogate is used</li>
<li><strong>LaunchPermissions</strong>: defines the ACL of users that can launch the application</li>
<li><strong>RemoteServerName</strong>: identifies the remove server machine</li>
</ul>
<p>The following images shows the values for an AppID key.<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/comreg_appid.png" title="AppID example" class="alignnone" width="797" height="295" /></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/01/com-and-registry/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.674 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-09-03 21:14:19 -->
