Communicating with MSVC++ via COM: how?

E

Eric Brunel

Hi all,

We're trying to communicate with Microsoft Visual C++ 6.0 via COM to make it
perform a few operations. Our main need is to set a breakpoint on a given line
in a given file. We ended up with the following script:

---------------------------------------------
import win32com.client
import time

## Get MSVC++ main application
app = win32com.client.Dispatch('MSDev.Application')

## Make the window visible
time.sleep(2)
app.Visible = 1

## Get loaded documents in MSVC++ and close them all
docs = app.Documents
docs.CloseAll()

## Load pseudo-workspace for our executable
workSpace = "C:\\tmp\\AllCheckings\\AllCheckings\\Debug\\AllCheckings.exe"
docs.Open(workSpace, 'Auto', 1)

## Open file where we want to put the breakpoint
docs.Open( "C:\\tmp\\AllCheckings\\ccg\\MyFile.c" )

## Get MSVC++ debugger
dbgr = app.Debugger

## Get breakpoint list
bkpts = dbgr.Breakpoints

## Clear all break points, then add the one we want
bkpts.RemoveAllBreakpoints()
bkpts.AddBreakpointAtLine(86)
---------------------------------------------

This script doesn't work: the call to docs.CloseAll fails with the error:

Traceback (most recent call last):
File "test.py", line 13, in ?
docs.CloseAll()
TypeError: object of type 'int' is not callable

which is quite weird, to say the least...

Since we didn't understand what was going on, we finally decided to comment out
this line and to try again. The script then fails on the last line with the error:

Traceback (most recent call last):
File "test.py", line 30, in ?
bkpts.AddBreakpointAtLine(86)
File "H:\Tools\Python\bin\Windows\Python21\win32com\client\dynamic.py", line
151, in __call__
return
self._get_good_object_(apply(self._oleobj_.Invoke,allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352573, 'Membre introuvable.', None, None)

The weird thing is that apparently, something is actually called, since a
breakpoint *is* set, but not on the line we requested. It seems to be set on the
line where the cursor is in the file opened in MSVC++.

In fact, it seems like just doing docs.CloseAll or bkpts.AddBreakpointAtLine
(without the ()) does not return the method, but actually calls it.

We tried to use makepy to generate the COM support files for the objects we
used, but the result were even weirder: the line "app.Visible = 1" crashed,
saying that the attribute could not be set. Using gencache.EnsureModule didn't
help: it also crashed because of an attribute MinorVersion missing on a module.


Did we miss something here? We're somewhat rookies wrt COM, but we're quite
fluent with Python (apparently, this doesn't seem to help a lot...). Did anyone
succeed in communicating with MSVC++ via COM from Python? Or did anyone see the
kind of problems we're having here? We're using Python 2.1 with win32all-136.

TIA
 
M

Mark Hammond

Eric said:
This script doesn't work: the call to docs.CloseAll fails with the error:

Traceback (most recent call last):
File "test.py", line 13, in ?
docs.CloseAll()
TypeError: object of type 'int' is not callable

This is because docs.CloseAll is making the call, and returning an int.
This, docs.CloseAll() is attempting to call that int.

It looks very much like you have no makepy support for MSVC built.

Just after the line:
app = win32com.client.Dispatch('MSDev.Application')
Try adding:
app = win32com.client.gencache.EnsureDispatch(app)

That should force makepy, and everything should start working.

Mark
 
E

Eric Brunel

Mark said:
This is because docs.CloseAll is making the call, and returning an int.
This, docs.CloseAll() is attempting to call that int.

Hi Mark,

Thanks for your answer. We finally figured out this one, but the problem lies a
bit farther in the script:

bkpts.AddBreakpointAtLine(86)

The method AddBreakpointAtLine takes an *optional* parameter which is the line
number on which to put the breakpoint. But bkpts.AddBreakpointAtLine actually
calls the method AddBreakpointAtLine with no parameter, so I just can't figure
out how to call the method *with* a parameter...
It looks very much like you have no makepy support for MSVC built.

Just after the line:
app = win32com.client.Dispatch('MSDev.Application')
Try adding:
app = win32com.client.gencache.EnsureDispatch(app)

That should force makepy, and everything should start working.

I tried what you suggest, but it didn't work: I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "H:\Tools\Python\bin\Windows\Python21\win32com\client\gencache.py", line
423, in EnsureDispatch
disp = win32com.client.Dispatch(prog_id)
File "H:\Tools\Python\bin\Windows\Python21\win32com\client\__init__.py", line
95, in Dispatch
return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo,
UnicodeToString, clsctx)
File "H:\Tools\Python\bin\Windows\Python21\win32com\client\__init__.py", line
26, in __WrapDispatch
typeinfo = dispatch.GetTypeInfo()
File "H:\Tools\Python\bin\Windows\Python21\win32com\client\dynamic.py", line
438, in __getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: MSDev.Application.GetTypeInfo

Trying to force the generation of the support files via an explicit makepy.py
didn't work either: doing a Dispatch('MsDev.Application') still doesn't get the
right type. Forcing the type by doing Dispatch('MsDev.Application',
resultCLSID="{the-class-id}") seems to work, but apparently, the generated class
knows nothing about the types of its members and simple things as:

app.Visible = 1

fail with a traceback (something like "incorrect type for variable" - sorry for
the imprecision: we have a French Windows, so the message I get is in
French...). Getting an attribute like app.Debugger seems to work, but printing
it says:
<COMObject Debugger>
so the object is a dynamic one, and also knows nothing about its type.

Is there any solution to our problems?

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top