Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用python ctypes包装扁平的c++;返回联合指针的类方法_C++_Python_Class_Ctypes_Flatten - Fatal编程技术网

C++ 使用python ctypes包装扁平的c++;返回联合指针的类方法

C++ 使用python ctypes包装扁平的c++;返回联合指针的类方法,c++,python,class,ctypes,flatten,C++,Python,Class,Ctypes,Flatten,我用cType编写了一个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

我用cType编写了一个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的能力仅仅是基于编译器对我的吼叫,所以这可能是错误的?< /P> 无论如何,下面是我为包装它而编写的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函数来设置数据,有人知道CyType返回是否引用同一块内存并设置它工作相同吗?(我将对此进行测试以找出答案)

您能更完整地描述一下您正在尝试做什么吗?Python的东西(状态)与C++的东西(HW)不匹配。我实际上已经设置了ReSpype来修复错误。后来我修改了这个问题。感谢您指出这个错误,我完全错过了一个结构,忘了在python中定义它。如果您有任何参数或重新类型作为指针传递,您应该显式声明它们,否则当您的64位指针在64位操作系统上作为默认32位整数传递时,您可能会感到惊讶(在程序开始访问更高的内存之前,可以正常工作一段时间).restype只需要设置一次,不需要在
get\u cam\u parms
中设置。好的,我会确保更改它。但是,现在我不确定如何设置数据。我想我可以重写整个问题,因为我已经更改了总体目标,但主题是一样的?