我是靠谱客的博主 腼腆百褶裙,最近开发中收集的这篇文章主要介绍python ctypes 回调函数_Python ctypes中具有自定义类型的回调,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

那里有一些错误,有些是基本的Python错误:

from ctypes import *

class A(Structure):

_fields_ = [

("a1", c_char_p),

("a2", c_int)]

class Callback(object):

def outputCallback(self, a, b): # outputCallback(): ?

print a.contents.a1, a.contents.a2

# The prototype of the `outputCallback`

# tells that it returns nothing, `void`

cb = Callback()

CMPFUNC = CFUNCTYPE(None, POINTER(A), c_void_p) # so `restype` shoud be

# None, void

cb.cmp_func = CMPFUNC(cb.outputCallback)

libc = CDLL('library.so')

libc.function1(0, 0, cb.cmp_func, 0, 0)

测试DLL:

#include

#ifdef _WIN32

#define DLLEXPORT __declspec(dllexport)

#else

#define DLLEXPORT

#endif

#ifdef __cplusplus

extern "C" {

#endif

typedef struct A

{

const unsigned char* a1;

unsigned int a2;

} A;

int DLLEXPORT function1(int a,

int b,

void (*outputCallback)(const A* a, void* b),

int c,

int d)

{

A obj;

obj.a1 = "Hello";

obj.a2 = 5;

outputCallback(&obj, NULL);

return 0;

}

#ifdef __cplusplus

};

#endif

测试:

>gcc library.c -o library.so -shared

>python py.py

Hello 5

>

最后

以上就是腼腆百褶裙为你收集整理的python ctypes 回调函数_Python ctypes中具有自定义类型的回调的全部内容,希望文章能够帮你解决python ctypes 回调函数_Python ctypes中具有自定义类型的回调所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部