快速业务通道

脚本系统:c++内嵌python

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-29

// liquidx.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/args.hpp>
#include <boost/python/class.hpp>
#include <boost/python/str.hpp>
#include <boost/python/dict.hpp>
#include <iostream>
#include <string>
#include <list>
#include <map>

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 <boost/python.hpp>
#include "sample.h"
#include <stdio.h>
#include <boost/smart_ptr.hpp>

class sample
{
int m_value;
public:
int GetValue();
void SetValue(int v);

sample(int v=0);
~sample(void);
};
typedef boost::shared_ptr<sample> 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>("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_fi

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号