python3.5 EMBED in C
来源:互联网 发布:简单数据库软件 编辑:程序博客网 时间:2024/06/07 12:34
首先吐槽下,python2 跟 3的差别太大了,网上找的embed python的教程大多数都是2.x的
在3.x版本大多不能用,很多函数都不存在了。找了几天的资料终于完成了c ember python基本的调用。
环境准备说明:
1、vc6 + python3.5.2 , 默认安装不会生成python_xx_d.lib ,
PYTHON安装包安装时需要选择高级模式,全部勾选。然后_d 调试文件都会生成
2、python3.5是vs2015编译的,所以还需要下载 vc14运行环境,调试版的还需要下载vcruntime140d.dll,ucrtbased.dll
========================================================================================
先上python脚本
#coding: utf-8
class mymath:
def __init__(self,name):
self.name=name
print(name)
def add(self,num1,num2):
print(num1+num2)
return num1 + num2
def retTuple(self):
print (self.name)
return ("中国人民解放军",231,222.55)
def retSingleStr(self,egg):
print (egg+"法拉利")
return "中国人民解放军"
def retSingleInt(self):
print (self.name)
return 231
def retSingleFloat(self,num1):
print (self.name)
return num1 + 231.58
============================================================================================
// pyEmbed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <Python.h>
#include <windows.h>
#include "pyhelper.hpp"
int main()
{
CPyInstance hInstance;
PyObject *pModule, *pClass,*pStuObj;
////////////////
//载入模块pyemb3
try{
pModule = PyImport_ImportModule("pyemb3");
assert(pModule!=NULL);
//从模块中得到类mymath
pClass = PyObject_GetAttrString (pModule, "mymath");
assert(pClass!=NULL);
PyObject *temp=Py_BuildValue("(s)","JACK!");
assert(temp!=NULL);
pStuObj = PyObject_CallObject(pClass, temp);
////////////////////////////////////////返回元组
PyObject *ast = PyObject_CallMethod(pStuObj, "retTuple", "()");
char *command;
int amt;
float nums;
char szData[]="我不想上幼儿园";
if (!PyArg_ParseTuple(ast, "sif", &command,&amt,&nums))
{
printf("发生错误!");
}
Convert(command,command,CP_UTF8,CP_ACP);
printf("返回元组的函数:%s_%i_%.2f\n",command,amt,nums);
////////////////////////调用返回单个字符串的类函数
Convert(szData,szData,CP_ACP,CP_UTF8);
ast = PyObject_CallMethod(pStuObj, "retSingleStr", "(s)",szData);
char *test = PyUnicode_AsUTF8(ast);
Convert(test,test,CP_UTF8,CP_ACP);
printf("返回单个字符串函数%s,value: %s\n ",Py_TYPE(ast)->tp_name,test);
////////////////////////////返回int 型
PyObject *result = PyObject_CallMethod(pStuObj, "add", "(ii)", 1, 2);
printf("返回INT:1 + 2 = %d\n", PyLong_AsLong(result));
////////////////////////////返回FLOAT型
result = PyObject_CallMethod(pStuObj, "retSingleFloat", "(f)", 3.25);
printf("返回float:%.2f\n", PyFloat_AsDouble(result));
}
catch(...)
{
printf("发生错误!");
}
////////////////
return 0;
}
最后我有个疑问
************不知道为什么 我没有调用 Py_XDECREF, Py_XDECREF 维护计数器 也没有出现内存泄漏
维护计数器太烦人了,每个变量都这样自行维护真是吐血啊。。。
- python3.5 EMBED in C
- C經典百例 in PYTHON3 .2
- Embed perl in c/c++, C调用perl, perl调用C 技术详解
- Embed an HTML control in your own window using plain C
- Embed an HTML control in your own window using plain C
- 内存数据表和内存索引通用结构(基于C++) --Memory Table Interface Embed In Your Application
- Embed an HTML control in your own window using plain C
- Embed an HTML control in your own window using plain C
- Embed
- embed
- embed
- embed
- How to embed Lua 5.1 in C++
- Embed Adobe Flash Player in Web
- Embed a JRE in a Windows executable
- [centos]install python3.5 in centos
- embed\vs2008_ucos\bsp\bspThread.h.C
- Using OBJECT, EMBED and APPLET Tags in Java Plug-in
- 转:在华为的工作体验,可以get一些点(仅自娱自乐)
- android之PowerManager 与电源管理,解决灭屏状态下来短信屏幕不会点亮问题
- STA基础分析-setup和hold
- Java笔记整理:一切都是对象
- Android 音量调节View
- python3.5 EMBED in C
- 82.Single Number-落单的数(容易题)
- 二、什么是asp网站
- UVA1610 Party Games
- Android 解析微信基础表情并显示
- java 泛型讲解
- Core Python Programming学习记录(1)
- linux 文件目录命令 pwd
- Spring的配置文档applicationContext.xml