[python-win32] Re: WIN32 API?

Chuck Esterbrook ChuckEsterbrook at yahoo.com
Fri Jun 6 13:34:46 EDT 2003


On Friday 06 June 2003 08:32 am, Thomas Heller wrote:
> > > > If I were to preprocess and parse the C headers, would anything
> > > > prevent me from generating Python source for ctypes to specify
> > > > the return types, arg types and structs?
> > >
> > > No, it should work.
>
> After thinking this over, I want to mention there are other
> informations which are not present in the header files.
>
> Windows api functions doen't really use a common convention for
> returning errors.  Some functions return 0 if they fail, some return
> -1 if they fail, some have very complicated rules to determine of the
> have failed or not, these rules may even differ on the various win32
> platforms. Some functions set additional error info which can be
> retrieved by GetLastError(), others do not, again sometimes depending
> on the platform. (All this with one exception: functions returning a
> HRESULT).
>
> The next thing is that several functions allocate resources which
> must be released when no longer used.
>
> Both of the above issues should probably be handled when ctypes wraps
> a certain aspect of the windows api, and they cannot be automated.

But then we'd be no worse off than a C programmer calling these 
functions. Wrapping all the funcs is a good first step, IMO.


> > > Niki spahiev mentioned Simon Burton's cdecl.py module in the
> > > ctypes-users mailing list. I did some experiments with is and the
> > > results are quite promising.
> > >
> > > I sent VC98's windows.h file through the cl preprocessor (which
> > > resulted in a 46000 line file after removing chunks of empty
> > > lines, and fed it to the cdecl module.
> > >
> > > I had to fix one bug with hex constants, and had to hack around
> > > several windows specific problems (__cdecl, __stdcall, and other
> > > MS qualifiers), and unnamed structures. cdecl does not yet parse
> > > the file completely, but it looks promising. Maybe the end result
> > > can be something like Python's h2py script.
>
> Currently I'm investigating David Beazley's PLY module for this
> purpose which is fantastic. A lex-yacc combination written in pure
> python!

Yeah I peeked at that too. But if your cdecl.py fixes already work, 
might you share them with the rest of us?


> > Great. Another thought I had was that we could put all this info in
> > a little database, perhaps through mxBeeBase, and only pull in the
> > stuff that gets used. I believe there are over 10,000 constants,
> > funcs and structs, but a given app would probably only use a
> > fraction of them.
>
> This is a good idea, it would prevent that really huge python wrapper
> files would have to be imported.
>
> Although I'm hesitating whether ctypes which is a small module,
> really grow much bigger...

If you're not comfortable growing ctypes (for example, having a 
ctypes.win32 module) then we could always make this a separate package.

If your cdecl.py code works, I'd like to take it for a spin.

-- 
Chuck
http://ChuckEsterbrook.com




More information about the Python-win32 mailing list