脚本系统:c++内嵌python[VC/C++编程]
本文“脚本系统:c++内嵌python[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
// 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;
}
以上是“脚本系统:c++内嵌python[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |