Python - 从 python33 调用函数.dll(访问冲突)



我有这个代码:

import ctypes

lib = ctypes.WinDLL('python33.dll')
print(lib['PyRun_SimpleString'])
func = lib['PyRun_SimpleString']
func.argtypes = [ctypes.c_char_p]
func.restype = ctypes.c_int
arg = "print(':P')"
arg = arg.encode('utf-8')
func(arg)

结果:

OSError: exception: access violation reading 0x00000004

使用崇高的文本运行(python3.3嵌入(

使用 PyDLL,而不是 WinDLL。 从文档中:

此类的实例的行为类似于 CDLL 实例,只是在函数调用期间不会释放 Python GIL,并且在函数执行后会检查 Python 错误标志。如果设置了错误标志,则会引发 Python 异常。

因此,这只对直接调用 Python C api 函数有用。

最新更新