Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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
Python ctypes和void常量指针 我有一个UnT8的NUMPY数组,我想通过SWIG将它作为空C++指针传递给C++库。_Python_C++_Types_Ctypes_Swig - Fatal编程技术网

Python ctypes和void常量指针 我有一个UnT8的NUMPY数组,我想通过SWIG将它作为空C++指针传递给C++库。

Python ctypes和void常量指针 我有一个UnT8的NUMPY数组,我想通过SWIG将它作为空C++指针传递给C++库。,python,c++,types,ctypes,swig,Python,C++,Types,Ctypes,Swig,我使用以下python代码: 打印(arr.ctypes.data\u as(ctypes.c\u void\u p)) display.GetImageDisplay().SetImage(arr.ctypes.data_as(ctypes.c_void_p)、arr.shape[0]、arr.shape[1]、1,1) display.GetImageDisplay().Update() 要在库中调用ImageDisplay类的SetImage方法,请执行以下操作: 我得到以下错误: c_

我使用以下python代码:

打印(arr.ctypes.data\u as(ctypes.c\u void\u p))
display.GetImageDisplay().SetImage(arr.ctypes.data_as(ctypes.c_void_p)、arr.shape[0]、arr.shape[1]、1,1)
display.GetImageDisplay().Update()
要在库中调用ImageDisplay类的SetImage方法,请执行以下操作:

我得到以下错误:

c_void_p(193598576)
Traceback (most recent call last):
  File ".\mvIMPACT test.py", line 137, in <module>
    display.GetImageDisplay().SetImage(arr.ctypes.data_as(ctypes.c_void_p),arr.shape[0],arr.shape[1],1,1)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\mvIMPACT\acquire.py", line 7101, in SetImage
    def SetImage(self, *args): return lib_mvIMPACT_acquire.ImageDisplay_SetImage(self, *args)
TypeError: in method 'ImageDisplay_SetImage', argument 2 of type 'void const *'
c_void_p(193598576)
回溯(最近一次呼叫最后一次):
文件“\mvIMPACT test.py”,第137行,在
display.GetImageDisplay().SetImage(arr.ctypes.data_as(ctypes.c_void_p)、arr.shape[0]、arr.shape[1]、1,1)
SetImage中的文件“C:\Program Files(x86)\Python37-32\lib\site packages\mvIMPACT\acquire.py”,第7101行
def SetImage(self,*args):返回lib\u mvIMPACT\u acquire.ImageDisplay\u SetImage(self,*args)
TypeError:在“ImageDisplay\u SetImage”方法中,参数2的类型为“void const*”
我认为pData的类型是错误的。我检查了SWIG包装器,我很确定我调用了正确的函数,并且第一个类型匹配

SWIGINTERN PyObject *_wrap_ImageDisplay_SetImage__SWIG_0(PyObject *SWIGUNUSEDPARM(self), int nobjs, PyObject **swig_obj) {
  PyObject *resultobj = 0; mvIMPACT::acquire::display::ImageDisplay *arg1 = (mvIMPACT::acquire::display::ImageDisplay *) 0 ;
  void *arg2 = (void *) 0 ; int arg3 ; int arg4 ; int arg5 ; int arg6 ; void *argp1 = 0 ; int res1 = 0 ; int res2 ; int val3 ;
  int ecode3 = 0 ; int val4 ; int ecode4 = 0 ; int val5 ; int ecode5 = 0 ; int val6 ; int ecode6 = 0 ;
  if ((nobjs < 6) || (nobjs > 6)) SWIG_fail;
  res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_mvIMPACT__acquire__display__ImageDisplay, 0 |  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "ImageDisplay_SetImage" "', argument " "1"" of type '" "mvIMPACT::acquire::display::ImageDisplay *""'");  }
   arg1 = reinterpret_cast< mvIMPACT::acquire::display::ImageDisplay * >(argp1);
  res2 = SWIG_ConvertPtr(swig_obj[1],SWIG_as_voidptrptr(&arg2), 0, 0); if (!SWIG_IsOK(res2)) {
    SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "ImageDisplay_SetImage" "', argument " "2"" of type '" "void const *""'");  }
   ecode3 = SWIG_AsVal_int(swig_obj[2], &val3); if (!SWIG_IsOK(ecode3)) {
    SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "ImageDisplay_SetImage" "', argument " "3"" of type '" "int""'"); }
    arg3 = static_cast< int >(val3); ecode4 = SWIG_AsVal_int(swig_obj[3], &val4); if (!SWIG_IsOK(ecode4)) {
    SWIG_exception_fail(SWIG_ArgError(ecode4), "in method '" "ImageDisplay_SetImage" "', argument " "4"" of type '" "int""'"); }
    arg4 = static_cast< int >(val4); ecode5 = SWIG_AsVal_int(swig_obj[4], &val5); if (!SWIG_IsOK(ecode5)) {
    SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "ImageDisplay_SetImage" "', argument " "5"" of type '" "int""'"); }
    arg5 = static_cast< int >(val5); ecode6 = SWIG_AsVal_int(swig_obj[5], &val6); if (!SWIG_IsOK(ecode6)) {
    SWIG_exception_fail(SWIG_ArgError(ecode6), "in method '" "ImageDisplay_SetImage" "', argument " "6"" of type '" "int""'"); }
    arg6 = static_cast< int >(val6); { try { { SWIG_PYTHON_THREAD_BEGIN_ALLOW;
        (arg1)->SetImage((void const *)arg2,arg3,arg4,arg5,arg6); SWIG_PYTHON_THREAD_END_ALLOW; }  }     
    catch(const mvIMPACT::acquire::ImpactAcquireException &e) { handleImpactAcquireException(e); SWIG_fail; }  
    catch(const std::exception &e) { SWIG_SetErrorMsg(PyExc_RuntimeError, e.what()); SWIG_fail; }  catch(...) {
      SWIG_SetErrorMsg(PyExc_RuntimeError, "unknown C++ exception"); SWIG_fail; }  }  resultobj = SWIG_Py_Void();
  return resultobj; fail: return NULL; }
SWIGINTERN PyObject*\u wrap\u imagesdisplay\u SetImage\u SWIG\u 0(PyObject*SwigunuUsedparm(self)、int-nobjs、PyObject**SWIG\u obj){
PyObject*resultobj=0;mvIMPACT::acquire::display::ImageDisplay*arg1=(mvIMPACT::acquire::display::ImageDisplay*)0;
void*arg2=(void*)0;int arg3;int arg4;int arg5;int arg6;void*argp1=0;int res1=0;int res2;int val3;
int-ecode3=0;int-val4;int-ecode4=0;int-val5;int-ecode5=0;int-val6;int-ecode6=0;
如果((nobjs<6)|(nobjs>6))开关失败;
res1=开关转换器PTR(开关对象[0],&argp1,开关类型影响采集显示图像显示,0);
如果(!SWIG_IsOK(res1)){
SWIG\u异常\u失败(SWIG\u ArgError(res1),“在方法”“ImageDisplay\u SetImage”“中,参数”“1”“类型”“mvIMPACT::acquire::display::ImageDisplay*”“”;)
arg1=重新解释强制转换(argp1);
res2=SWIG_ConvertPtr(SWIG_obj[1],SWIG_as_voidptrptr(&arg2),0,0);如果(!SWIG_IsOK(res2)){
SWIG\u异常\u失败(SWIG\u ArgError(res2),“在方法”“ImageDisplay\u SetImage”“中,参数”“2”“类型”“void const*”“”;)
ecode3=SWIG_AsVal_int(SWIG_obj[2],&val3);如果(!SWIG_IsOK(ecode3)){
SWIG\u异常\u失败(SWIG\u ArgError(ecode3),“在方法”“ImageDisplay\u SetImage”“中,参数”“3”“类型”“int”“”;)
arg3=static_cast(val3);ecode4=SWIG_AsVal_int(SWIG_obj[3],&val4);if(!SWIG_IsOK(ecode4)){
SWIG\u异常\u失败(SWIG\u ArgError(ecode4),“在方法”“ImageDisplay\u SetImage”“中,参数”“4”“类型”“int”“”;)
arg4=static_cast(val4);ecode5=SWIG_AsVal_int(SWIG_obj[4],&val5);if(!SWIG_IsOK(ecode5)){
SWIG\u异常\u失败(SWIG\u ArgError(ecode5),“在方法”“ImageDisplay\u SetImage”“中,参数”“5”“类型”“int”“”;)
arg5=static_cast(val5);ecode6=SWIG_AsVal_int(SWIG_obj[5],&val6);if(!SWIG_IsOK(ecode6)){
SWIG\u异常\u失败(SWIG\u ArgError(ecode6),“在方法”“ImageDisplay\u SetImage”“中,参数”“6”“类型”“int”“”;)
arg6=static_cast(val6){try{{SWIG\u PYTHON\u THREAD\u BEGIN\u ALLOW;
(arg1)->SetImage((void const*)arg2、arg3、arg4、arg5、arg6);SWIG\u PYTHON\u THREAD\u END\u ALLOW;}
catch(const mvIMPACT::acquire::ImpactAcquireException&e){handleImpactAcquireException(e);SWIG_fail;}
catch(const std::exception&e){SWIG_SetErrorMsg(PyExc_RuntimeError,e.what());SWIG_fail;}catch(…){
SigiguStReReRoMsg(PyExcExrimeTimeError,“未知C++异常”);SigiguFalth.}}ReultObj= SigigPyPyVoID();
返回resultobj;失败:返回NULL;}
  • 我是否因为指针不是常量而遇到问题
  • 我可以用ctypes创建一个常量指针吗
  • 我需要用另一个库复制我的numpy数组吗? 我知道C++代码在调用该调用后不使用数组。

  • 我举了一个小例子,演示了为什么不能将
    ctypes.c\u void\u p
    与SWIG包装的函数一起使用,并提供了两种替代解决方案

    给出了一个C库的例子——对于C++,你需要找到第一部分的被损坏的名字。 测试h

    #pragma once
    
    void SetImage(const void* pData,
                  int width,
                  int height,
                  int hitsPerPixel,
                  int pitch);
    
    测试c

    #include "test.h"
    
    #include <stdio.h>
    
    void SetImage(const void* pData,
                  int width,
                  int height,
                  int hitsPerPixel,
                  int pitch) {
      double* _pData = (double*)pData;
      printf("%f\n",_pData[0]);
    }
    
    在Python中,可以执行以下操作

    import ctypes
    import example
    import numpy as np
    
    arr = np.ones(10,dtype=np.float64)
    
    arg = arr.ctypes.data_as(ctypes.c_void_p)
    
    # Using the C functions
    libexample = ctypes.cdll.LoadLibrary("_example.so")
    
    libexample.SetImage(arg, 0,0,0,0)
    
    n = 20
    a = example.doubleArray(n)
    a[0] = 37.0
    
    # Using the SWIG wrapping
    example.SetImage(a,0,0,0,0)
    

    我建议您考虑不要使用空洞指针。SWIG是一个接口生成器。NumPy的开发人员使处理阵列输入和输出变得非常容易,请参见@jensunk有趣的链接,谢谢。但是,如果我不使用void指针,那么我需要修改SDK代码吗?SWIG层也作为SDK的一部分提供。我不熟悉
    mvIMPACT
    ,但我猜
    SetImage
    已经在他们的SWIG层中包装和导出了。如果该函数是一个普通的公共函数,则可以找到该符号并使用ctypes调用它。可以调用任何导出的函数,请参见许多如何使用的示例ctypes@Ok,我的问题是找不到一种方法来获取带有ctypes的const void*。它认为这个问题属于mvIMPACT论坛。一个小示例将显示swig对函数进行了包装,因此您无法使用ctypes.c_voidp。如果您使用cType .CDL.LoadLibrary('xxx,SO)),您可以像调用函数那样调用函数,但是如果C++,您需要知道SimTimeg的被损坏名称。
    swig3.0 -python -includeall -Wall test.i
    gcc -shared -o _example.so test.c test_wrap.c -Wall -Wextra -fPIC -I/usr/include/python2.7/ -DSWIGRUNTIME=extern
    
    import ctypes
    import example
    import numpy as np
    
    arr = np.ones(10,dtype=np.float64)
    
    arg = arr.ctypes.data_as(ctypes.c_void_p)
    
    # Using the C functions
    libexample = ctypes.cdll.LoadLibrary("_example.so")
    
    libexample.SetImage(arg, 0,0,0,0)
    
    n = 20
    a = example.doubleArray(n)
    a[0] = 37.0
    
    # Using the SWIG wrapping
    example.SetImage(a,0,0,0,0)