【问题标题】:declare variable in python ctypes and passing to dll function在 python ctypes 中声明变量并传递给 dll 函数
【发布时间】:2018-07-16 08:50:02
【问题描述】:

例如我有一个 C++ 变量声明和这样的函数(假设它是一个 dll 函数)

int img_width,img_height,步幅; 一些功能(&img_width,&img_height,&stride) { …… }

我们如何使用 python ctypes 做同样的事情?

我尝试了以下方式

img_width, img_height, 步幅 = c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int) dll.somefunction(img_width,img_height,步幅)

导致以下异常

ctypes.ArgumentError: 参数 1: : 不知道如何转换参数 1)

我也尝试过以下方式

dll.somefunction.restype = c.c_void_p dll.somefunction.argtypes = [c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int)] dll.somefunction(img_width,img_height,步幅)

我最终遇到以下异常

NameError:未定义全局名称“img_width”

【问题讨论】:

    标签: c++ python-2.7 ctypes


    【解决方案1】:

    您正在创建需要创建整数实例并通过引用传递的类型:

    from ctypes import *
    
    # Declare the argument types
    dll.somefunction.argtypes = POINTER(c_int), POINTER(c_int), POINTER(c_int)
    
    # Create instances of the types needed
    img_width, img_height, stride = c_int(), c_int(), c_int()
    
    # Pass by reference
    dll.somefunction(byref(img_width), byref(img_height), byref(stride))
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2021-09-28
      相关资源
      最近更新 更多