2

I have written the following code in visual studio to create an extension dll.

class A
{
     public:
      void someFunc()
      {

      }
};


  extern "C" __declspec(dllexport) A* A_new() 
  { 
     return new A(); 
  }

 extern "C" __declspec(dllexport) void A_someFunc(A* obj) 
  { 
    obj->someFunc(); 
  }

  extern "C" __declspec(dllexport) void A_destruct(A* obj) 
  { 
    delete obj; 
  }

I want to use ctypes to use class A in python. I have written the following code in wrapper.py --

from ctypes import windll

libA = windll.LoadLibrary("c:\ctypestest\test.dll")

class A: def init(self): self.obj = libA.A_new()

def __enter__(self):
    return self

def __exit__(self):
   libA.A_destruct(self.obj)

def some_func(self):
   libA.A_someFunc(self.obj)

At python 2.7.1 command prompt I do the following -

import wrapper as w ----> works fine

a = w.A()            ----> works fine  
a.some_func()        ----> Error  

libA.A_someFunc(self.obj)

ValueError:Procedure probably called with too many arguments.(4 bytes in excess)

Please help.

thanks in advance,

Abhaya
  • 197
  • 3
  • 9
  • possible duplicate of [How to use C++ classes with ctypes?](http://stackoverflow.com/questions/1615813/how-to-use-c-classes-with-ctypes) –  Aug 23 '11 at 13:05
  • You forget about `extern "C"` all function you use by ctypes. IMHO delete c++ object inside `__del__` method. – Arpegius Aug 23 '11 at 18:43

2 Answers2

5

Your exports use the cdecl calling convention, not stdcall, so you need to use CDLL instead of WinDLL.

test.cpp:

#include <iostream>
#include <string>
using namespace std;

class A {
    string name;
    public:        
        A(const string& name) {
            this->name = name;
            cout << name << ": signing on" << endl;
        }
        ~A() {
            cout << name << ": signing off" << endl;
        }
        void someFunc() { 
            cout << name << ": calling someFunc" << endl;
        }
};

extern "C" {
__declspec(dllexport) A *A_new(const char *name) { 
    return new A(string(name)); 
}
__declspec(dllexport) void A_someFunc(A *obj) { 
    obj->someFunc(); 
}
__declspec(dllexport) void A_destruct(A *obj) { 
    delete obj; 
}
}

test.py:

import ctypes

lib = ctypes.CDLL('test.dll')

def opaque_ptr(name):
    cls = type(name, (ctypes.Structure,), {})
    return ctypes.POINTER(cls)

class A(object):
    _A = opaque_ptr('CPP_A')
    lib.A_new.restype = _A
    lib.A_new.argtypes = ctypes.c_char_p,
    lib.A_destruct.argtypes = _A,
    lib.A_someFunc.argtypes = _A,

    def __init__(self, name, func=lib.A_new):
        self._obj = func(name.encode('ascii'))

    def __del__(self):
        self.destruct()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.destruct()

    def destruct(self, func=lib.A_destruct): 
        if self._obj:
            func(self._obj)
        self._obj = None

    def some_func(self, func=lib.A_someFunc):
        if not self._obj:
            raise RuntimeError
        func(self._obj)

with A('test') as a:
    a.some_func()

Output:

test: signing on
test: calling someFunc
test: signing off

FYI, WinDLL is a subclass of CDLL. The only change is that it sets _FUNCFLAG_STDCALL in the flags of the function pointers that it creates instead of _FUNCFLAG_CDECL.

cdll and windll are LibraryLoader instances. These are more useful in Windows, which automatically supplies the .dll extension. For example, you can use cdll.test.A_new. When used like this, cdll caches the loaded CDLL instance, which in turn caches function pointers.

Due to the above caching, avoid using the global loader instances when creating a library. Your argtypes, restype, and errcheck definitions on function pointers may conflict with other libraries. Instead use CDLL or a private loader such as cdll = LibraryLoader(CDLL).

Also, cdll.LoadLibrary returns an non-cached instance of CDLL. There's no reason to ever call it in place of using CDLL directly.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
0

it should be the same as any object creation in python:

some_var = A()
ghostJago
  • 3,381
  • 5
  • 36
  • 51
  • Implementing `__enter__` and `__exit__` may mean that he want to use the `with var=exr:` statement. – Arpegius Aug 23 '11 at 18:50
  • Thanks for your help. I realised that I didn't paste the correct copy of code here. class A { public: void someFunc() { } }; //extern "C" //Tells the compile to use C-linkage for the next scope. //{ extern "C" __declspec(dllexport) A* A_new() { return new A(); } extern "C" __declspec(dllexport) void A_someFunc(void* obj) { //obj->someFunc(); } extern "C" __declspec(dllexport) void A_destruct(A* obj) { delete obj; } – Abhaya Aug 24 '11 at 07:43
  • Thanks to everyone who helped. Realised that I had not put the correct copy of the code. I request you to reread the code and if possible help. – Abhaya Aug 24 '11 at 07:57