【发布时间】:2020-08-26 21:38:01
【问题描述】:
为什么这个ctypes包装函数的返回值是c_long(0)而不是c_long(3)?
// main.cpp
#include <iostream>
class AComplicatedCPPObj {
int *d_;
public:
explicit AComplicatedCPPObj(int *d)
: d_(d) {
}
int *getD() const {
return d_;
}
void setD(int *d) {
d_ = d;
}
};
#ifdef CTYPESTEST_EXPORTS
#if defined(_WIN64)
#define CTYPESTEST_API __declspec(dllexport)
#else
#define CTYPESTEST_API __declspec(dllimport)
#endif
#endif
extern "C" {
CTYPESTEST_API AComplicatedCPPObj *AComplicatedCPPObj_new(int *d) {
return new AComplicatedCPPObj(d);
}
CTYPESTEST_API int *AComplicatedCPPObj_getD(AComplicatedCPPObj *aComplicatedCppObj) {
return aComplicatedCppObj->getD();
}
CTYPESTEST_API void AComplicatedCPPObj_setD(AComplicatedCPPObj *aComplicatedCppObj, int *d) {
aComplicatedCppObj->setD(d);
}
}
编译成共享库的cmake脚本
cmake_minimum_required(VERSION 3.15)
project(ctypesTest)
set(CMAKE_CXX_STANDARD 14)
add_library(ctypesTest SHARED main.cpp)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/INSTALL)
add_definitions("-DCTYPESTEST_EXPORTS=TRUE")
install(TARGETS ctypesTest)
在 python 端:
import ctypes as ct
import os, glob, sys
WORKING_DIRECTORY = os.path.dirname(__file__)
# find the shared library
if sys.platform == "linux":
so_path = os.path.join(WORKING_DIRECTORY, "INSTALL/lib/libctypesTest.so")
else:
so_path = os.path.join(WORKING_DIRECTORY, "INSTALL/bin/ctypesTest.dll")
if not os.path.isfile(so_path):
raise FileNotFoundError(so_path)
# load sharedd library into python
lib = ct.CDLL(so_path)
class AComplicatedCPPObj:
def __init__(self, d):
self.set_function_types("AComplicatedCPPObj_new", [ct.POINTER(ct.c_int)], ct.c_void_p)
# checking some ctyeps functions
print("d", d, ct.c_int(d), ct.pointer(ct.c_int(d)), ct.pointer(ct.c_int(d)).contents)
self.obj = lib.AComplicatedCPPObj_new(ct.pointer(ct.c_int(d)))
self.set_function_types("AComplicatedCPPObj_getD", [ct.c_void_p], ct.POINTER(ct.c_long))
self.set_function_types("AComplicatedCPPObj_setD", [ct.c_void_p, ct.POINTER(ct.c_long)], None)
def set_function_types(self, func_name, argtypes, restype):
f = lib.__getattr__(func_name)
f.argtypes = argtypes
f.restype = restype
def getD(self):
return lib.AComplicatedCPPObj_getD(self.obj).contents
def setD(self, d):
if isinstance(d, str):
d = d.encode()
return lib.AComplicatedCPPObj_setD(self.obj, d)
if __name__ == '__main__':
o = AComplicatedCPPObj(3)
print(o.getD())
这会返回:
d 3 c_long(3) <__main__.LP_c_long object at 0x000001F66F0E8248> c_long(3)
c_long(0)
当我希望看到时
d 3 c_long(3) <__main__.LP_c_long object at 0x000001F66F0E8248> c_long(3)
c_long(3)
编辑 - 一个错误
我不确定我现在做了什么不同但现在重新运行此代码时我得到的输出是这样的:
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A52C8> c_long(3)
c_long(193626992)
另外,根据 cmets 中的一个问题,当我运行时:
x = 3
o = AComplicatedCPPObj(x)
print(o.getD())
输出是
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A5248> c_long(3)
c_long(193627312)
现在还没有
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A52C8> c_long(3)
c_long(3)
?
【问题讨论】:
-
如果
x = 3、o = AComplicatedCPPObj(x)怎么办? -
嗨,克里斯蒂,再次感谢您的意见,我已经更新了问题
标签: python c++ shared-libraries ctypes language-interoperability