2017-02-14 87 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函數。