ctypes.com - unable to call function, read property

Thomas Heller theller at python.net
Wed Apr 14 11:04:13 EDT 2004


"Roman Yakovenko" <roman.yakovenko at actimize.com> writes:

> Hi. I am trying for the first time to use ctypes.com module. 
> I have question: I have reference to object that implements IDiaSession interface.
>
> IDiaSession._methods_ = IUnknown._methods_ + [
>     STDMETHOD(HRESULT, "_get_loadAddress", POINTER(c_ulonglong)),
>     STDMETHOD(HRESULT, "getEnumDebugStreams", POINTER(POINTER(IDiaEnumDebugStreams))),
>
> I can't find out how I call _get_loadAddress and getEnumDebugStreams methods.
> I know that in com "get_" is a prefix for property. What should I do ? 
> Also "_get_loadAddress" defined as property.
>

So far, ctypes.com does not try to expose a higher level interface.  So
properties are exposed as getter and setter methods, with _get_ and
_set_ prepended to the property name.

You call the methods exactly as you would do in C (or raw C++, maybe):


from ctypes import c_ulonglong, byref

load_address = c_ulonglong()
obj._get_loadAddress(byref(load_address))
# now access load_address.value to get the numerical value

Calling getEnumDebugStreams is only slightly more complicated:

enum_streams = POINTER(IDiaEnumDebugStreams)()
obj.getEnumDebugStreams(byref(enum_streams))

Now you can call methods on the enum_streams object in the same way:

retval = c_long()
enum_streams.count(byref(retval))
print retval.value

I hope you get the idea - most of the time you can literally convert C
code to Python code.

Thomas

PS: I would like to clarify two additional things:

1. POINTER(IDiaEnumDebugStreams) creates a *class*.

POINTER(IDiaEnumDebugStreams)() creates an instance of this class, which
is actually a NULL pointer to a IDiaEnum... interface.

This latter corresponds to this C code:

IDiaEnumDebugStreams *p;

2. COM refcounting is automatic for probably 95% of the cases, although
the rules are pretty complicated.  The largest sample program for client
dies com is probably the readtlb.py tool itself.

PPS: There is a ctypes mailing list, which is the best place to answer
questions like these ;-)  Also available via nntp through gmane.org.





More information about the Python-list mailing list