Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ interface #127

Open
coldfire0200 opened this issue Oct 31, 2023 · 1 comment
Open

C++ interface #127

coldfire0200 opened this issue Oct 31, 2023 · 1 comment

Comments

@coldfire0200
Copy link

The current implementation of ctypeslib does not support C++ interface (I understand this could be a very complicated issue). If I have a c++ interface like this:

class TestClassInterface
{
	public:
	virtual void test() = 0;
	virtual void test1() = 0;
};
TestClassInterface* createTestClass();

the output of clang2py is like this:

class class_TestClassInterface(Structure):
    pass

class_TestClassInterface._pack_ = 1 # source:False
class_TestClassInterface._fields_ = [
    ('PADDING_0', ctypes.c_ubyte * 8),
]

createTestClass = _libraries['FIXME_STUB'].createTestClass
createTestClass.restype = ctypes.POINTER(class_TestClassInterface)
createTestClass.argtypes = []

Which basically generates a dummy class.

I would expect something like this:

class TestClassInterface(ctypes.Structure):
    class VTable(ctypes.Structure):
        _pack_ = 1
        _fields_ = [
            ('test1', ctypes.CFUNCTYPE(None, ctypes.POINTER(None))),
            ('test2', ctypes.CFUNCTYPE(None, ctypes.POINTER(None))),
        ]
    _pack_= 1
    _fields_ = [
        ('vtable', ctypes.POINTER(VTable)),
    ]
python_dll.getTestClass.restype = ctypes.POINTER(TestClassInterface)

And I can call the function from python like:

test_wrapper = python_dll.getTestClass()
test_wrapper.contents.vtable.contents.test1(test_wrapper)
test_wrapper.contents.vtable.contents.test2(test_wrapper)

@mara004
Copy link

mara004 commented Feb 21, 2024

To my understanding, you can't use C++ with ctypes, because it does not result in a stable, FFI-bindable ABI (as opposed to C). You'll want something like pybind11 instead.

See also https://stackoverflow.com/a/1616143/15547292.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants