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

  • Post author:
  • Post category:python


我使用

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端解决它.