📄 getpythonfunction.cpp
字号:
// GetPythonFunction.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<Python.h>//前面所做的一切配置都是为了调用这个头文件和相关库
#include <object.h>
#include <iostream>
#include <algorithm>
using namespace std;
void getPythonFunc(char * py_MoudleName,char * py_FunName, int py_params[],int py_paramNum);
void getPythonFunc(char * py_MoudlemName,char * py_FuncName, char * py_params[],int py_paramNum);
int getPythonFunc(int argc, char* argv[]);
void main()
{
//第一种形式调用两个函数
//调用函数MaxNum
int py_paramNum=2;
char * py_MoudleName="PyMaxNum";
char * py_FuncName="MaxNum";
char * py_params[]={"23","32","34"};
getPythonFunc(py_MoudleName,py_FuncName,py_params,py_paramNum);
//调用函数GetCub
py_paramNum=3;
py_FuncName="GetCub";
char * py_params1[]={"23","32","34"};
getPythonFunc(py_MoudleName,py_FuncName,py_params1,py_paramNum);
//////////////////////////////////////////////////////////////////////////
//第二种形式调用两个函数
//调用函数MaxNum
int argc=4;
char *argv[]={"PyMaxNum","MaxNum","23","32"};
getPythonFunc(argc, argv);
//调用函数GetCub
argc=5;
char *argv1[]={"PyMaxNum","GetCub","23","32","34.34"};
getPythonFunc(argc, argv1);
//////////////////////////////////////////////////////////////////////////
//第三种调用形式,参数列用的是int数组
py_MoudleName="PyMaxNum";
py_FuncName="MaxNum";
int para[]={134,12}; //输入参数,存储到数组当中,以便于加入到Python函数的参数元组当中
int paraNum=2; //para[]数组的参数个数
getPythonFunc(py_MoudleName,py_FuncName, para, paraNum);
}
void getPythonFunc(char * py_MoudlemName,char * py_FuncName, char * py_params[],int py_paramNum)
{
// 初始化解释器
Py_Initialize();
// 加载模块:py_MoudlemName为待加载模块名
PyObject* pPy_Module = PyImport_ImportModule(py_MoudlemName);
if (NULL == pPy_Module)
{
PyErr_Print();
return ;
}
// 获得函数对象:py_FuncName为函数名
PyObject* pPy_Func = PyObject_GetAttrString(pPy_Module, py_FuncName);
if (NULL == pPy_Func)
{
// 释放资源引用计数
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
return ;
}
// 打包参数:tuple packing
PyObject* pPy_Args = PyTuple_New(py_paramNum);
PyObject* pPy_Value = NULL;
for (int i = 0; i < py_paramNum; i++)
{
pPy_Value = PyInt_FromLong(atoi(py_params[i]));
if (NULL == pPy_Value)
{
Py_DECREF(pPy_Args), pPy_Args = NULL;
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
cout << "Invalid params" << endl;
return;
}
PyTuple_SetItem(pPy_Args, i,pPy_Value);
pPy_Value = NULL;
}
// 执行函数对象
pPy_Value = PyObject_CallObject(pPy_Func, pPy_Args);
Py_DECREF(pPy_Args);
if (NULL != pPy_Value)
{
cout << "funct obj : " <<py_FuncName<< endl;
cout << "paramters : " << flush;
copy(py_params, py_params+py_paramNum, ostream_iterator<char*>(cout, " "));
cout << endl;
cout << "return val: " <<PyInt_AsLong(pPy_Value)<< endl<< endl;
Py_DECREF(pPy_Value), pPy_Value = NULL;
}
else
{
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
return ;
}
// 释放资源引用计数
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
// 释放Python解释器
Py_Finalize();
return ;
}
//第二中调用形式,把所有的参数都放到一个数组当中
int getPythonFunc(int argc, char* argv[])
{
// 初始化解释器
Py_Initialize();
// 加载模块:argv[0]为待加载模块名
PyObject* pPy_Module = PyImport_ImportModule(argv[0]);
if (NULL == pPy_Module)
{
PyErr_Print();
return -1;
}
// 获得函数对象:argv[1]为函数名
PyObject* pPy_Func = PyObject_GetAttrString(pPy_Module, argv[1]);
if (NULL == pPy_Func)
{
// 释放资源引用计数
Py_DECREF(pPy_Module), pPy_Module = NULL;
// Python Traceback
PyErr_Print();
return -1;
}
// 打包参数:tuple packing
PyObject* pPy_Args = PyTuple_New(argc - 2);
PyObject* pPy_Value = NULL;
for (int i = 0; i <argc -2; i++)
{
pPy_Value = PyInt_FromLong(atoi(argv[i + 2]));
if (NULL == pPy_Value)
{
Py_DECREF(pPy_Args), pPy_Args = NULL;
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
cout << "Invalid params" << endl;
return -1;
}
PyTuple_SetItem(pPy_Args, i, pPy_Value);
pPy_Value = NULL;
}
// 执行函数对象
pPy_Value = PyObject_CallObject(pPy_Func, pPy_Args);
Py_DECREF(pPy_Args);
if (NULL != pPy_Value)
{
cout << "funct obj : " << argv[1] << endl;
cout << "paramters : " << flush;
copy(argv+2, argv+argc, ostream_iterator<char*>(cout, " "));
cout << endl;
cout << "return val: " << PyInt_AsLong(pPy_Value) << endl<< endl;
Py_DECREF(pPy_Value), pPy_Value = NULL;
}
else
{
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
return -1;
}
// 释放资源引用计数
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
// 释放Python解释器
Py_Finalize();
return 0;
}
void getPythonFunc(char * py_MoudleName,char * py_FuncName, int py_params[],int py_paramNum)
{
Py_Initialize();
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
PyObject * pArg = NULL;
PyObject * pPy_Value = NULL;
pModule = PyImport_ImportModule(py_MoudleName);
pFunc = PyObject_GetAttrString(pModule,py_FuncName);
PyObject* pPy_Args = PyTuple_New(py_paramNum);
for (int i = 0; i < 2; i++)
{
pPy_Value = Py_BuildValue("i", py_params[i]);
if (NULL == pPy_Value)
{
Py_DECREF(pPy_Args), pPy_Args = NULL;
Py_DECREF(pFunc), pFunc = NULL;
Py_DECREF(pModule), pModule = NULL;
PyErr_Print();
cout << "Invalid params" << endl;
return ;
}
PyTuple_SetItem(pPy_Args, i,pPy_Value);
pPy_Value = NULL;
}
pPy_Value=PyEval_CallObject(pFunc, pPy_Args);
cout << "return val: " <<PyInt_AsLong(pPy_Value) << endl<< endl;
Py_Finalize();
}
//现在可以调用有参数的python函数,也可以返回值,但是参数必须指定为字符集形式。
//而且返回值只能是整型。郁闷。。。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -