views:

229

answers:

2

This is somewhat related to another question that I've asked that I've pretty much figured out. The last piece of the puzzle is using CoCreateInstance() instead of GetActiveObject(). I don't want to use an existing instance of EnvDTE, so I call CoCreateInstance, which properly fires off a new instance of VisualStudio. CoCreateInstance() calls AddRef() and I store the output pointer in a CComPtr, which properly calls Release on destruction. When this Release() happens, lo and behold the instance of VS closes! Of course it does because the refcount is at zero. What I'm wanting to do is have the new process own that last instance, so when the user closes VS with the Close (X) button it will destroy the COM object.

There are a few things I have tried: 1. Calling Detach() on my CComPtr, so the object lives on. Sure it works, however, closing VS with the close button doesn't actually kill the process (it's still running in the Task Manager list). 2. Launch a separate process of VS and then use ROT to find the new instance. This is ugly because I have to wait an indeterminate amount of time for the application to launch before trying to find the new instance of the COM object. 3. Use a global or static CComPtr, and manually destroy the object when my app closes. I'd rather not do it this way.

A: 

Look at WindowClosing Event. You could subscribe to that event and when the event is fired call Release(). This will require you to determine which window events to subscribe to.

sharptooth
+1  A: 

So, I've figured this out for the specific case of creating an VisualStudio.DTE object using CoCreateInstance. The DTE object returned has a UserControl property, which can be set to TRUE. When you set this to TRUE, then a Release() of the CComPtr that holds the DTE object doesn't destroy the instance:

#define RETURN_ON_FAIL( expression ) \
result = ( expression ); \
if ( FAILED( result ) )  \
 return false;   \
else // To prevent danging else condition

HRESULT result;
CLSID clsid;
CComPtr<IUnknown> punk = NULL;

CComPtr<EnvDTE::_DTE> dte = NULL;
RETURN_ON_FAIL( ::CLSIDFromProgID(L"VisualStudio.DTE", &clsid) );
RETURN_ON_FAIL( ::CoCreateInstance( clsid, NULL, CLSCTX_LOCAL_SERVER, EnvDTE::IID__DTE, (LPVOID*)&punk ) );
dte = punk;
dte->put_UserControl( TRUE );
Amir Ebrahimi