?? getpythonfunction.cpp
字號:
// GetPythonFunction.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<Python.h>//前面所做的一切配置都是為了調(diào)用這個頭文件和相關(guān)庫
#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()
{
//第一種形式調(diào)用兩個函數(shù)
//調(diào)用函數(shù)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);
//調(diào)用函數(shù)GetCub
py_paramNum=3;
py_FuncName="GetCub";
char * py_params1[]={"23","32","34"};
getPythonFunc(py_MoudleName,py_FuncName,py_params1,py_paramNum);
//////////////////////////////////////////////////////////////////////////
//第二種形式調(diào)用兩個函數(shù)
//調(diào)用函數(shù)MaxNum
int argc=4;
char *argv[]={"PyMaxNum","MaxNum","23","32"};
getPythonFunc(argc, argv);
//調(diào)用函數(shù)GetCub
argc=5;
char *argv1[]={"PyMaxNum","GetCub","23","32","34.34"};
getPythonFunc(argc, argv1);
//////////////////////////////////////////////////////////////////////////
//第三種調(diào)用形式,參數(shù)列用的是int數(shù)組
py_MoudleName="PyMaxNum";
py_FuncName="MaxNum";
int para[]={134,12}; //輸入?yún)?shù),存儲到數(shù)組當(dāng)中,以便于加入到Python函數(shù)的參數(shù)元組當(dāng)中
int paraNum=2; //para[]數(shù)組的參數(shù)個數(shù)
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 ;
}
// 獲得函數(shù)對象:py_FuncName為函數(shù)名
PyObject* pPy_Func = PyObject_GetAttrString(pPy_Module, py_FuncName);
if (NULL == pPy_Func)
{
// 釋放資源引用計數(shù)
Py_DECREF(pPy_Module), pPy_Module = NULL;
PyErr_Print();
return ;
}
// 打包參數(shù):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;
}
// 執(zhí)行函數(shù)對象
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 ;
}
// 釋放資源引用計數(shù)
Py_DECREF(pPy_Func), pPy_Func = NULL;
Py_DECREF(pPy_Module), pPy_Module = NULL;
// 釋放Python解釋器
Py_Finalize();
return ;
}
//第二中調(diào)用形式,把所有的參數(shù)都放到一個數(shù)組當(dāng)中
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;
}
// 獲得函數(shù)對象:argv[1]為函數(shù)名
PyObject* pPy_Func = PyObject_GetAttrString(pPy_Module, argv[1]);
if (NULL == pPy_Func)
{
// 釋放資源引用計數(shù)
Py_DECREF(pPy_Module), pPy_Module = NULL;
// Python Traceback
PyErr_Print();
return -1;
}
// 打包參數(shù):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;
}
// 執(zhí)行函數(shù)對象
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;
}
// 釋放資源引用計數(shù)
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();
}
//現(xiàn)在可以調(diào)用有參數(shù)的python函數(shù),也可以返回值,但是參數(shù)必須指定為字符集形式。
//而且返回值只能是整型。郁悶。。。
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -