python调用dll实例_Python ctypes cdll.LoadLibrary,实例化一个对象,执行其方法

533e4d470001a00a02000200-100-100.jpg

慕的地2183247

始终(正确)为C中定义的函数指定argtypes和restype,否则(C89样式)它们将默认为int(通常为32位),生成!!! 未定义的行为!。在64位,地址(大于2 GiB)将被截断(这正是您正在经历的)。同一错误的另一个后果:[SO]:不同操作系统上的python ctypes问题(@CristiFati的回答)。此外,遇到问题时,不要忘记[Python 3.Docs]:ctypes - Python的外部函数库。下面是您的代码的改编版本。detector.cpp:#include #include #include #define C_TAG "From C"#define PRINT_MSG_2SP(ARG0, ARG1) printf("%s - [%s] (%d) - [%s]:  %s: 0x%0p\n", C_TAG, __FILE__, __LINE__, __FUNCTION__, ARG0, ARG1)using std::endl;std::ofstream outFile;class Detector {    public:        Detector();        void process(int *pIn, int *pOut, int n);    private:        int m_var;};Detector::Detector() : m_var(0) {    outFile.open("addr_debug.txt");    outFile << "m_var init address: " << &m_var << endl;    PRINT_MSG_2SP("&m_var", &m_var);}void Detector::process(int *pIn, int *pOut, int n) {    outFile << "m_var process address: " << &m_var << endl;    outFile.close();    PRINT_MSG_2SP("&m_var", &m_var);}#define SIM_EXPORT __declspec(dllexport)#if defined(__cplusplus)extern "C" {#endif    SIM_EXPORT Detector *DetectorNew() { return new Detector(); }    SIM_EXPORT void DetectorProcess(Detector *pDet, int *pIn, int *pOut, int n) {        pDet->process(pIn, pOut, n);    }    SIM_EXPORT void DetectorDelete(Detector *pDet) { delete pDet; }#if defined(__cplusplus)}#endifcode.py:import sysfrom ctypes import CDLL, POINTER, \    c_int, c_void_pimport numpy as npsim_dll = CDLL("./sim.dll")detector_new_func = sim_dll.DetectorNewdetector_new_func.restype = c_void_pdetector_process_func = sim_dll.DetectorProcessdetector_process_func.argtypes = [c_void_p, POINTER(c_int), POINTER(c_int), c_int]detector_delete_func = sim_dll.DetectorDeletedetector_delete_func.argtypes = [c_void_p]class Detector():    def __init__(self):        self.obj = detector_new_func()    def process(self, pin, pout, n):        detector_process_func(self.obj, pin, pout, n)    def __del__(self):        detector_delete_func(self.obj)def main():    detector = Detector()    n = 1024    a = np.arange(n, dtype=np.uint32)    b = np.zeros(n, dtype=np.int32)    aptr = a.ctypes.data_as(POINTER(c_int))    bptr = b.ctypes.data_as(POINTER(c_int))    detector.process(aptr, bptr, n)if __name__ == "__main__":    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))    main()备注:正如我在开头所说,问题是argtypes和restype没有被指定(例如对于DetectorNew:评论detector_new_func.restype = c_void_p,你会再次遇到问题)问题中的代码是缺少部分(#include s,import s,...),也有一些语法错误,因此它不编译,因此不遵循[SO]:如何创建Minimal,完整且可验证的示例(mcve)指南。请问时确保有mcve你分配的对象(new Detector()),也必须被释放(否则,它会产生内存泄漏),所以我添加了一个函数(DetectorDelete - 要做到这一点),这是从(Python)Detector的析构函数调用的其他(非关键)更改(标识符重命名,一点重构,打印到stdout,......)输出:(py35x64_tes1) e:\Work\Dev\StackOverflow\q052268294>"c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64(py35x64_test) e:\Work\Dev\StackOverflow\q052268294>dir /bcode.pydetector.cpp(py35x64_test) e:\Work\Dev\StackOverflow\q052268294>cl /nologo /DDLL /EHsc detector.cpp  /link /DLL /OUT:sim.dlldetector.cpp   Creating library sim.lib and object sim.exp(py35x64_test) e:\Work\Dev\StackOverflow\q052268294>dir /bcode.pydetector.cppdetector.objsim.dllsim.expsim.lib(py35x64_test) e:\Work\Dev\StackOverflow\q052268294>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" ./code.pyPython 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32From C - [detector.cpp] (28) - [Detector::Detector]:  &m_var: 0x0000020CE366E270From C - [detector.cpp] (34) - [Detector::process]:  &m_var: 0x0000020CE366E270

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值