Roadmap wanted: Python access to SPIRIT.OCX (Win9x)

Alex Martelli aleaxit at yahoo.com
Wed Jan 31 03:24:22 EST 2001


"Martin Bless" <m.bless at gmx.de> wrote in message
news:3a7b39da.39409649 at news.muenster.de...
> (1) Learning Python ist fun.
> (2) Playing with the LEGO Mindstorms system building robots is fun.
>
> Using Python to control LEGO Mindstorms would be triple fun.
>
> Everything in LEGO is controlled by a fully registered SPIRIT.OCX
> (Win9x)
>
> (3) Is somebody already using Python to control SPIRIT.OCX?

Not me (another fascinating timesink being the LAST thing I
need:-) but I do understand why one would!-)


> (4) And can somebody give me a short roadmap how to proceed?

I guess you start by navigating to http://www.legomindstorms.com/sdk/SDK.asp
(accepting a page of legalese on the way) and downloading the
467 KB zipped technical docs (other pointers are at
http://www.holdren.com/scott/legos/ etc etc).

Then, you somehow get the spirit.ocx control on your machine
(sorry, I have no URLs for that), register it (regsvr32 or
whatever else you prefer to use for Active/X registration),
then you run makepy on it -- oh yes, for COM work with Python
you DO need Hammond's win32all extensions, either as a
separate package downloadable from the ActiveState site
for addition to pythonlabs' standard Python distribution
for Windows, or as a part of the ActivePython distribution
which you can download directly from ActiveState.

Easiest way to run makepy is from the PythonWin IDE, under
the Tools menu; you do have to know the "human-readable
name" of the type library that gets registered along with
the Active/X control.  Or you can run makepy.py directly
(e.g. from a DOS box) giving it the path to your ocx file
(which must be registered anyway, I think), I believe.

Now begins the interesting part.  If spirit.ocx is well
designed, it lets you instantiate it as a simple COM
server, without full-fledged Active/X hosting/embedding
overhead -- as it should have no indispensable GUI part,
it SHOULD let you do this (if, again, it IS well designed).

If this is the case, then just calling win32com.client.Dispatch
with, as argument, the ProgId of COM objects supplied by
spirit.ocx will let you instantiate such objects and
start calling methods and reading/writing properties on
them.  If you need to respond to COM events too, then
consider win32com.client.DispatchWithEvents -- to that
one, you must also pass a class object of your own which
defines the event-response methods you want to be called
when events are fired (there are alternatives, but this
one is pragmatically -- if not conceptually -- simplest
when applicable; the alternatives are mainly for getting
events from objects you don't directly instantiate, but
rather must get from get-active-object, or from methods,
properties, collections, etc, of OTHER objects yet).

If spirit.ocx insists on being hosted by a full-fledged
Active/X host, then your best bet is to use Internet
Explorer for the purpose -- see the OBJECT tag.  You
can fully control IE from Python in many different ways
("HTML Applications", aka HTA, maybe the most direct),
and IE can host just about any Active/X control you
may have around (HTA's have no special security issues).

There are other ways to 'fully host Active/X controls',
but none is as simple as exploiting work already done
on your behalf (by the designers of IE, VB forms,
MFC frames, ATL composite controls with a few twists
as applied and explained in a great MSJ article, Delphi
forms, etc, etc) -- of these, IE is what just about
any Windows box will have around & requires no special
non-python programming for you to control it (well, a
BIT of HTML, yes, if you call THAT programming:-).


One way or another, once you do have instantiated the
object[s] from spirit.ocx, then it's just a matter of
calling their methods etc -- you can probably start
by transposing to Python the VB example Lego gives.
It's probably a good way to learn the practical issues
of Python COM programming: case sensitivity, care
about [out] and [in,out] arguments, etc.  Nothing TOO
terrible though!-)


Alex






More information about the Python-list mailing list