从 python 将指针值传递给 C 函数

use*_*168 1 c python

我有一个 C 函数,它接受一个 int 并通过填充 b 中的值来返回

typedef   uint8_t State;
#define   STATE_POWERDOWN               ((State) 0x00) 
#define   STATE_SLEEP                   ((State) 0x10) 


int Val_GetStatus(int a , State *b)
Run Code Online (Sandbox Code Playgroud)

该函数与其他函数一起从 C DLL 中导出。

我从 python 中调用这个函数。虽然我能够连接 DLL,但我不明白如何在 python 中将变量传递给该函数?

def Acme_getDeviceStatus(self,DeviceStatus):
    # b 
    self.device.Val_GetStatus(0,b)  # how to pass and get value in b
    # DeviceStatus = b
    # return DeviceStatus somehow 
Run Code Online (Sandbox Code Playgroud)

wen*_*zul 6

例如,您可以使用ctypesswig在 Python 中调用 C 函数。

from ctypes import *

Val_GetStatus= CDLL('x').Val_GetStatus
Val_GetStatus.argtypes = [c_int,POINTER(c_unit8)]
Val_GetStatus.restype = c_int

deviceStatus = ctypes.c_uint8()

print Val_GetStatus(0, byref(deviceStatus))
print deviceStatus.value
Run Code Online (Sandbox Code Playgroud)

Swig 将为您生成某种界面,因此您不必手动执行此操作。