我已经搜索过google和许多参考资料,但我只看到一个复杂的编码示例,你能给我一个示例(简单的代码)让我能理解。 我已经编码了,但每次我运行它时它都会崩溃 这是密码
#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处的第一次机会异常:0xc000005:访问冲突读取位置0x00000004。 Python.exe中0x1e00503b处未处理的异常:0xc000005:访问冲突读取位置0x00000004。 程序“[4548]Python.exe:Native”已退出,代码为0(0x0)。
您很可能还没有安装python开发工具,也就是说,python.h永远也找不到。找到python.h并查找编译错误,如果有,则重新发送。
编辑:这是旧信息。include应该和将include目录添加到include目录一样简单,请参见:How do I get Visual Express 2010 to find my python.h header file?
这是我问的一个问题的代码。这应该是你想要的。
参数: argv[1]包含指向.py文件的路径 argv[2]包含要调用的函数的名称
如果您还没有找到它,下面的链接描述了Python的C接口。非常方便: http://docs.python.org/2/c-api/index.html
相关问题 更多 >
编程相关推荐