Working with ATL's CComGITPtr Class

by Codewiz51 March 16, 2011 19:43

I've been working on a series of articles regarding threading and ATL 9.  I recently decided to update Microsoft's sample multithreaded IConnectionPointImpl implementation, called IConnectionPointImplMT.  You can read about the original work on the Microsoft Support WebSite: SAMPLE: ATLCPImplMT encapsulates ATL event firing across COM apartments.  The template uses the Global Interface Table (GIT) to marshal the ATL event interface to a thread that is not executing in the primary thread of the STA.

The component was written for ATL 3.0 and will not compile when used with ATL 9.0.  I decided to update the component to compile with ATL 9 and Visual Studio 2008.

In the course of modifying the code, I decided to use CComGITPtr to handle implementation of IGlobalInterfaceTable. This decision proved more difficult to implement than I first realized.  The problem is, CComGITPtr is revoke happy.  If you do not call Detach after calling Attach, you will end up wondering why you cannot use a GIT cookie more than once.  I finally had to go through the template source to understand that Detach is the only way to clear the pointer without revoking the interface.  You must also be careful that the pointer does not go out of scope before you Detach it, or the interface associated with your GIT cookie will be revoked.

The moral of this story: If you call Attach, you had better call Detach or your interface will be revoked from the GIT.

Here's the updated ATLCPImplMT.h. Update: I uploaded a version of the header that would not compile.  I also found some bugs when I attempted to use the class in a true multi-threaded environment.  This is the latest version.

ATLCPImplMT.1.7z (2.58 kb)

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011