python函数调用函数指针_python – 使用ctypes调用指向静态定义的函数的函数指针...

我使用

Python 3.6.0和ctypes来访问从.dll导出的函数.我想通过使用paramflags参数实例化函数原型来访问函数. .dll中的一个函数是静态定义的,因此不会导出.但是.dll还包含一个指向该函数并导出的函数指针.

这是.dll的简化版本

typedef void ( *function ) ( int *output, int input );

__declspec( dllexport ) void two( int *output, int input );

__declspec( dllexport ) extern const function four;

void two( int *output, int input ) {

*output = 2 * input;

}

static void three( int *output, int input ) {

*output = 3 * input;

}

const function four = three;

这是我的Python代码

import ctypes

dll = ctypes.cdll.test

# calling two( ) as an attribute of dll

two = dll.two

two.argtypes = [ ctypes.POINTER( ctypes.c_int ), ctypes.c_int ]

two.restype = None

output = ctypes.c_int( )

# output = c_long(2)

two( ctypes.byref( output ), 1 )

print( 'output =', output )

# calling two( ) by instantiating a function prototype with paramflags

function = ctypes.CFUNCTYPE( None, ctypes.POINTER( ctypes.c_int ), ctypes.c_int )

paramflags = ( ( 2, 'output' ), ( 1, 'input' ) )

two = function( ( 'two', dll ), paramflags )

# output = 2

output = two( 1 )

print( 'output =', output )

# I can call four( ) using in_dll( )

four = function.in_dll( dll, 'four' )

output = ctypes.c_int( )

# output = c_long(3)

four( ctypes.byref( output ), 1 )

print( 'output =', output )

# but not as an attribute of dll

four = dll.four

four.argtypes = [ ctypes.POINTER( ctypes.c_int ), ctypes.c_int ]

four.restype = None

output = ctypes.c_int( )

# this results in a "OSError: exception: access violation writing 0x6C0C3064"

four( ctypes.byref( output ), 1 )

print( 'output =', output )

# nor by instantiating a function prototype

paramflags = ( ( 2, 'output' ), ( 1, 'input' ) )

four = function( ( 'four', dll ), paramflags )

# this results in a "OSError: exception: access violation writing 0x6C0C3064"

output = four( 1 )

print( 'output =', output )

我可以访问两个()作为dll的属性,并通过实例化一个函数原型.但是,当我尝试使用four()时,我得到“OSError:exception:access violation writing 0x6C0C3064”.经过大量的实验,我发现我可以使用in_dll()访问four(),但后来我无法应用paramflags功能.

有没有办法访问four()并使用paramflags?

注意:我不想更改dl​​l(它不是我的项目的一部分),我试图在Python端解决它.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值