[C++-sig] Getting a c++ pointer from a python object

Simon Pickles sipickles at googlemail.com
Mon Jun 8 21:31:23 CEST 2009


Hi,

I am trying to get a c++ pointer to a python object.

I am using DirectPython, and a function in that library, getDevice(), 
"returns the address of the object" - a IDirect3DDevice9*, already 
created using DirectPython code.

Using boost::python, I have an extension module function with this
signature:

// c++
void TestClass::Init( IDirect3DDevice9* device );

I tried to call this function with:

#python, called after device is created:
from testClass import TestClass
test = TestClass()
test.Init( d3d.getDevice() )

Here's the error I get:
##################
Traceback (most recent call last):
   File "client.py", line 31, in __init__
     test.Init(d3d.getDevice())
Boost.Python.ArgumentError: Python argument types in
     TestClass.Init(TestClass, long)
did not match C++ signature:
     Init(class TestClass {lvalue}, struct IDirect3DDevice9 *)
##################

But got a type mismatch. DirectPython gives the address as a long, when 
c++ wants a pointer to a struct.

I then tried this with the same python calling code:

void TestClass::Init(void* deviceAddress)
{
     IDirect3DDevice9* m_device = 
static_cast<IDirect3DDevice9*>(deviceAddress);
}

Of course I get a similar error:

##################
Traceback (most recent call last):
   File "client.py", line 33, in __init__
     test.Init(d3d.getDevice())
Boost.Python.ArgumentError: Python argument types in
     TestClass.Init(TestClass, long)
did not match C++ signature:
     Init(class TestClass {lvalue}, void *)
##################

How can I get the address from python to c++?

Thanks

Simon


More information about the Cplusplus-sig mailing list