[C++-sig] Accessing Class Member Function from Python

Nat Linden nat at lindenlab.com
Wed Jun 6 15:27:46 CEST 2012


On Wed, Jun 6, 2012 at 6:57 AM, Nagaraju <srirangamnagaraju at gmail.com> wrote:

> Thank you very much for your reply. I am sorry if I did not explain
> something clearly.
>
> I am doing as below after implementing the MyClass in the same file:
>
> BOOST_PYTHON_MODULE(hello){
> ...
> }
>
> I am using CDLL from ctypes to load this Test.DLL. Say
> planet = CDLL("Test.DLL").
>
> Now I want to create an object of MyClass and call add function. How can I
> do this?

I think you're mixing two different tactics here.

You can use ctypes to load a plain DLL that publishes extern "C"
functions, and call those functions. But I don't believe it supports
the notion of a Python class defined in that DLL.

Or you can use Boost.Python to prepare a special DLL that Python will
recognize as an extension module. Such modules can be loaded with
'import'. In this case you don't use ctypes because the module
contents describe themselves for the Python runtime to know how to use
them directly, classes and methods and functions and data.
Boost.Python is a succinct way to provide such a description.

But it's my belief that a DLL containing the description compiled from
BOOST_PYTHON_MODULE(hello) must be named "hello.pyd" for the Python
interpreter to successfully import it.

Once you're able to import hello, you should be able to instantiate
hello.MyClass() and proceed from there.


More information about the Cplusplus-sig mailing list