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?