惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
月光博客
月光博客
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
B
Blog
博客园_首页
G
Google Developers Blog
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI

博客园 - Ady Lee

【C语言】二维数组在内存中的存储方式 C++:继承访问属性(public/protected/private) Makefile文件基本格式 异步IRP的教训(已附DUMP) IRQL 文件过滤驱动中的重入处理 FastIO 用poolmon来查找内存泄露 缓存管理器 浅议Windows 2000/XP Pagefile组织管理 FCB CCB FileObject WDM驱动改可手动加卸载的NT驱动 Windows 文件过滤驱动经验总结 PKCS #1 RSA Encryption Version 1.5 解决STM32 I2C接口死锁在BUSY状态的方法讨论 亲测实验,stm32待机模式和停机模式唤醒程序的区别,以及唤醒后程序入口 python中使用 C 类型的数组以及ctypes 的用法 Windbg 内核态调试用户态程序然后下断点正确触发方法(亲自实现发现有效) ___security_cookie机制,防止栈溢出 security cookie 机制(2)--- 初始化___security_cookie
python ctypes库3_如何传递并返回一个数组
Ady Lee · 2019-01-21 · via 博客园 - Ady Lee

可以将数组指针传递给dll,但无法返回数组指针,python中没有对应的数组指针类型。

如果需要返回数组,需借助结构体。

参考ctypes官方文档:

https://docs.python.org/3.6/library/ctypes.html#structures-and-unions

返回一个结构体例程:

# 返回结构体
import ctypes
path = r'E:\01_Lab\VisualStudioLab\cpp_dll\cpp_dll\Debug\cpp_dll.dll'
dll = ctypes.WinDLL(path)

class StructPointer(ctypes.Structure):
_fields_ = [("name", ctypes.c_char * 20),
("age", ctypes.c_int),
("arr", ctypes.c_int * 3)]

dll.test.restype = ctypes.POINTER(StructPointer)
p = dll.test()

print(p.contents.name)
print(p.contents.age)
print(p.contents.arr[0])
print(p.contents.arr[1])
print(p.contents.arr[2])

c++中

1、定义结构体

typedef struct StructPointerTest
{
char name[20];
int age;
int arr[3];
}StructPointerTest, *StructPointer;
2、定义函数

DLLEXPORT StructPointer __stdcall test() // 返回结构体指针
{
StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest));
strcpy(p->name, "Joe");
p->age = 20;
p->arr[0] = 3;
p->arr[1] = 5;
p->arr[2] = 10;

return p;
}