c++调用python封装接口

xufangfa.h 文件

#include <iostream>

#include <Python.h>
#include <string>
#include <map>
using namespace std;


class CPyScriptRunner
{
public:
CPyScriptRunner()
{
Initialize();
}
~CPyScriptRunner()
{
Finalize();
}
public:
static bool CallEx(string FileName , string funcName , string fm = "" , void *returnVal = NULL, ...);
private:
static bool Initialize();
static bool Finalize();
static PyObject* LoadScript(string name);
//static map<string, PyObject*> m_mapScript; //存取python字典的map

};

xufangfa.cpp 文件

#include <iostream>
#include "xufangfa.h"
#include <string>
using namespace std;


static CPyScriptRunner g;


bool CPyScriptRunner::Initialize()
{
Py_Initialize();
if( !Py_IsInitialized() )
{
return false;
}
int n = PyRun_SimpleString("import sys");
n = PyRun_SimpleString("import sys");
PyRun_SimpleString("print sys.path");
string strPath = "D:/testPy";
char *cPath = const_cast<char *>( strPath.c_str() );
string cmd = string("sys.path.append('");
cmd+=cPath;
cmd+="')";
int nRet = PyRun_SimpleString(cmd.c_str());
if( -1 == nRet)
{
return false;
}
return true;
}


bool CPyScriptRunner::Finalize()
{
if(Py_IsInitialized())
{
return true;
}
return false;
}




PyObject* CPyScriptRunner::LoadScript(string strFileName)
{
PyObject *pModuleDict = NULL;
PyObject *pScriptModule = NULL;
pScriptModule = PyImport_ImportModule(strFileName.c_str());
if( !pScriptModule )
{
return NULL;
}
pModuleDict = PyModule_GetDict(pScriptModule);
if(!pModuleDict)
{
return NULL;
}
//m_mapScript[strFileName] = pModuleDict;
return pModuleDict;
}


bool CPyScriptRunner::CallEx(string filename , string funcname , string fmt , void* returnVal, ...)
{
PyObject* pModuleDict = NULL;
//map<string , PyObject*>::iterator iter = m_mapScript.find(filename);
//if(iter == m_mapScript.end())
//{
pModuleDict = CPyScriptRunner::LoadScript(filename);
if( pModuleDict == NULL )
{
return false;
}

//else 
// pModuleDict = iter->second;
if( pModuleDict == NULL )
{
return false;
}
PyObject *pArgs =NULL;
int nParamCount = fmt.size();
if( nParamCount > 1 )
{
va_list marker;
va_start(marker , returnVal);
pArgs = PyTuple_New(nParamCount -1);
for( int i = 1 ; i < nParamCount ; ++i )
{
PyObject *pValue = Py_BuildValue(const_cast<char *>(fmt.substr(i , 1).c_str()) , va_arg(marker , PyObject*));
PyTuple_SetItem(pArgs , i-1 , pValue);
}
va_end(marker);
}
PyObject* pFunc = PyDict_GetItemString(pModuleDict , funcname.c_str());
if(!pFunc || !PyCallable_Check(pFunc))
{
Py_XDECREF(pArgs);
return false;
}
PyObject*pResult = PyObject_CallObject(pFunc , pArgs);
if( pResult )
{
if( nParamCount >= 1)
{
if( pResult != Py_None )
{
if( !PyArg_Parse(pResult ,const_cast<char*>(fmt.substr(0,1).c_str()),returnVal))
{

}
else
{


}
}
}
Py_XDECREF(pArgs);
return true;
}
else
{
Py_XDECREF(pArgs);
return false;
}
return true;
}






int main()
{
PyObject *pyNode = NULL ;
if(CPyScriptRunner::CallEx("test" , "testaa" , "O" ,pyNode  ))
{
int a = PyInt_AsLong(pyNode);
return a;
}
else
cout<<"没有输出成功!!!"<<endl;


}


































/*
set_new_handler(nomorememory);
try
{
while(1)
{
new int[100000000];
cout<<"Allocating 100000000 ints"<<endl;
}
}
catch( exception e)
{
cout<<e.what()<<"xxx"<<endl;
}
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值