Andrew MacIntyre | 2 Nov 00:41 2004
Picon

RE: ctypes

You're creating a pointer to the string "OSVERSIONINFO".
 
Instead pass the structure's address with byref():
 
  f.GetVersionExA(byref(x))
 
-----------------------------------------------------------------------
Andrew MacIntyre                  \ E-mail: andrew.macintyre <at> aba.gov.au
Planning & Licensing Branch        \ Tel:   +61 2 6256 2812
Australian Broadcasting Authority   \ Fax:  +61 2 6253 3277
-> "These thoughts are mine alone!" <----------------------------------
 
-----Original Message-----
From: Goran [mailto:gblankendal <at> curlink.com]
Sent: Monday, 1 November 2004 7:20 AM
To: ctypes-users <at> lists.sourceforge.net
Subject: [ctypes-users] ctypes

Hi,
 
I'm trying to get the os version info, but I get an error.
What is wrong with my script?
 
 
from ctypes import *
import ctypes
 
#According to the msdn we need to define an OSVERSIONINFO Structure:
 
class OSVERSIONINFO(Structure):
    _fields_ = [("dwOSVersionInfoSize",c_long),
                ("dwMajorVersion", c_long),
                ("dwMinorVersion", c_long),
                ("dwBuildNumber", c_long),
                ("dwPlatformId", c_long),
                ("szCSDVersion", c_char_p*128)]
 
lpOSVERSIONINFO = POINTER("OSVERSIONINFO")
 
x=OSVERSIONINFO()
x.dwOSVersionInfoSize=ctypes.sizeof(OSVERSIONINFO)

f=windll.kernel32
f.GetVersionExA(lpOSVERSIONINFO)
 
print "Version number: " + str(x.dwMajorVersion) + "." + str(x.dwMinorVersion)
print "Build number: " + str(x.dwBuildNumber)
print "Platform Id: " + str(x.dwPlatformId)
for pt in x.szCSDVersion:
    print pt,
 
 
Traceback (most recent call last):
  File "E:\PYTHON24\LIB\SITE-PACKAGES\PYTHONWIN\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Python24\Projects\OsVersionInfo.py", line 17, in ?
    f.GetVersionExA(lpOSVERSIONINFO)
ArgumentError: argument 1: exceptions.TypeError: Don't know how to convert parameter 1
 
 

Matt Campbell | 2 Nov 20:15 2004
Picon

Why bother with ctypes.com?

Hello,

I was wondering why it's worth the time and effort to develop the 
ctypes.com package, considering that win32com is already mature.  
ctypes.com looks rough around the edges right now; what are its advanges 
over win32com?  I figure this will be a frequently asked question as 
ctypes becomes more well-known.

Thanks,
Matt

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Thomas Heller | 2 Nov 20:38 2004
Picon

Re: ctypes 0.9.2 on cygwin

Doug Henderson <djhender <at> telusplanet.net> writes:

> Has any else tried to install ctypes 0.9.2 in cygwin?
>
> Indications are that previous versions worked on cygwin.

Once I had it running under cygwin, but then I decided that I wouldn't
care anymore about it.  You need the x-platform version (the tar.gz file) to
build it, but that doesn't support the stdcall calling convention, which
makes it useless for calling windows functions.

> I am running a fully up-to-date cygwin on Win XP (fully patched but
> without SP2).
>
> Cygwin currently installs Python 2.3.4
>
> The .zip archive will not build on cygwin, as it expects the windows
> extensions (and probably msvc).
>
> The .tar.gz archive builds and installs, but 59 of 175 unit tests fail.
>
> I unpacked the .tar.gz; and did
> 	python setup.py build
> 	python setup.py install
> 	cd unittest; python runtests.py
>
> Some tests failed:
>
> $ python runtests.py
> Warning: could not import test_unicode.py: dlsym, Win32 error 127
> .................................EEEEEEEEEEEEEEEEEEEEEEEEEEE.....EEE.E
> EEE.EEEEEE....................................EE...EEEEEEEEE.E....E.EE
> .E..E........................E....E
> ======================================================================
> ERROR: test_integrate (test_callbacks.SampleCallbacksTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>    File
>    "/cygdrive/d/Download/Python/ctypes-0.9.2/unittests/test_callbacks.py",
>    line 123, in test_integrate
>      integrate = dll.integrate
>    File "/usr/lib/python2.3/site-packages/ctypes/__init__.py", line
>    296, in __getattr__
>      func = self._CdeclFuncPtr(name, self)
> AttributeError: dlsym, Win32 error 127

Maybe because the test dll (which is the _ctypes_test extension
module) doesn't export any functions.  See the definition of EXPORT at
the top of the _ctypes_test.c file.

Can you use the MSVC compiled binary under cygwin?

Thomas

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Dave Brueck | 2 Nov 21:15 2004

Re: Why bother with ctypes.com?

Matt Campbell wrote:
> I was wondering why it's worth the time and effort to develop the 
> ctypes.com package, considering that win32com is already mature.  
> ctypes.com looks rough around the edges right now; what are its advanges 
> over win32com?  I figure this will be a frequently asked question as 
> ctypes becomes more well-known.

Here's my two cents: they should merge - most or all of pywin32 should be built 
on top of ctypes, such that pywin32 becomes a Pythonic interface for Windows 
APIs, while ctypes remains a low-level, cross-platform means of accessing libraries.

Given the above, win32com would be built on top of ctypes and ctypes.com and 
win32com would become one.

Overall I'd say the advantage of ctypes.com over win32com is the same as the 
advantage of ctypes over pywin32 - a lot more flexibility and a much broader 
scope. The disadvantage is the same as well - lower-level APIs and often more 
manual work.

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Thomas Heller | 2 Nov 21:34 2004
Picon

Re: Why bother with ctypes.com?

Matt Campbell <mattcampbell <at> pobox.com> writes:

> Hello,
>
> I was wondering why it's worth the time and effort to develop the
> ctypes.com package, considering that win32com is already mature.
> ctypes.com looks rough around the edges right now; what are its
> advanges over win32com?  I figure this will be a frequently asked
> question as ctypes becomes more well-known.

pywin32 doesn't allow to call custom (non-dispatch) COM interfaces
without writing an extension.

That's the main reason, but there are other design (maybe minor)
decisions in pywin32 that I don't like too much.

Thomas

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Doug Henderson | 3 Nov 06:59 2004
Picon

Re: ctypes 0.9.2 on cygwin

Thomas Heller wrote:
> Doug Henderson <___> writes:
> 
> 
>>Has any else tried to install ctypes 0.9.2 in cygwin?
>>
>>Indications are that previous versions worked on cygwin.
> 
> 
> Once I had it running under cygwin, but then I decided that I wouldn't
> care anymore about it.  You need the x-platform version (the tar.gz file) to
> build it, but that doesn't support the stdcall calling convention, which
> makes it useless for calling windows functions.
> 
> 
>>I am running a fully up-to-date cygwin on Win XP (fully patched but
>>without SP2).
>>
>>Cygwin currently installs Python 2.3.4
>>

<snip>

> 
> 
> Maybe because the test dll (which is the _ctypes_test extension
> module) doesn't export any functions.  See the definition of EXPORT at
> the top of the _ctypes_test.c file.
> 
> Can you use the MSVC compiled binary under cygwin?
> 
> Thomas
> 

<snip>

I ran a test using a simple dll compiled with gcc. I can call that dll
from both a C++ main program and a python script using ctypes.

This suggests that the basic functionality: calling a dll created
with gcc on cygwin from the cygwin version of python works, at least for 
simple cases.

I am not sure about calling an MSVC binary. I believe that python
extensions must be compiled with the same compiler as the main
python executable, so the binary extensions for the windows version
of python are not usable with the cygwin version and vis-versa. So I 
don't think I can drop the windows _ctypes.pyd or _ctypes_test.pyd into 
the cygwin python environment in place of the _ctypes.dll and 
_ctypes_test.dll.

Did ctypes at one time allow calling into any windows DLL?

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Jimmy Retzlaff | 5 Nov 14:40 2004

ANN: EasyDialogs for Windows version 1.16.0

EasyDialogs for Windows is available at:

http://www.averdevelopment.com/python/

EasyDialogs for Windows is a ctypes based emulation of the EasyDialogs
module included in the Python distribution for Macintosh. It attempts to
be as compatible as possible. Code using the Macintosh EasyDialogs
module can often be run unchanged on Windows using this module. The
module has been tested on Python 2.3 running on Windows NT, 98, XP, and
2003. I would appreciate feedback at jimmy <at> retzlaff.com about experience
running this on other versions of Windows (I attempted to avoid APIs
that weren't available on Windows 95 or Windows NT 4.0).

EasyDialogs is written in pure Python using Thomas Heller's ctypes
module (http://starship.python.net/crew/theller/ctypes/) to call Windows
APIs directly. No Python GUI toolkit is used. This means that relatively
small distributions can be made with py2exe (or its equivalents). A
simple test of all the dialogs in EasyDialogs bundled up using py2exe
results in a distribution that is about 1.25MB. Using py2exe in concert
with NSIS as shown at
http://starship.python.net/crew/theller/moin.cgi/SingleFileExecutable
allows the same test to run as a single file executable that is just
under 500KB.

Prerequisites:
EasyDialogs for Windows requires Windows, Python 2.3, and ctypes 0.6.3.

License: MIT

Change history:

Version 1.16.0
 - Removed resource DLL, resources are now in a Python source file which
   simplifies distribution of apps with py2exe
 - Spelling corrections
 - File open/save dialogs did not display on Windows 98
 - AskString edit boxes were too short on Windows 98
 - Improved display of drop down lists on Windows 98 and NT
 - Made minor changes to bring inline with CVS version 1.16 for
Macintosh

Version 1.14.0
 - Initial public release

Jimmy
--

-- 
http://mail.python.org/mailman/listinfo/python-list

buzifuzee | 10 Nov 00:58 2004
Picon

BHOL_Skel.py stalls when open link in new window

Hi folks,

I have tried out the BHO_Skelleton which is really great, but it stalls when I click "Open link in new window".

After the stall has ended the debugging messages are:

[3052] DllGetClassObject (1403464, 2001410656, 1186024)
[3052] <method call> IClassFactory.AddRef -> 1
[3052] <method call> IClassFactory.AddRef -> 2
[3052] <method call> IClassFactory.AddRef -> 3
[3052] <method call> IClassFactory.QueryInterface(IClassFactory) -> 0x0
[3052] <method call> IClassFactory.Release -> 2
[3052] <method call> IClassFactory.Release -> 1
[3052] # unimplemented GetSite for interface IObjectWithSite
[3052] LockServer 1 2
[3052] <method call> IObjectWithSite.AddRef -> 1
[3052] <method call> IClassFactory.CreateInstance(IObjectWithSite) -> 0x0
[3052] LockServer 1 3
[3052] <method call> IObjectWithSite.AddRef -> 2
[3052] LockServer 0 2
[3052] <method call> IObjectWithSite.Release -> 1
[3052] <method call> IClassFactory.Release -> 0
[3052] <method call> IObjectWithSite.QueryInterface(IMarshal) -> 0x80004002
[3052] <method call> IObjectWithSite.QueryInterface({0000001B-0000-0000-C000-000000000046}) -> 0x80004002
[3052] LockServer 1 3
[3052] <method call> IObjectWithSite.AddRef -> 2
[3052] <method call> IObjectWithSite.QueryInterface(IUnknown) -> 0x0
[3052] LockServer 1 4
[3052] <method call> IObjectWithSite.AddRef -> 3
[3052] <method call> IObjectWithSite.QueryInterface(IStdMarshalInfo) -> 0x80004002
[3052] <method call> IObjectWithSite.QueryInterface(IExternalConnection) -> 0x80004002
[3052] <method call> IObjectWithSite.QueryInterface({4C1E39E1-E3E3-4296-AA86-EC938D896E92}) -> 0x80004002
[3052] LockServer 0 3
[3052] <method call> IObjectWithSite.Release -> 2
[3052] LockServer 1 4
[3052] <method call> IObjectWithSite.AddRef -> 3
[3052] <method call> IObjectWithSite.QueryInterface(IObjectWithSite) -> 0x0
[3052] LockServer 1 5
[3052] <method call> IObjectWithSite.AddRef -> 4
[3052] <method call> IObjectWithSite.QueryInterface(IObjectWithSite) -> 0x0
[3052] LockServer 0 4
[3052] <method call> IObjectWithSite.Release -> 3
-----------------------------
By the way, the arguments of GetSite are documented the other way round, or is it given in stack-order ?!:

   (STDMETHOD(HRESULT, "GetSite", POINTER(c_void_p), REFIID))]
should be:
   (STDMETHOD(HRESULT, "GetSite", REFIID, POINTER(c_void_p)))]

But that doesn't change the behavior. :(

Documentation Page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/oin_e2o_6tb9.asp

And GetSite mustn't return NOT_IMPLEMENTED the document says, and the pointer should be set NULL or something.

I'm new to this stuff, so maybe somebody has some ideas?!

Thanks for helping,
Buzi

Thomas Heller | 10 Nov 20:08 2004
Picon

Re: BHOL_Skel.py stalls when open link in new window

buzifuzee <buzifuzee <at> yahoo.de> writes:

> Hi folks,
>
> I have tried out the BHO_Skelleton which is really great, but it
> stalls when I click "Open link in new window".
>
> After the stall has ended the debugging messages are:
[...]

> By the way, the arguments of GetSite are documented the other way
> round, or is it given in stack-order ?!:
>
>    (STDMETHOD(HRESULT, "GetSite", POINTER(c_void_p), REFIID))]
> should be:
>    (STDMETHOD(HRESULT, "GetSite", REFIID, POINTER(c_void_p)))]

Correct.

>
> But that doesn't change the behavior. :(
>
> Documentation Page:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/oin_e2o_6tb9.asp
>
> And GetSite mustn't return NOT_IMPLEMENTED the document says, and the
> pointer should be set NULL or something.

I have implemented GetSite, but that doesn't change anything.

I played a little bit more, and it seems some of the stuff that is done
in SetSite() has problems, removing this code makes it work (although
now you don't receive any events).  For further experiments, I attach
the code below.

There's another problem now: If you open new windows, and close the
windows not in the reverse order, the windows close but the process does
not terminate after closing the last window.

Thomas

"""
1. Generate ie6 module with ctypes/com/tools/readtlb.py

 (I forget the details, but you can probably use ie6_gen.py that comes with
 ctypes in the samples directory)

2. Generate a GUID and cut-n-paste it as PythonBHO._reg_clsid_.  To
 generate a GUID using win32all (aka "Python for Windows Extensions"):

import pythoncom
pythoncom.CreateGUID()

3. python bho_skel.py /regserver

4. Get and run DebugView to see the output from print statements.

5. Run IE, watch output in DebugView.

6. Remember to write something useful ;-)

"""
# A couple of example debug output lines:
# [Iexplore] # Unimplemented (NavigateComplete2 ('(9)', '(16396)'))
# [Iexplore] url (16396)

import sys
import _winreg

from ctypes import *
from ctypes.com import IUnknown, PIUnknown, REFIID, GUID, STDMETHOD, HRESULT, \
     COMObject
from ctypes.com.automation import IDispatch, BSTR, VARIANT, \
                                  dispinterface, DISPMETHOD
from ctypes.com.register import Registrar
from ctypes.com.connectionpoints import dispinterface_EventReceiver, \
     GetConnectionPoint
from ctypes.com.hresult import S_OK, E_FAIL

import ie6  # module generated by ctypes/com/tools/readtlb.py

HKLM = _winreg.HKEY_LOCAL_MACHINE
BHO_KEY = ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\"
           "Browser Helper Objects\\")
class MyRegistrar(Registrar):
    def build_table(self):
        table = Registrar.build_table(self)
        table.extend([(HKLM, BHO_KEY+self._reg_clsid_, "", None)])
        return table

class IObjectWithSite(IUnknown):
    _iid_ = GUID("{FC4801A3-2BA9-11CF-A229-00AA003D7352}")
    _methods_ = IUnknown._methods_ + [
        (STDMETHOD(HRESULT, "SetSite", POINTER(IUnknown))),
        (STDMETHOD(HRESULT, "GetSite", REFIID, POINTER(POINTER(IUnknown))))]

class PythonBHO(COMObject):
    _reg_clsid_ = "{693D1AC0-2D77-11D8-9B9E-FB41F7E93A45}"
    _reg_progid_ = "PythonBHO"
    _com_interfaces_ = [IObjectWithSite]

    def _get_registrar(cls):
        return MyRegistrar(cls)
    _get_registrar = classmethod(_get_registrar)

    _site = None
    def IObjectWithSite_SetSite(self, this, pUnkSite):
        self._site = pUnkSite
        return S_OK

    def IObjectWithSite_GetSite(self, this, riid, ppvSite):
        if self._site is not None:
            return self._site.QueryInterface(riid, ppvSite)
        # I'm not sure the following is correct
        ppvSite[0] = POINTER(IUnknown)() # set to a NULL pointer
        # or should it be this?
##        ppvSite[0] = 0
        return S_OK

class DWebBrowserEvents2Impl(dispinterface_EventReceiver):
    _com_interfaces_ = [ie6.DWebBrowserEvents2]

    def OnQuit(self, this, *args):
        self.disconnect(self.handle)

    def BeforeNavigate2(self, this, pDisp, url, Flags, TargetFrameName,
                        PostData, Headers, Cancel):
        print "url", url

if __name__ == '__main__':
    from ctypes.com.server import UseCommandLine
    UseCommandLine(PythonBHO)

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
Thomas Heller | 10 Nov 20:12 2004
Picon

Re: Re: ctypes 0.9.2 on cygwin

Doug Henderson <djhender <at> telusplanet.net> writes:

> Thomas Heller wrote:
>> Doug Henderson <___> writes:
>>
>>>Has any else tried to install ctypes 0.9.2 in cygwin?
>>>
>>>Indications are that previous versions worked on cygwin.
>> Once I had it running under cygwin, but then I decided that I
>> wouldn't care anymore about it.  You need the x-platform version (the
>> tar.gz file) to build it, but that doesn't support the stdcall
>> calling convention, which makes it useless for calling windows
>> functions.
>>
>>>I am running a fully up-to-date cygwin on Win XP (fully patched but
>>>without SP2).
>>>
>>>Cygwin currently installs Python 2.3.4
>>>
>
> <snip>
>
>> Maybe because the test dll (which is the _ctypes_test extension
>> module) doesn't export any functions.  See the definition of EXPORT at
>> the top of the _ctypes_test.c file.
>> Can you use the MSVC compiled binary under cygwin?
>> Thomas
>>
>
> <snip>
>
> I ran a test using a simple dll compiled with gcc. I can call that dll
> from both a C++ main program and a python script using ctypes.
>
> This suggests that the basic functionality: calling a dll created
> with gcc on cygwin from the cygwin version of python works, at least
> for simple cases.
>
> I am not sure about calling an MSVC binary. I believe that python
> extensions must be compiled with the same compiler as the main
> python executable, so the binary extensions for the windows version
> of python are not usable with the cygwin version and vis-versa.

In principle that is correct - although it boils down to compatibility
of the C runtime dlls used.  Python 2.3 extensions must be linked to
msvcrt.dll, and Python 2.4 extensions must be linked to msvcr71.dll.

> So I don't think I can drop the windows _ctypes.pyd or
> _ctypes_test.pyd into the cygwin python environment in place of the
> _ctypes.dll and _ctypes_test.dll.

Not sure about that.  Do you have to recompile other extension for
cygwin, or can you use native windows binaries?

> Did ctypes at one time allow calling into any windows DLL?

The native version of course.  The cygwin version doesn't support the
stdcall calling convention, which is used by most windows system dlls.

Thomas

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click

Gmane