python ctypes 回调函数使用 函数使用 库函数调用 指定参数

  • Post author:
  • Post category:python


# coding=UTF-8

import ctypes
from ctypes import*

TEST_TIME = 5

#加载所依赖的动态库
CDLL("/lib/librbac-cli.so.1.0.0", mode=ctypes.RTLD_GLOBAL)
CDLL("/lib/librg_tty.so.1.0.0", mode=ctypes.RTLD_GLOBAL)
CDLL("/lib/libclient_new.so.1.0.0", mode=ctypes.RTLD_GLOBAL)
CDLL("/lib/librg_lsm_impl.so.1.0.0", mode=ctypes.RTLD_GLOBAL)
libc = cdll.LoadLibrary("/lib/libdnsclient.so.0.111")
libc1 = cdll.LoadLibrary("/lib/librg-thread.so.1.0.0")

g_master = c_void_p()
g_timer = ctypes.create_string_buffer(2048)
thread = ctypes.create_string_buffer(2048)
libc1.rg_thread_insert_timer.argtypes = [c_void_p, c_void_p, c_void_p, c_void_p, c_long, c_char_p]
libc1.rg_thread_insert_timer.restype = c_int
libc1.rg_thread_master_create.restype = c_void_p
libc1.rg_thread_fetch.argtypes = [c_void_p, c_void_p]
libc1.rg_thread_fetch.restype = c_void_p
libc1.rg_thread_call.argtypes = [c_void_p]

#构造test_timer回调函数
def test_timer(t):
    print("test timer exipre\n")
    libc1.rg_thread_insert_timer(g_master, g_timer, my_test_timer, None, 1, b"g-timer")
    return 0
#构造test_timer回调函数指针
test_timer_type = ctypes.CFUNCTYPE(c_int, c_void_p)

my_test_timer = test_timer_type(test_timer)

g_master = libc1.rg_thread_master_create()

libc1.rg_thread_insert_timer(g_master, g_timer, my_test_timer, None, 1, b"g-timer")

while(libc1.rg_thread_fetch(g_master, thread)):
    libc1.rg_thread_call(thread)
    

#libc.dns_agent_init.argtypes = [c_char_p,c_void_p]
#ret = libc.dns_agent_init(b"app",g_master)



版权声明:本文为nb_zsy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。