python ctypes 回调函数

定义结构体

'''
typedef struct pborca_direntry 
{
					                        /* Comments */
    TCHAR            szComments[PBORCA_MAXCOMMENT + 1];

    LONG            lCreateTime;            /* Time of entry create/mod */ 
    LONG            lEntrySize;             /* Size of entry */
    LPTSTR           lpszEntryName;          /* Pointer to entry name */
    PBORCA_TYPE     otEntryType;            /* Entry type */

} PBORCA_DIRENTRY
'''
class PBORCA_DIRENTRY(ctypes.Structure):
    _fields_ = [('szComments'   ,c_char*256),
                ('lCreateTime'  ,c_long),
                ('lEntrySize'   ,c_long),
                ('lpszEntryName',c_char_p),
                ('otEntryType'  ,c_int)]

定义结构体指针

ctypes.POINTER(KBDLLHOOKSTRUCT)

stdcall调用约定

'''
void (__stdcall *pbcall )(PPBORCA_DIRENTRY,LPVOID);
'''
def pbcallback(pd, a):
  print pd.contents.lpszEntryName

pbcall = ctypes.WINFUNCTYPE(ctypes.POINTER(PPBORCA_DIRENTRY),c_void_p)
c_pbcallback = KHOOK(pbcallback)

 cdecl调用约定

CmpFuncType = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))

def py_cmp_func(a, b):
    if a[0] > b[0]:
        return 1
    elif a[0] < b[0]:
        return -1
    else:
        return 0
        
cmpfunc = CmpFuncType(py_cmp_func)

调用dll 

#stdcall
Objdll = ctypes.windll.LoadLibrary("dllpath")
Objdll = ctypes.WinDLL("dllpath") 

#cdecl
Objdll = ctypes.cdll.LoadLibrary("dllpath")
Objdll = ctypes.CDLL("dllpath")


#调用函数
# 方法1
szPara = create_string_buffer('/0'*100)
dll.PrintInfo(byref(szPara), 100);
print szPara.value
 
# 方法2
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = c_char_p( )
pStr.value = sBuf
#pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
dll.PrintInfo(pStr, len(pStr.value))
print pStr.value
 
# 方法3
strMa = "/0"*20
FunPrint  = dll.PrintInfo
FunPrint.argtypes = [c_char_p, c_int]
#FunPrint.restypes = c_void_p
nRst = FunPrint(strMa, len(strMa))
print strMa,len(strMa)
 
# 方法4
pStr2 = c_char_p("/0")
print pStr2.value
#pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
dll.PrintInfo(pStr2, len(pStr.value))
print pStr2.value

#参考https://blog.csdn.net/magictong/article/details/3075478/


pb通过pborca提取pbl中的对象

# -*- coding: cp936 -*-
import ctypes

from ctypes import *

class pborca_direntry(ctypes.Structure):
    _fields_ = [
        ('szComments', ctypes.c_char*256),
        ('lCreateTime', ctypes.c_long),
        ('lEntrySize', ctypes.c_long),
        ('lpszEntryName', ctypes.c_char_p),
        ('otEntryType', ctypes.c_byte),
    ]

        
class pborca:
    def __init__(self,dllPath):
        self.pborc90= windll.LoadLibrary(dllPath)


    def open(self):
        self.pbs = self.pborc90.PBORCA_SessionOpen()
        return self.pbs

    def close(self):
        self.pborc90.PBORCA_SessionClose(self.pbs)
        self.pbs=None

    def direntry_callback(self,callback):
        CMPFUNC = WINFUNCTYPE(None, POINTER(pborca_direntry),c_void_p)
        return CMPFUNC(callback)

    
    def LibraryDirectory(self):
        return self.pborc90.PBORCA_LibraryDirectory

    # 遍历所有的对象
    def LibraryDirectoryExt(self,pblPath):
        ls_open=0
        if not hasattr(self,'pbs'):
            ls_open=self.open()
        print self.pbs
        res=[]
        def def_callback(abc,a):
            res.append(abc.contents)
        _callback = self.direntry_callback(def_callback)
        self.pborc90.PBORCA_LibraryDirectory(self.pbs,pblPath,'',0,_callback,0)
        if ls_open!=0:
            self.close()
        return res


def callback(abc,a):
    print 'callback by c++ in python...  %s' % abc.contents.lpszEntryName


pb9 = pborca("R:\Sybase\Shared\PowerBuilder\pborc90.dll")
hORCASession = pb9.open()
_callback = pb9.direntry_callback(callback)
pb9.LibraryDirectory()(hORCASession,'D:\pb工具\pbfunction.pbl','',0,_callback,0)
pb9.close()

l = pb9.LibraryDirectoryExt('D:\pb工具\pbfunction.pbl')
print l

'''
pborc90 = windll.LoadLibrary("R:\Sybase\Shared\PowerBuilder\pborc90.dll")

CMPFUNC = WINFUNCTYPE(None, POINTER(pborca_direntry),c_void_p) 
_callback = CMPFUNC(callback)
sOpen = pborc90.PBORCA_SessionOpen
sOpen.restype = c_void_p
hORCASession = sOpen()
listLib = pborc90.PBORCA_LibraryDirectory

listLib(hORCASession,'D:\pb工具\pbfunction.pbl','',0,_callback,0)


print hORCASession
sClose = pborc90.PBORCA_SessionClose
sClose.argtypes = [c_void_p]
sClose.restype = None
sClose(hORCASession)

'''

特别需要注意的是,在python3中:

Byte String:对应 C 中的一个字符串指针 char * ,指向一块内存区域。(字符串前面会加小b,  b"helloworld")

Unicode String :对应 C 中一个宽字符串指针 wchar_t *,指向一块内存区域。(对应的就是字符串, "helloworld")

a = self.pborc90.PBORCA_LibraryDirectory(self.pbs,pblPath.encode('gb2312'),'',0,_callback,0)

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,要调用C语言的回调函数,需要使用ctypes库。ctypesPython的外部函数库,它提供了与C语言库函数交互的接口。 首先,我们需要在Python代码中导入ctypes库,以便使用其功能。 接下来,我们需要定义一个C语言回调函数,并通过ctypes的CFUNCTYPE函数声明函数类型和参数类型。然后,我们将C语言回调函数的指针传递给Python函数。 在Python中,我们可以使用ctypes.CDLL函数加载C语言动态链接库,并通过函数名调用C语言回调函数。 下面是一个简单的示例代码,演示了如何在Python中调用C语言的回调函数: import ctypes # 定义C语言回调函数 def my_callback_function(number): print("C语言回调函数被调用,传递的参数是:", number) # 声明C语言回调函数的类型 CallbackFunc = ctypes.CFUNCTYPE(None, ctypes.c_int) # 将Python函数转换为C语言回调函数 c_callback = CallbackFunc(my_callback_function) # 加载C语言动态链接库 c_lib = ctypes.CDLL("my_c_lib.so") # 假设my_c_lib.so是C语言动态链接库 # 调用C语言回调函数 c_lib.call_callback(c_callback) 在上面的示例中,我们定义了一个名为my_callback_function的Python函数作为C语言回调函数。然后,我们使用CFUNCTYPE函数声明了C语言回调函数的类型,指定了参数类型和返回类型。 通过CallbackFunc函数将Python函数转换为C语言回调函数,并将其传递给C语言动态链接库中的call_callback函数。 当C语言动态链接库中的call_callback函数被调用时,C语言回调函数会被执行,同时将参数传递给Python中的my_callback_function函数。 这样,我们就可以在Python中成功调用C语言的回调函数了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值