C调用Python脚本文件里的函数
2009-09-02 09:21
597 查看
#Python脚本,存为pytest.py
def add(a,b):
print "in python function add"
print "a = " + str(a)
print "b = " + str(b)
print "ret = " + str(a+b)
return a + b
// C代码调,用上面的add函数
#include <stdio.h>
#include <stdlib.h>
#include "C:/Python26/include/python.h"
#pragma comment(lib, "C:\\Python26\\libs\\python26.lib")
int main(int argc, char** argv)
{
// 初始化Python
//在使用Python系统前,必须使用Py_Initialize对其
//进行初始化。它会载入Python的内建模块并添加系统路
//径到模块搜索路径中。这个函数没有返回值,检查系统
//是否初始化成功需要使用Py_IsInitialized。
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;
Py_Initialize();
// 检查初始化是否成功
if ( !Py_IsInitialized() )
{
return -1;
}
// 载入名为pytest的脚本(注意:不是pytest.py)
pName = PyString_FromString("pytest");
pModule = PyImport_Import(pName);
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if ( !pDict )
{
return -1;
}
// 找出函数名为add的函数
pFunc = PyDict_GetItemString(pDict, "add");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [add]");
getchar();
return -1;
}
// 参数进栈
pArgs = PyTuple_New(2);
// PyObject* Py_BuildValue(char *format, ...)
// 把C++的变量转换成一个Python对象。当需要从
// C++传递变量到Python时,就会使用这个函数。此函数
// 有点类似C的printf,但格式不同。常用的格式有
// s 表示字符串,
// i 表示整型变量,
// f 表示浮点数,
// O 表示一个Python对象。
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));
// 调用Python函数
pRetVal = PyObject_CallObject(pFunc, pArgs);
printf("function return value : %ld\r\n", PyInt_AsLong(pRetVal));
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pRetVal);
// 关闭Python
Py_Finalize();
return 0;
}
//一下为个人实践的另一套方法
def add(a,b):
print "in python function add"
print "a = " + str(a)
print "b = " + str(b)
print "ret = " + str(a+b)
return a + b
// C代码调,用上面的add函数
#include <stdio.h>
#include <stdlib.h>
#include "C:/Python26/include/python.h"
#pragma comment(lib, "C:\\Python26\\libs\\python26.lib")
int main(int argc, char** argv)
{
// 初始化Python
//在使用Python系统前,必须使用Py_Initialize对其
//进行初始化。它会载入Python的内建模块并添加系统路
//径到模块搜索路径中。这个函数没有返回值,检查系统
//是否初始化成功需要使用Py_IsInitialized。
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;
Py_Initialize();
// 检查初始化是否成功
if ( !Py_IsInitialized() )
{
return -1;
}
// 载入名为pytest的脚本(注意:不是pytest.py)
pName = PyString_FromString("pytest");
pModule = PyImport_Import(pName);
if ( !pModule )
{
printf("can't find pytest.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if ( !pDict )
{
return -1;
}
// 找出函数名为add的函数
pFunc = PyDict_GetItemString(pDict, "add");
if ( !pFunc || !PyCallable_Check(pFunc) )
{
printf("can't find function [add]");
getchar();
return -1;
}
// 参数进栈
pArgs = PyTuple_New(2);
// PyObject* Py_BuildValue(char *format, ...)
// 把C++的变量转换成一个Python对象。当需要从
// C++传递变量到Python时,就会使用这个函数。此函数
// 有点类似C的printf,但格式不同。常用的格式有
// s 表示字符串,
// i 表示整型变量,
// f 表示浮点数,
// O 表示一个Python对象。
PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));
// 调用Python函数
pRetVal = PyObject_CallObject(pFunc, pArgs);
printf("function return value : %ld\r\n", PyInt_AsLong(pRetVal));
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pRetVal);
// 关闭Python
Py_Finalize();
return 0;
}
//一下为个人实践的另一套方法
#include <Python.h> #include <conio.h> int main() { Py_Initialize(); if (!Py_IsInitialized()) { printf("初始化错误\n"); return -1; } PyObject* pModule = NULL; PyObject* pFunc = NULL; PyObject* pArg = NULL; PyObject* pRetVal = NULL; pModule = PyImport_ImportModule("hello"); pFunc = PyObject_GetAttrString(pModule,"hello"); pArg = Py_BuildValue("(i,i)",33,44); pRetVal = PyObject_CallObject(pFunc,pArg); printf("%d\n",PyInt_AsLong(pRetVal)); Py_Finalize(); _getch(); return 0; }
相关文章推荐
- C调用Python脚本文件里的函数
- 两步解决python调用Matlab的脚本和函数文件
- C调用Python脚本文件里的函数(转)
- C调用Python脚本文件里的函数
- C调用Python脚本文件里的函数
- 用脚本C调用Python脚本文件里的函数
- C调用Python脚本文件里的函数
- python 一个.py文件如何调用另一个.py文件中的类和函数
- IDAPython脚本之收集函数的调用信息
- python调用自己文件函数/执行函数找不到包
- 数据挖掘之Python调用R包、函数、脚本
- arcgis脚本Python调用自定义的函数方法
- Python中在脚本中引用其他文件函数的方法
- python 如何调用另一个路径下py文件的函数
- 【python】如何在某.py文件中调用其他.py内的函数
- WebBrowser 调用HTML脚本文件函数
- 关于使用c#调用python脚本文件,脚本文件需要传递参数
- 从 stdin 读取输入的C程序示例 && python脚本调用可执行文件
- matlab调用Python的.py脚本文件
- Python实现调用另一个路径下py文件中的函数方法总结