is win api available in native python

David Bolen db3l at fitlinxx.com
Tue Mar 25 11:29:37 EST 2003


"Phil" <ycal at grapevine.net> writes:

> wouldn't that be the windows api extension program I mentioned.  I
> didn't want such a huge, but I'm sure useful, extension.  Also I am
> trying to stick to the non os specific python.

Just to clarify, you're trying to stick to a non-OS specific routine
in order to implement an OS-specific request?  Sounds "ambitious". :-)

Ok, hanging up a dynamically dialed connection or shutting down the
system may conceptually be something that is common cross-platform,
but the devil is in the details and I don't believe there's any high
level common API to accomplish such tasks - with the possible 
each system may offer - in a generic way.

BTW, you don't mention your Windows platform, but the
InternetAutodialHangup API is a Windows CE specific API.  I haven't
worked on the CE platform, so I'm not certain what other limitations
(if any) may exist with Python there.  However, there are other RAS
APIs (like RasHangup) that are more cross-version and would let your
code operate on other Windows versions as well.

If you really want to avoid win32all, there are some other options,
but they're still going to be Windows-specific because of the nature
of what you are trying to do (e.g., given that the routines you want
to access are specific to Windows).

1. You could create your own extension for Python that strictly wrapped
   the win32 entry points you wanted.

   This would involve building your own custom extension and wrapping
   the functions in question.  Then you could just import your module
   and use those functions.  The resource overhead would be minimal
   (needing to include your pyd file) but you'd need a development
   environment to create the extension and you'd be maintaining
   non-Python code.

2. You could find external utilities (or write one) to call the APIs and
   then use them.

   I believe that even XP has an existing "rasdial" command that can
   kill an existing connection (execute "rasdial /d" on the system in
   question).  And you can get a "shutdown" utility from several
   places, including the Windows resource kit.

   You could then simply execute those external utilities when you
   needed the functionality (using for example, os.system() or
   os.popen()).

3. You could use an existing add-on module such as calldll or ctypes to
   explictly load the relevant Win32 DLL and call the routine.

   This is effectively a generic way to accomplish (1) without having
   to write any extension module code on your own.  calldll is an
   older module, with ctypes being more current, and more actively
   maintained.  They both provide generic ways for loading and
   accessing entry points in Win32 DLLs (and it looks like ctypes is
   actually now cross-platform).

   calldll: http://www.nightmare.com/software.html (Alex Martelli put
   a 2.2 binary online at http://www.aleax.it/Python/calldll_22.zip)

   ctypes: http://starship.python.net/crew/theller/ctypes.html

But to be honest, things work very smoothly with the win32all
extensions, and they're so prevalent for any systems that really
interact with the win32 environment at all with Python, that I
wouldn't hesitate to install them.  If you're worried about
redistribution size, realize that if you wrap up a particular
application you won't need the entire win32all package, but just a few
pyds and dlls.

In win32all, you'd accomplish the hangup with the win32ras.Hangup
method (you can use win32ras.EnumConnections to get a handle for the
active connection), and the shutdown with win32api.ExitWindows or
win32api.ExitWindowsEx.

> I was hoping python would have a way to call the loadlibrary function
> and whatever mac and unix equivalent would be.  More of a lowlevel os
> starting point to execute dll functions.  I am not familiar with unix or
> mac but it seems like an obvious need.  Not the whole api just a way to
> build your own.

That's more or less item (3) above, and it looks like recently ctypes
even became cross platform (not that the methods you want to call are
cross-platform), so I'd definitely suggest taking a look at it.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list