1

I would like to attach to a remote process with a running Visual Studio instance using pythoncom and COM objects provided by Microsoft Development Environment. So far I am able to acquire from DTE object a debugger object implementing Debugger interface. However, I need to access the Transports property of the debugger. It is implemented by Debugger2 class. So somehow I need Debugger2 instance instead of Debugger. In VB or C++ this can be done by a simple cast (DTE -> DTE2). But how can this be done in Python?

QueryInterface

Approach ends with exception

IID_DTE2 = IID("{2EE1E9FA-0AFE-4348-A89F-ED9CB45C99CF}")

def get_vs_instances():
    rot = pythoncom.GetRunningObjectTable()
    running_objects = rot.EnumRunning()

    while True:
        moniker = running_objects.Next()

        if not moniker:
            break

        ctx = pythoncom.CreateBindCtx(0)
        name = moniker[0].GetDisplayName(ctx, None)

        if name.startswith("!VisualStudio.DTE."):
            obj = rot.GetObject(moniker[0])
            dte = win32com.client.Dispatch(
                obj.QueryInterface(pythoncom.IID_IDispatch))
            dte2 = dte._oleobj_.QueryInterface(IID_DTE2)

TypeError: There is no interface object registered that supports this IID

Invoke

(I took DISPID 1101 of Transports property from Python Object Browser.)

transports = dte.Debugger._oleobj_.Invoke(1101, 0x400, pythoncom.DISPATCH_PROPERTYGET, True)

pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

CastTo does not help either because of error The interface does not appear in the same library as the object. (similar problem)

Any suggestions?

Patrik Polakovic
  • 542
  • 1
  • 8
  • 20
  • You're trying to cast from `IDispatch` to `IDebugger2`. You don't have an `IDebugger` anywhere, so you haven't written the equivalent of a C++ cast from DTE->DTE2 anywhere. Those are only equivalent the class is dispatch-automation-compatible (carries its names around in string form so it can be used VB4-style). Which may be true in this case, but I have no idea whether it is; it should say in the docs. – abarnert May 25 '18 at 17:38
  • IDebugger2 does not exist. There is Debugger2 interface inheriting from Debugger. So DTE.Debugger instance does implement Debugger2 interface. Maybe it can be problem with typelib? – Patrik Polakovic May 25 '18 at 18:15
  • `Debugger2` is a dispatch-only interface? – abarnert May 25 '18 at 18:59
  • Going throught the VS SDK header file - `MIDL_INTERFACE("8B5E2BFD-4642-4efe-8AF4-0B2DA9AAA23C") Debugger2 : public Debugger` defined in dte80.h And `MIDL_INTERFACE("338FB9A0-BAE5-11d2-8AD1-00C04F79E479") Debugger : public IDispatch` defined in dteinternal.h – Patrik Polakovic May 25 '18 at 19:14
  • Does anybody know how to make things like this happen? I also need to access Debugger2 interface from via pywin32. – libbkmz Sep 23 '19 at 18:02

0 Answers0