I am using below code to manipulate visual studio.
Everything worked fine except that smart pointers did not release properly before function exit: it threw exception upon release.:exception 0xc0000005, access violation when write to 0xfeeefeee.
the running stop at the bold part below.
public:
typedef T _PtrClass;
~CComPtrBase() throw()
{
if (p)
p->Release();
}
Could any one give me some hint?
int myAutomateVS()
{
CoInitialize(NULL);
CComPtr<IRunningObjectTable> pTable=NULL;
GetRunningObjectTable(0, &pTable);
CComPtr<IEnumMoniker> pEnumMoniker=NULL;;
pTable->EnumRunning(&pEnumMoniker);
CComPtr<IMoniker> pMoniker=NULL;
ULONG nMoniker=0;
CComPtr<IUnknown> pRunningObj=NULL;
while(pEnumMoniker->Next(1, &pMoniker, &nMoniker) == S_OK)
{
CComPtr<IBindCtx> pCtx=NULL;
CreateBindCtx(0, &pCtx);
LPOLESTR pwszName;
pMoniker->GetDisplayName(pCtx, NULL, &pwszName);
pTable->GetObject(pMoniker, &pRunningObj);
if(_tcsstr(pwszName, L"VisualStudio.DTE.8") != NULL)
break;
}
CComPtr<EnvDTE::_DTE> pDTE;
pDTE = pRunningObj;
CComPtr<EnvDTE::_Solution> solution;
pDTE->get_Solution(&solution);
CComBSTR fullName;
solution->get_FullName(&fullName);
CoUninitialize();
return 0;
}
CoUninitialize
whileCComPtr
objects holding interface pointers are still in scope. They will callRelease()
on their underlying dumb pointers at the closing brace - but that's illegal afterCoUninitialize
pTable->GetObject(pMoniker, &pRunningObj);
This leaks - the interface pointer currently held bypRunningObj
is overwritten and never released.