在Python中使用ctypes访问DLL返回的对象的内容 [英] Accessing contents of an object returned by DLL using ctypes in Python

查看:245
本文介绍了在Python中使用ctypes访问DLL返回的对象的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dll在使用python中的ctypes调用函数时返回一个对象。

The dll returns an object on calling a function using ctypes in python.

它返回以下内容-称其为ReturnO; print(ReturnO)给出以下内容:

It returns the following - say it is named as ReturnO; print(ReturnO) gives the following:

(63484, <DLLname.ClassName object at 0x09D35670>)

对象应返回参数;它们的名称是:Paramater_1,Parameter_2等。我的问题是,我如何访问Parameter_1,Parameter_2等中的值。

The object should return the parameters; their names are: Paramater_1, Parameter_2 and so on. My question is, how do i access the values in Parameter_1, Parameter_2 etc.

如果我按如下所示进行打印

if i do a print as follows

print(ClassName.Parameter_1)
print(ClassName.Parameter_2)

我得到以下内容

 Field type=c_float_Array_5, ofs=49483, size=20
 Field type=c_float_Array_5, ofs=49503, size=20

现在,如何获取该值在这个数组中。 dotValue(.value)不起作用。

Now, how do I get the value in this array. dotValue (.value) does not work.

感谢您的帮助。谢谢。

----------------添加/修改----------下面- ----------

----------------ADDED/MODIFIED----------BELOW------------

下面是代码;感谢您的帮助:

below is the code; appreciate your help:

num1=10.1234
int1=10
num11=1.1111
str1="abcd"

ret=GetOutput_Main(int1,num1,num11,str1)

class ClassName(ctypes.Structure):
  _pack_ = 1
  _fields_ = [("parameter_1", ctypes.c_float * 5),
              ("parameter_2", ctypes.c_float * 5)]

def GetOutput_Main (int2,num2,num22,str2):
    lib = ctypes.WinDLL("mydllname.dll")
    prototype = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ClassName))
    paramflags = (1, "int2",), (1, "num2",), (2, "num22",), (2, "str2",),
    Getoutput_Sub = prototype(("Getoutput", lib), paramflags))
    ret = Getoutput_Sub(int2,num2)
    print(ret) #gives the details of the object
    print(str2.parameter_1) #gives the details of array

的详细信息print(ret)给我:

the print(ret) gives me:

(63484, <mydllname.ClassName object at 0x09D35670>)

如果我打印(str2),则会得到以下信息:

if i do print(str2), I get the following:

<class 'mydllname.ClassName'>

然后print(str2.parameter_1)给我

and print(str2.parameter_1) gives me

Field type=c_float_Array_5, ofs=49483, size=20

谢谢,我正在寻找解压缩对象的方法。

i am looking for ways to unpack the object, thanks.

如果这样做,其中num22是大小

if I do, where num22 is the size

UnpackedST = struct.unpack(str2,num22)

我遇到以下错误

Struct() argument 1 must be a str or bytes object, not _ctypes.PyCStructType


推荐答案

鉴于您的描述,您似乎拥有一个类似于以下内容:

Given your description it looks like you have a C function similar to the following:

#include <inttypes.h>

#define API __declspec(dllexport)

struct ClassName
{
    float parameter_1[5];
    float parameter_2[5];
};

API int __stdcall Getoutput(int a, uint32_t b, uint32_t* pc, struct ClassName* pd)
{
    int i;
    *pc = a+b;
    for(i=0;i<5;++i)
    {
        pd->parameter_1[i] = i*.5f;
        pd->parameter_2[i] = i*.25f;
    }
    return a*b;
}

您的 paramflags 参数表示两个输入(类型1)和两个返回值(类型2)。只需传递两个必需的输入值,然后索引第二个返回值即可访问其成员。使用 list()将数组转换为Python列表:

Your paramflags argument indicates two inputs (type 1) and two return values (type 2). Simply pass the two required input values, then index the second return value to access its members. Use list() to convert the array to Python lists:

from ctypes import *

class ClassName(Structure):
  _fields_ = [('parameter_1',c_float * 5),
              ('parameter_2',c_float * 5)]

lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)

ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))

输出:

(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]

这篇关于在Python中使用ctypes访问DLL返回的对象的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆