python ctypes 指针_Python Ctypes传递.h文件中定义的结构指针。

我认为您可能缺少的是确切地知道您希望分配结构内存的位置。下面的c代码提供了一个为struct分配内存并返回指向它的指针的函数(new_struct())。#include

#include

#include

typedef struct {

int a;

int b;

} my_struct;

my_struct *new_struct()

{

my_struct *struct_instance = (my_struct *)malloc(sizeof(my_struct));

memset(struct_instance, 0, sizeof(my_struct));

return struct_instance;

}

int modify_struct(my_struct *ms) {

ms->a = 1;

ms->b = 2;

return 0;

}

void print_struct_c(my_struct *ms) {

printf("my_struct {\n"

" a = %d\n"

" b = %d\n"

"}\n", ms->a, ms->b);

}

从Python获取指针,调用执行分配的C函数,然后可以将其传递给将其作为参数的其他C函数。import ctypes

lib_file_path = <<< path to lib file >>>

# Very simple example of how to declare a ctypes structure to twin the

# C library's declaration. This doesn't need to be declared if the Python

# code isn't going to need access to the struct's data members.

class MyStruct(ctypes.Structure):

_fields_ = [('a', ctypes.c_int),

('b', ctypes.c_int)]

def print_struct(s):

# Print struct that was allocated via Python ctypes.

print("my_struct.a = %d, my_struct.b = %d" % (s.a, s.b))

def print_struct_ptr(sptr):

# Print pointer to struct. Note the data members of the pointer are

# accessed via 'contents'.

print("my_struct_ptr.contents.a = %d, my_struct_ptr.contents.b = %d"

% (sptr.contents.a, sptr.contents.b))

my_c_lib = ctypes.cdll.LoadLibrary(lib_file_path)

# If you don't need to access the struct's data members from Python, then

# it's not necessary to declare MyStruct above. Also, in that case,

# 'restype' and 'argtypes' (below) can be set to ctypes.c_void_p instead.

my_c_lib.new_struct.restype = ctypes.POINTER(MyStruct)

my_c_lib.modify_struct.argtypes = [ctypes.POINTER(MyStruct)]

# Call C function to create struct instance.

my_struct_c_ptr = my_c_lib.new_struct()

print_struct_ptr(my_struct_c_ptr)

my_c_lib.modify_struct(my_struct_c_ptr)

print_struct_ptr(my_struct_c_ptr)

# Allocating struct instance from Python, then passing to C function.

my_struct_py = MyStruct(0, 0)

print_struct(my_struct_py)

my_c_lib.modify_struct(ctypes.byref(my_struct_py))

print_struct(my_struct_py)

# Data members of Python allocated struct can be acessed directly.

my_struct_py.a = 555

my_c_lib.print_struct_c(ctypes.byref(my_struct_py)) # Note use of 'byref()'

# to invoke c function.

上面的代码已经更新,包括如何通过Python分配结构实例的示例,以及如何访问C已分配或Python分配结构的数据成员(请注意打印函数中的差异)。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/292751.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iptables 手册

iptables防火墙可以用于创建过滤(filter)与NAT规则。所有Linux发行版都能使用iptables&#xff0c;因此理解如何配置iptables将会帮助你更有效地管理Linux防火墙。如果你是第一次接触iptables&#xff0c;你会觉得它很复杂&#xff0c;但是一旦你理解iptables的工作原理&#x…

Android之如何看混淆后的错误日志代码

第一步&#xff1a; 找到混淆的mapping.txt文件 build -> outputs -> mapping -> release -> mapping.txt 或者&#xff1a;1项目目录的progurad下有一个mapping.txt文件 这是混淆后的名字 和原名字的映射关系。 第二步: 找到项目用的sdk依次定位到sdk -> to…

inode

硬盘的最小存储单位叫“扇区(sector)”,每个扇区存储512字节(相当于0.5kb).系统读取硬盘时&#xff0c;只会读取多个sector即一个block.block 是文件存取的最小单位block的大小为&#xff14;kb,也就是8*sector1block. inode 是存放文件的元信息&#xff0c;如文件的创建者&…

男朋友的回答可以多敷衍?

1 跟最好的朋友好到什么程度&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼2 不要在妈妈打麻将的时候要生活费&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼3 现在卖家还要发毒誓了&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼4 刘德华老师…

Android之android.os.DeadObjectException原因

问题&#xff1a; 今天看到客户的手机出现了 &#xfeff;&#xfeff;andorid.os.DeadObjectException android.os.DeadObjectException 07-13 18:28:45.398: W/System.err(32272): at android.os.BinderProxy.transact(Native Method) 原因&#xff1a; 我们项目有2个进程 …

学习Java,容易被你忽略的小细节(2)

昨天心情真的太糟糕了&#xff0c;写完《学习Java&#xff0c;值得注意你注意的问题&#xff08;1&#xff09;》之后&#xff0c;迎来些许的支持以后就是一片片的谴责。我的主页上涌现出许许多多Java方面的牛人&#xff0c;谴责我水平太低&#xff0c;写的问题太初级。搞得我非…

WPF XAML 为项目设置全局样式

全局资源样式属性App.xaml<Application.Resources><ResourceDictionary><br><ResourceDictionary.MergedDictionaries><ResourceDictionary Source"Dictionary.xaml"/></ResourceDictionary.MergedDictionaries><br><S…

controller是什么意思_SpringMVC是什么??

SpringMVC是什么&#xff1f;一&#xff0c;首先是一个MVC框架。在web模型中&#xff0c;MVC是一种很流行的框架&#xff0c;通过把Model&#xff0c;View&#xff0c;Controller分离&#xff0c;把较为复杂的web应用分成逻辑清晰的几部分&#xff0c;是为了简化开发&#xff0…

起一卦都这么凶

公元&#xff1a;2014年5月22日0时15分14秒阳8局农历&#xff1a;2014年04月24日0时15分立夏&#xff1a;2014-5-5 22:16:00 芒种&#xff1a;2014-6-6 2:32:00干支&#xff1a;甲午年己巳月癸巳日壬子时旬空&#xff1a;辰巳空戌亥空午未空寅卯空直符&#xff1a;天冲直使&am…

You third iOS app,这个APP要放到手机上面运行,才会成功,才会新建记录。

当提示如上图所示时&#xff0c;很可能你只有在苹果设备上运行才可以提示成功&#xff0c; iCloud is available。 转载于:https://www.cnblogs.com/liqiwa/p/5933542.html

TCP/IP、Http、Socket的区别

TCP/IP、Http、Socket的区别 大学学习网络基础的时候老师讲过&#xff0c;网络由下往上分为物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。通过初步的了解&#xff0c;我知道IP协议对应于网络层&#xff0c;TCP协议对应于传输层&#xff0c;而HTTP协议对应于应…

客户要求ASP.NET Core API返回特定格式,怎么办?(续2)

前言前2次&#xff0c;我们都是假设客户需要返回不同的字符串格式。但是&#xff0c;有可能客户要求的返回是非本文格式&#xff0c;比如文件流。怎么办&#xff1f;思路前提&#xff0c;当然还是使用同一API接口&#xff0c;不影响现有使用方式。虽然ASP.NET Core Web API默认…

第四章例题、心得及问题。

例题4-1&#xff1a; #include<stdio.h> #include<math.h> int main(void) {int denominator,flag;double item,pi;flag1;denominator1;item1.0;pi0;while(fabs(item)>0.0001){itemflag*1.0/denominator;pipiitem;flag-flag;denominatordenominator2;}pipi*4;pr…

springboot springcloud区别_SpringCloud微服务全家桶-第一篇!为什么要用微服务

从今天开始&#xff0c;学习SpringCloud微服务全家桶。一、引导1、什么是微服务&#xff1f;2、微服务之间是如何独立通讯的3、springCloud和Dubbo有哪些区别&#xff1f;4、什么是服务熔断&#xff1f;什么是服务降级5、微服务的优缺点分别是什么&#xff1f;6、微服务技术栈有…

[Todo] 乐观悲观锁,自旋互斥锁等等

乐观锁、悲观锁、要实践 http://chenzhou123520.iteye.com/blog/1860954 《mysql悲观锁总结和实践》 http://chenzhou123520.iteye.com/blog/1863407 《mysql乐观锁总结和实践》 http://outofmemory.cn/sql/optimistic-lock-and-pessimistic-lock 注意&#xff0c;以下的表里面…

Android之 ListView滑动时不加载图片

listview加载图片优化的功能&#xff0c; 在我们使用新浪微博的时候&#xff0c;细心的同学一定发现了&#xff0c;在滑动的过程中&#xff0c;图片是没有被加载的&#xff0c; 而是在滑动停止时&#xff0c;才加载图片了。 我们今天就做一个这样的效果吧。 我们先考虑两个问题…

C#之Lock

lock 关键字将语句块标记为临界区&#xff0c;方法是获取给定对象的互斥锁&#xff0c;执行语句&#xff0c;然后释放该锁。 class Program{static void Main(string[] args){Thread t new Thread(LockObject.MonitorIncrement);Thread t1new Thread(new ThreadStart(LockObje…

记一次 .NET 某消防物联网 后台服务 内存泄漏分析

一&#xff1a;背景 1. 讲故事去年十月份有位朋友从微信找到我&#xff0c;说他的程序内存要炸掉了。。。截图如下&#xff1a;时间有点久&#xff0c;图片都被清理了&#xff0c;不过有点讽刺的是&#xff0c;自己的程序本身就是做监控的&#xff0c;结果自己出了问题&#xf…