加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python中访问c指向结构的指针

发布时间:2020-12-20 13:41:16 所属栏目:Python 来源:网络整理
导读:是否可以将int转换为类类型? 我在C中有以下代码: #include "Python.h"#define PYTHON_FILENAME "modelparam"void getmodelparam(long pModelParam) ;typedef struct { int seconds; int nanoseconds;} someTime;int main (){someTime *pSome ;long a ;prin
是否可以将int转换为类类型?

我在C中有以下代码:

#include "Python.h"

#define PYTHON_FILENAME "modelparam"

void getmodelparam(long pModelParam) ;

typedef struct {
 int seconds;
 int nanoseconds;
} someTime;


int main ()
{
someTime *pSome ;
long a ;
printf ("Testing the python interfacesn") ;

pSome = (someTime *) calloc(1,sizeof(someTime)) ;

pSome->seconds = 10 ;
pSome->nanoseconds = 20 ;

a = (long) pSome ;
printf ("a is %d,pSome is %dn",a,pSome) ;

getmodelparam(a) ;  

printf ("After the python call values are : %d,%dn",pSome->seconds,pSome->nanoseconds) ; 

return 0 ;

}
void getmodelparam(long pModelParam)
{
    PyObject    *pName ;
PyObject    *pModule ; 
PyObject    *pDict ;
PyObject    *pFunc ;
int         iSize = 0 ;
char        pcFunctionName[] = "modifymodelparam" ;

double      dTemp1,dTemp2 ; 

/* Initialize the python interpreter */
Py_Initialize() ;

/* Get Python code/module */
pName = PyUnicode_FromString(PYTHON_FILENAME);
if (NULL != pName)
{
    /* Import the module equivalent to doing 'import calresidual' in python */
    pModule = PyImport_Import(pName);
    Py_DECREF(pName) ;
    if (NULL != pModule)
    {
        /* Get the function and check if its callable function */   
        pFunc = PyObject_GetAttrString(pModule,pcFunctionName);
        if (pFunc && PyCallable_Check(pFunc))
        {
            /* Build the input arguments */
            PyObject *pResult = PyObject_CallFunction(pFunc,"i",pModelParam) ;
        }
        else
        {
            printf ("Some error with the functionn") ;
        }
    }
    else
    {
        printf ("Couldnt load the module %sn",PYTHON_FILENAME) ;
    }
}
else
{
    printf ("Couldnt convert the name of the module to python namen") ;
}
/* Release the resources. */
Py_DECREF(pModule) ;
Py_DECREF(pFunc) ;
Py_DECREF(pName) ;

/*Release the interpreter */
Py_Finalize() ;
}

在Python代码中:

import ctypes

class someTime(ctypes.Structure):
     _fields_ = [("seconds",ctypes.c_uint),("nanoseconds",ctypes.c_uint)]  

def modifymodelparam(m):
# Not sure how to access internal elements using m ?? 
    # How to typecast m ?? 
    n = someTime(m)
print ('Seconds',n.seconds)

如何在Python中将从C传递到类类型的地址进行类型转换,以便我可以访问这些类参数或间接说访问结构参数?

解决方法

那么上面的代码有一些错误.

需要将指针地址作为unsigned long / int传递给python模块.所以

void getmodelparam(long pModelParam)

//has to become

void getmodelparam(unsigned long pModelParam)

//and 

PyObject *pResult = PyObject_CallFunction(pFunc,pModelParam) ;

// has to become 

PyObject *pResult = PyObject_CallFunction(pFunc,"k",pModelParam) ;

然后在python文件中:

def modifymodelparam(m):
    n = ctypes.cast(m,ctypes.POINTER(someTime))
    print (n.contents.seconds)
    print (n.contents.nanoseconds)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读