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.