使用python ctypes包装扁平化的c ++类方法,返回联合指针
||
我正在使用ctypes为C ++ DLL编写python包装器。我已经“展平”了C ++类来处理基于C的函数,并且大多数都运行良好。虽然我不确定如何处理类库中的一些奇怪函数。
这是我要包装的代码的C ++版本:
typedef struct {
short Valid; // magic number
short SizeX,SizeY;
short Fps;
short Shutter1,Shutter2; //Preview 1 and 2 Shutter, in lines (=ShutterTime[sec]*Fps*SizeY)
short PreTrigPages;
short Options;
short BufStart;
short BufEnd;
short SeqLength;
short ShiftCount;
short DBS;
short FN;
short MAXBL;
} CAM_PARMS_V1;
union CAM_PARMS {
CAM_PARMS_V1 p;
BYTE bytes[128];
unsigned long quads[32];
};
//Function prototype
virtual CAM_PARMS *GetCamParms(void);
//Use of function
GetCamParms()->p.SizeX=640;
现在,我将“类”方法“压平”到C函数,如下所示:
typedef CVitCamDLL *CameraHandle;
#define EXPORTCALL __declspec(dllexport) __stdcall
CAM_HW EXPORTCALL GetCamParms(CameraHandle handle)
{
return *handle->GetCamParms();
}
我还没有把这种包装方法卖掉,但是编译器没有抱怨,所以我认为这是一个好兆头。我在一定程度上调试C ++和C的能力完全基于编译器对我大喊大叫,所以这可能是错误的吗?
无论如何,这是我自那时以来包装它的Python代码:
import ctypes as ct
from ctypes.util import find_library
dll = find_library(\'VitCamC\')
if dll == None:
raise Exception(\"VitCamC.dll not found\")
cam = ct.WinDLL(dll)
class CAM_PARMS_V1(ct.Structure):
_fields_ = [(\"Valid\", ct.c_short),
(\"SizeX\", ct.c_short),
(\"SizeY\", ct.c_short),
(\"Fps\", ct.c_short),
(\"Shutter1\", ct.c_short),
(\"Shutter2\", ct.c_short),
(\"PreTrigPages\", ct.c_short),
(\"Options\", ct.c_short),
(\"BufStart\", ct.c_short),
(\"BufEnd\", ct.c_short),
(\"SeqLength\", ct.c_short),
(\"ShiftCount\", ct.c_short),
(\"DBS\", ct.c_short),
(\"FN\", ct.c_short),
(\"MAXBL\", ct.c_short)]
class CAM_PARMS(ct.Union):
_fields_ = [(\"p\", CAM_PARMS_V1),
(\"bytes\", ct.c_ubyte * 128),
(\"quads\", ct.c_ulong * 32)]
def get_cam_parms(handle, mode):
cam.GetCamParms.restype = CAM_PARMS #This fixes it!
return cam.GetCamHw(handle, ct.byref(mode))
好的,因此我能够解决内存访问冲突问题,并且可以读取数据和所有奇妙的内容。但是,从C ++使用示例来看,他们使用GetCamParms函数来设置数据,有人知道ctypes返回是否引用相同的内存块,并且其设置是否相同? (我将对此进行测试以找出答案)
没有找到相关结果
已邀请:
0 个回复