Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Tyring to properly cast an object from the Running Object Table to a DTE pointer

$
0
0

This code worked when I was writing in Managed C++.   But in the code below, where I am now using unmanged C++ ATL classes, I am having trouble on the line that casts the unknown pointer returned by IRunningObjectTable::GetObjectW to a DTE object.  If anyone has any thoughts I would be greatly appreciative:

        _DTE* FindDebuggerDTEPtr()
        {
//            System::Diagnostics::Process^ theProcess = System::Diagnostics::Process::GetCurrentProcess();

            CComPtr<_DTE> dteDebugger = nullptr;
            IRunningObjectTable* theROT;
            IBindCtx* theContext;
            CreateBindCtx(0, &theContext);
            theContext->GetRunningObjectTable(&theROT);

#pragma push_macro("GetObject")
#undef GetObject

            while (dteDebugger == nullptr)
            {
                IEnumMoniker* theMonikerEnumerator;
                theROT->EnumRunning(&theMonikerEnumerator);
                ULONG theReturnedCount;
                ULONG* theCountPtr(&theReturnedCount);
                LPOLESTR theOLEReturnedName(L"");

                IMoniker* theROTMonikers[1];
                IMoniker* theDummyMoniker = nullptr;
                int numTries = 0;
                while ((!theMonikerEnumerator->Next(1,theROTMonikers,theCountPtr)) &&
                       (numTries < 100))
                {
                    try
                    {                    
                        theROTMonikers[0]->GetDisplayName(theContext,theDummyMoniker,&theOLEReturnedName);
                        USES_CONVERSION;
                        char* theReturnedName = OLE2A(theOLEReturnedName);
                        if (!strncmp(theReturnedName,"!VisualStudio.DTE.11.0",22))
                        {
                            IUnknown* pUnk;
                            theROT->GetObjectW(theROTMonikers[0],&pUnk);
                            dteDebugger = dynamic_cast<_DTE*>(pUnk);
                            CComPtr<Debugger> theDebuggerDebugger;
                            dteDebugger->get_Debugger(&theDebuggerDebugger);
                            CComPtr<Processes> theDebuggedProcesses;
                            theDebuggerDebugger->get_DebuggedProcesses(&theDebuggedProcesses);
                            CComPtr<Process> theDebuggedProcess;
                            theDebuggedProcesses->Item(CComVariant(1),&theDebuggedProcess);
                            long ID;
                            if (theDebuggedProcess->get_ProcessID(&ID) == GetCurrentProcessId())
                            {
                                return dteDebugger;
                            }
                            numTries++;
                        }
                    }

                    catch (...)
                    {
                        theMonikerEnumerator->Reset();
                        numTries++;
                    }
                }
            }    
#pragma pop_macro("GetObject")
            return nullptr;
        }


Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>