我對此採取的PyObject作爲參數 像傳遞Python對象作爲參數傳遞給C/C++使用功能的ctypes
void MyFunction(PyObject* obj)
{
PyObject *func, *res, *test;
//function getAddress of python object
func = PyObject_GetAttrString(obj, "getAddress");
res = PyObject_CallFunction(func, NULL);
cout << "Address: " << PyString_AsString(PyObject_Str(res)) << endl;
}
一個函數的DLL,我想通過調用從蟒蛇的dll這一功能ctypes的
我的Python代碼看起來像
import ctypes as c
path = "h:\libTest"
libTest = c.cdll.LoadLibrary(path)
class MyClass:
@classmethod
def getAddress(cls):
return "Some Address"
prototype = c.CFUNCTYPE(
c.c_char_p,
c.py_object
)
func = prototype(('MyFunction', libTest))
pyobj = c.py_object(MyClass)
func(c.byref(pyobj))
有我的Python代碼 一些問題,當我運行這款C頌我得到的消息像
WindowsError:異常:訪問衝突閱讀0x00000020
任何建議,以提高Python代碼將appriciated。
我想'PyObject_GetAttrString(班 「的getAddress」)'真的'PyObject_GetAttrString(OBJ 「的getAddress」)'?我還假設你檢查每個函數結果,並且它們都不是null,直到'cout <<'? – 9000
是它是一個錯字。謝謝。我會解決它。除此之外,C代碼還是不錯的。我在Python部分遇到問題。將指針指向從Python到Python函數的Python對象。怎麼樣 ? – Maverick
嗯,我不知道真實的,但我_think_'byref'在這裏不需要。我想'func(pyobj)'應該可以工作。通過'byref',C代碼得到一個指向指針的指針。 – 9000