Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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
当相同的代码在VC+中运行良好时,为什么Python中的OpenCyUSB设备会出现故障+;?_Python_C++ - Fatal编程技术网

当相同的代码在VC+中运行良好时,为什么Python中的OpenCyUSB设备会出现故障+;?

当相同的代码在VC+中运行良好时,为什么Python中的OpenCyUSB设备会出现故障+;?,python,c++,Python,C++,我围绕CyAPI.lib编写了一个python包装器,并使用VC++构建了包装器项目。(我怀疑这与我的问题有关。)我正在使用Cypress Fx3拖缆示例设备以及Cypress引导加载设备 CCyUSBDevice::Open(index)使Python崩溃(没有异常或回溯)。但是如果我在VisualStudio2008上使用同一台设备、同一台计算机上的同一个驱动程序运行CCyUSBDevice::Open(index)函数,它就可以正常工作 调用代码非常简单。请看下面的代码。 在Python中

我围绕CyAPI.lib编写了一个python包装器,并使用VC++构建了包装器项目。(我怀疑这与我的问题有关。)我正在使用Cypress Fx3拖缆示例设备以及Cypress引导加载设备

CCyUSBDevice::Open(index)使Python崩溃(没有异常或回溯)。但是如果我在VisualStudio2008上使用同一台设备、同一台计算机上的同一个驱动程序运行CCyUSBDevice::Open(index)函数,它就可以正常工作

调用代码非常简单。请看下面的代码。 在Python中:

>>>from wrapprj import *
>>>try:
...   openDevice()   ###exception happens here and quit python,no way to get following exception No. and string.
...except Exception as e:
...   print "exception is {0}:{1}".format(e.errno,e.strerror)
在换行文件中:

static PyObject *
do_openDevice(PyObject *self, PyObject *args)
{
    bool ret;
    ret = call_openCyUSBDevice();
    return Py_BuildValue("O", ret?Py_True: Py_False);
}

PyMethodDef methods[] = {
    {"openDevice", do_openCyUSBDevice, METH_VARARGS,
        "open CyUSB Device"},        
    {NULL, NULL},
};
在CPP文件中:

extern "C" bool
call_openCyUSBDevice(void)
{
    devHandler = new CCyUSBDevice(NULL,CyUSBDevice_GUID,False);//don't try to open the device here. Otherwise exception happens here.

    if (NULL == devHandler)
    {
        return FALSE;
    }

    return devHandler->Open(0); // exception happens. But if I add some printing before this line, it CAN OPEN correctly. STRANGE!
}
在Cyapi.h中,CCyUSBDevice::Open()的定义如下:

class CCyUSBDevice
{
public:

  CCyUSBDevice(HANDLE hnd = NULL, GUID guid = CYUSBDRV_GUID, BOOL bOpen = true);
  ~CCyUSBDevice(void);
  .
  .
  bool                  Open(UCHAR dev);
  .
  .
}
在VisualStudioC++ 2008中,我运行代码,这样做很好:

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        CCyUSBDevice *USBDevice;
    public: 
        Form1(void)
        {
            InitializeComponent();
            USBDevice = new CCyUSBDevice(NULL,CyUSBDevice_GUID,False);
            USBDevice->Open(0);
        }
    .
    .
    }
为什么VC++可以正确地打开设备,而我的python包装器却不能,因为它们都调用相同的底层代码