python - 在Python中使用ctypes访问DLL返回的对象的内容

标签 python object dll ctypes

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

它返回以下内容 - 假设它被命名为 ReturnO; print(ReturnO) 给出以下内容:

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

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

如果我按如下方式打印

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) 不起作用。

感谢您的帮助。谢谢。

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

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

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

打印(ret)给了我:

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

如果我执行 print(str2),我会得到以下结果:

<class 'mydllname.ClassName'>

和 print(str2.parameter_1) 给了我

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

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

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

UnpackedST = struct.unpack(str2,num22)

我收到以下错误

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

最佳答案

根据您的描述,您似乎有一个类似于以下内容的 C 函数:

测试.c

#include <inttypes.h>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

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() 或对数组进行切片来访问其元素。我提供了一个 __repr__ 函数,以便该类可以在打印时显示自身。

测试.py

import ctypes as ct

class ClassName(ct.Structure):

    _fields_ = (('parameter_1', ct.c_float * 5),
                ('parameter_2', ct.c_float * 5))

    def __repr__(self):
        '''Return string describing how to print ClassName object.
        '''
        # Note that slicing an array produces a list of its elements.
        return f'ClassName({self.parameter_1[:]}, {self.parameter_2[:]})'

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

pc, pd = Getoutput(10, 11)
print(f'{pc=} {pd=}')

输出:

pc=21 pd=ClassName([0.0, 0.5, 1.0, 1.5, 2.0], [0.0, 0.25, 0.5, 0.75, 1.0])

关于python - 在Python中使用ctypes访问DLL返回的对象的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53367453/

相关文章:

python - 如何将zerorpc作为greenlet运行?

存储 type_info 对象的 C++ 方法不起作用

c# - 如何在基于 Visual-C++ 的 .net DLL 中从 C# 接收字符串?

C# Windows Service 找不到 dll 的命名空间,可在其他项目中使用

java - 使用不同的键对列表进行排序

c++ dll windows 插件体系结构方法

python - 在 OSX 上哪里可以找到/下载适用于 python 的 lib rsvg

python - Scrapy尝试抓取网页的信息内部链接

python - 如何获取函数的返回值?

Javascript - 如何从对象数组中删除覆盖/包装对象