logo       

Re: accessing classes in a C++ DLL: msg#00003

python.ctypes

Subject: Re: accessing classes in a C++ DLL

Shi Sherebrin <shi@xxxxxxxxxxxxxxxxxx> writes:

> Is there any way to access a class that is exported by a DLL, or are
> we limited to using only extern C functions (as described by the
> article referred to by the FAQ on the ctypes website:
> http://sourceforge.net/mailarchive/forum.php?thread_id=1604647&forum_id=24606)
>
> any suggestions for how to do this?
>
> I'm thinking we'd have to rewrite the DLL to provide extern C access
> functions which then in turn create and use the classes - would the
> class objects have to stay *in* the DLL? in our case this should in
> fact be possible as the class provides very few functions and it
> should be possible to have the class created just for the purpose of
> calling those functions. But what would happen if the class took a
> long time to initialize (and was then efficient for subsequent
> function calls) and we didn't want to have every call to the function
> pay the penalty of that initialization?
>
> I wouldn't think it's possible to get classes out of an 'extern C'
> DLL, but not being too knowledgeable in this area I thought I'd ask.

You could create one or more functions creating instances of these
classes, and let them return pointers to the instances. Maybe you name
the pointers 'handles'.

Then, write an exported function for each method of each class, these
functions would receive the pointer to the instance as additional
argument, and call the instance method.

MS has used a similar approach with GDIPlus - which is a C++ class
library. There's a GdiPlusFlat.h header file in the platform sdk, it can
be used by C programs, or ctypes.


For accessing C++ classes directly, I don't know if that#s possible.
Are the binary layouts of C++ classes really documented? I thought that
was a problem that only COM was supposed to solve.

Thomas



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise