2017-02-14 50 views
1

我有这样的代码: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嵌入)

回答

1

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

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

因此,这仅用于直接调用Python C api函数。