python 调用 c++类

转载自:http://stackoverflow.com/questions/7161349/help-required-using-a-class-in-extension-dll-through-ctypes

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 usecdll.test.A_new. When used like this,cdll caches the loadedCDLL instance, which in turn caches function pointers.

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

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值