我已經通過google進行了搜索,以及許多refence,但是我只看到一個複雜的編碼示例,你會給我一個示例(簡單代碼),所以我可以理解。 我已經編寫它,但它每次打破我運行它 下面的代碼簡單示例如何在C/C++中調用Python函數
#include <Python.h>
int main()
{
PyObject *pName, *pModule, *pDict, *pFun, *pValue;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString("C:\\Documents and Settings\\MASTER\\My Documents\\Visual Studio 2010\\Projects\\Python\\Test.py");
if(pName)printf("OK");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFun = PyDict_GetItemString(pDict, "prinTname");
if (PyCallable_Check(pFun))
{
PyObject_CallObject(pFun, NULL);
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
Py_DECREF(pDict);
Py_DECREF(pFun);
// Finish the Python Interpreter
Py_Finalize();
getchar();
return 0;
}
,並有在Python.exe一些消息 第一次機會異常在0x1e00503b:0000005:訪問衝突讀取位置0x00000004 。 Python.exe中的0x1e00503b未處理的異常:0xC0000005:訪問衝突讀取位置0x00000004。 程序'[4548] Python.exe:Native'已退出,代碼爲0(0x0)。
至於你的問題,你有沒有試過在調試器中運行你的程序?你確定所有的函數調用都成功嗎? –
我已經建立了它在VS 2010和它建立成功了,我只是想它調用C/C++ 這是Python代碼: DEF prinTname(): \t打印「OK玩轉」 –