【问题标题】:Python - call function from python33.dll (access violation)Python - 从 python33.dll 调用函数(访问冲突)
【发布时间】:2017-02-14 04:10:48
【问题描述】:

我有这个代码:

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

使用 sublime text 运行(python3.3 嵌入)

【问题讨论】:

    标签: python dll sublimetext3 ctypes


    【解决方案1】:

    使用PyDLL,而不是 WinDLL。来自文档:

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

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

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多