设为首页 加入收藏

TOP

脚本系统:C++内嵌python
2014-11-24 01:22:55 来源: 作者: 【 】 浏览:2
Tags:脚本 系统 内嵌 python

// liquidx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
PyObject * RepeatString (PyObject *pSelf, PyObject *pParams)
{
char *pstrString;
int iRepCount;
PyArg_ParseTuple(pParams, "si", &pstrString, &iRepCount);
for (int i = 0; i < iRepCount; i++)printf("---%d\n", i);
return PyInt_FromLong(iRepCount);
}
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize();
Py_IsInitialized();
PyImport_AddModule("HostAPI");
//create a function table
PyMethodDef HostAPIFuncs [] =
{
{"RepeatString", RepeatString, METH_VARARGS, NULL},
{NULL, NULL, NULL, NULL}
};
//initial the module with the function table
Py_InitModule("HostAPI", HostAPIFuncs);
PyObject *pName = PyString_FromString("helloworld");
PyObject * pModule = PyImport_Import(pName);
PyObject *pstr= NULL, *pDict= NULL;
//get the module dict
pDict = PyModule_GetDict(pModule);
pstr = PyDict_GetItemString(pDict, "PrintStuff");//get the function with the dict
PyObject_CallObject ( pstr, NULL ); //call the function
PyObject *pFunc = PyDict_GetItemString(pDict, "RepCount");
int as = PyInt_AS_LONG(pFunc);
printf( "**************************%d", as );
//Py_XDECREF(pFunc);
Py_XDECREF(pstr);
//Py_XDECREF(pDict);
Py_XDECREF(pModule);
Py_XDECREF(pName);
Py_Finalize(); // 清除
getchar();
return 0;
}
Helloworld.py 如下:
import HostAPI
def PrintStuff():
global RepCount
RepCount = HostAPI.RepeatString("String repetition", 4)


使用BOOST::PYTHON来写脚本的方法:
# include
#include "sample.h"
#include
#include
class sample
{
int m_value;
public:
int GetValue();
void SetValue(int v);
sample(int v=0);
~sample(void);
};
typedef boost::shared_ptr pSample;
sample::sample(int v):m_value(v)
{
}
int sample::GetValue()
{
return m_value;
}
void sample::SetValue(int v)
{
m_value=v;
}
sample::~sample(void)
{
fprintf(stderr,"Delete sample address is %x\n",this);
}
namespace boost{
namespace python{
BOOST_PYTHON_MODULE(pySample)
{
class_("sample")
.def("get",&sample::GetValue)
.def("set",&sample::SetValue)
;
register_ptr_to_python< pSample >();
}
}
};
using namespace boost::python;
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize();
initpySample();
try
{
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
pSample ptr( new sample(25) );
main_namespace["sample"] = ptr;
exec_file( "sample.py", main_namespace,main_namespace );
fprintf( stderr,"print in c:%d %d\n", ptr->GetValue(), ptr.use_count() );
}
catch(error_already_set const &)
{
PyErr_Print();
}
fprintf(stderr,"Before finalize\n");
Py_Finalize();
fprintf(stderr,"After finalize\n");
getchar();
return 0;
}
Sample.py 如下:
print "print in python",sample.get()
print sample
sample.set(12345)


结果:
print in python 25

print in c:12345 2
Before finalize
Delete sample address is b46638
After finalize


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ACE在Linux和Windows下编译及实例.. 下一篇CentOS RHEL 5.3 编译内核

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: