当前位置: 首页 > news >正文

C++(MFC)调用Python

环境:

phyton版本:3.10

VS版本:VS2017


包含文件头:Python\Python310\include
包含库文件:Python\Python310\libs

程序运行期间,以下函数只需要调用一次即可,重复调用会导致崩溃
void Initialize();
void Finalize();

“C_Test.py”需要拷贝程序运行所有的目录。

下载:https://download.csdn.net/download/luo_sen/88094131

PythonHelper.h

#pragma once
#include<string>
using namespace std;
/*
phyton版本:3.10
包含文件头:Python\Python310\include
包含库文件:Python\Python310\libs
*/
// https://docs.python.org/3/c-api/arg.html
/*
b (int) [unsigned char]
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C unsigned char.B (int) [unsigned char]
Convert a Python integer to a tiny int without overflow checking, stored in a C unsigned char.h (int) [short int]
Convert a Python integer to a C short int.H (int) [unsigned short int]
Convert a Python integer to a C unsigned short int, without overflow checking.i (int) [int]
Convert a Python integer to a plain C int.I (int) [unsigned int]
Convert a Python integer to a C unsigned int, without overflow checking.l (int) [long int]
Convert a Python integer to a C long int.k (int) [unsigned long]
Convert a Python integer to a C unsigned long without overflow checking.L (int) [long long]
Convert a Python integer to a C long long.K (int) [unsigned long long]
Convert a Python integer to a C unsigned long long without overflow checking.n (int) [Py_ssize_t]
Convert a Python integer to a C Py_ssize_t.c (bytes or bytearray of length 1) [char]
Convert a Python byte, represented as a bytes or bytearray object of length 1, to a C char.Changed in version 3.3: Allow bytearray objects.C (str of length 1) [int]
Convert a Python character, represented as a str object of length 1, to a C int.f (float) [float]
Convert a Python floating point number to a C float.d (float) [double]
Convert a Python floating point number to a C double.D (complex) [Py_complex]
Convert a Python complex number to a C Py_complex structure.
*/#define  PYTHON_FILE_NAME _T("C_Test")extern "C"
{
#include "Python.h"
}class CPythonHelper
{public:void			Initialize();void			Finalize();PyObject*		GetPyFunc(CString strModuleName, CString strFuncName);	PyObject*		RunPyFunc(CString strModuleName, CString strFuncName, PyObject *pArgs);int				PyTupleSetItem(PyObject *, Py_ssize_t, PyObject *);CString			PyObjectToCString(PyObject *pPyObj);int				PyObjectToInt(PyObject *pPyObj);double			PyObjectToFloat(PyObject *pPyObj);int				PyObjectToFloatArray(PyObject *pPyObj, CArray<double, double>& szData);PyObject*		IntToPyObject(int data);PyObject*		FloatToPyObject(double data);PyObject*       CStringToPyObject(CString data);PyObject*		FloatArrayToPyObject(CArray<double, double> &szData);PyObject*		CreateArgs(int size);protected:string			UTF8_To_string(const std::string & str);string			string_To_UTF8(const std::string & str);string			CStringToPyString(CString text);PyObject*		GetPyFunc(char* pModuleName, char* pFuncName);PyObject*		RunPyFunc(char* pModuleName, char* pFuncName, PyObject *pArgs);};

PythonHelper.cpp

#include "pch.h"
#include "PythonHelper.h"std::string CPythonHelper::UTF8_To_string(const std::string & str)
{int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 memset(pwBuf, 0, nwLen * 2 + 2);MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char * pBuf = new char[nLen + 1];memset(pBuf, 0, nLen + 1);WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr = pBuf;delete[]pBuf;delete[]pwBuf;pBuf = NULL;pwBuf = NULL;return retStr;
}
std::string CPythonHelper::string_To_UTF8(const std::string & str)
{int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2);::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char * pBuf = new char[nLen + 1];ZeroMemory(pBuf, nLen + 1);::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr(pBuf);delete[]pwBuf;delete[]pBuf;pwBuf = NULL;pBuf = NULL;return retStr;
}string CPythonHelper::CStringToPyString(CString text)
{string tt = CT2A(text);string strUTF8 = string_To_UTF8(tt);return strUTF8;
}CString CPythonHelper::PyObjectToCString(PyObject *pPyObj)
{PyObject* str = PyUnicode_AsEncodedString(pPyObj, "utf-8", "Error");char *result = (PyBytes_AsString(str));string temp = UTF8_To_string(result);CString strResult = CString(temp.c_str());return strResult;
}
int CPythonHelper::PyObjectToInt(PyObject *pPyObj)
{int data = 0;PyArg_Parse(pPyObj, "i", &data);return data;
}
double CPythonHelper::PyObjectToFloat(PyObject *pPyObj)
{double data = 0;PyArg_Parse(pPyObj, "d", &data);return data;
}int CPythonHelper::PyObjectToFloatArray(PyObject *pPyObj, CArray<double, double>& szData)
{szData.RemoveAll();int SizeOfList = PyList_Size(pPyObj);for (int i = 0; i < SizeOfList; i++){PyObject *Item = PyList_GetItem(pPyObj, i);double result=0;PyArg_Parse(Item, "d", &result);szData.Add(result);}return SizeOfList;}void CPythonHelper::Initialize()
{if (!Py_IsInitialized()){Py_Initialize();}}void CPythonHelper::Finalize()
{if (Py_IsInitialized()){Py_Finalize();}}
PyObject* CPythonHelper::GetPyFunc(CString strModuleName, CString strFuncName)
{string pModuleName = CStringToPyString(strModuleName);string pFuncName = CStringToPyString(strFuncName);return GetPyFunc((char*)pModuleName.c_str(), (char*)pFuncName.c_str());}PyObject* CPythonHelper::GetPyFunc(char* pModuleName, char* pFuncName)
{PyObject *pModule = PyImport_ImportModule(pModuleName);PyObject *pFunc = PyObject_GetAttrString(pModule, pFuncName);//Py_CLEAR(pModule);return pFunc;
}PyObject * CPythonHelper::RunPyFunc(char* pModuleName, char* pFuncName, PyObject *pArgs)
{PyObject *pFunc = GetPyFunc(pModuleName, pFuncName);PyObject *pRetrun = PyObject_CallObject(pFunc, pArgs);	//Py_CLEAR(pFunc);return pRetrun;
}
PyObject * CPythonHelper::RunPyFunc(CString strModuleName, CString strFuncName, PyObject *pArgs)
{string pModuleName = CStringToPyString(strModuleName);string pFuncName = CStringToPyString(strFuncName);PyObject *pRetrun = RunPyFunc((char*)pModuleName.c_str(), (char*)pFuncName.c_str(), pArgs);	return pRetrun;
}
int CPythonHelper::PyTupleSetItem(PyObject * pObject, Py_ssize_t index, PyObject * pData)
{return PyTuple_SetItem(pObject, index, pData);
}PyObject* CPythonHelper::IntToPyObject(int data)
{return Py_BuildValue("i", data);
}
PyObject* CPythonHelper::FloatToPyObject(double data)
{return Py_BuildValue("d", data);
}PyObject* CPythonHelper::FloatArrayToPyObject(CArray<double, double> &szData)
{int nSize = szData.GetSize();PyObject *PyList = PyList_New(nSize);for (int i = 0; i < PyList_Size(PyList); i++){PyList_SetItem(PyList, i, PyFloat_FromDouble(szData[i]));}return PyList;
}
PyObject* CPythonHelper::CStringToPyObject(CString data)
{return Py_BuildValue("s", (CStringToPyString(data).c_str())); }PyObject* CPythonHelper::CreateArgs(int size)
{return PyTuple_New(size);
}

C_Test.py

def Hello():print("hello Python")def Add(a,b):import numpy as npprint(np.pi)return a+bdef GetText(msg):print(msg)return  msg;

程序调用示例-整数(一):

CPythonHelper PY;PY.Initialize();int a = 10;int b = 20;int sum = 0;	//参数设置PyObject *pArgs = PyTuple_New(2);PY.PyTupleSetItem(pArgs, 0,Py_BuildValue("i", a));PY.PyTupleSetItem(pArgs, 1, Py_BuildValue("i", b));//调用函数PyObject *pRetrun = PY.RunPyFunc(PYTHON_FILE_NAME, _T("Add"), pArgs);//返回值转换PyArg_Parse(pRetrun, "i", &sum);//输出CString strResult = _T("");strResult.Format(_T("%d+%d=%d"), a, b, sum);AfxMessageBox(strResult);PY.Finalize();

程序调用示例-字符串(二)

  CPythonHelper PY;PY.Initialize();CString strText = _T("ABC中国人123");//参数设置PyObject *pArgs = PyTuple_New(1);PY.PyTupleSetItem(pArgs, 0, Py_BuildValue("s", PY.CStringToPyString(strText).c_str()));//调用函数PyObject *pRetrun = PY.RunPyFunc(PYTHON_FILE_NAME, _T("GetText"), pArgs);//返回值转换CString strResult = PY.PyObjectToCString(pRetrun);//输出AfxMessageBox(strResult);PY.Finalize();

程序调用示例-数组(三)

 

   CPythonHelper PY;PY.Initialize();CString strText = _T("ABC中国人123");//参数设置PyObject *pArgs = PY.CreateArgs(1);PyObject* pObject = PY.CStringToPyObject(strText);CArray<double, double> szData;szData.Add(1.0);szData.Add(1.1);szData.Add(2);PyObject *PyList = PY.FloatArrayToPyObject(szData);PY.PyTupleSetItem(pArgs, 0, PyList);//调用函数PyObject *pReturn = PY.RunPyFunc(PYTHON_FILE_NAME, _T("SetVaule"), pArgs);//返回值转换PY.PyObjectToFloatArray(pReturn, szData);PY.Finalize();

相关文章:

C++(MFC)调用Python

环境&#xff1a; phyton版本&#xff1a;3.10 VS版本&#xff1a;VS2017 包含文件头&#xff1a;Python\Python310\include 包含库文件&#xff1a;Python\Python310\libs 程序运行期间&#xff0c;以下函数只需要调用一次即可&#xff0c;重复调用会导致崩溃 void Initial…...

深度学习实践——循环神经网络实践

系列实验 深度学习实践——卷积神经网络实践&#xff1a;裂缝识别 深度学习实践——循环神经网络实践 深度学习实践——模型部署优化实践 深度学习实践——模型推理优化练习 代码可见于&#xff1a; 深度学习实践——循环神经网络实践 0 概况1 架构实现1.1 RNN架构1.1.1 RNN架…...

docker简单web管理docker.io/uifd/ui-for-docker

要先pull这个镜像docker.io/uifd/ui-for-docker 这个软件默认只能使用9000端口&#xff0c;别的不行&#xff0c;因为作者在镜像制作时已加入这一层 刚下下来镜像可以通过docker history docker.io/uifd/ui-for-docker 查看到这个端口已被 设置 如果在没有设置br0网关时&…...

SpringBoot内嵌的Tomcat:

SpringBoot内嵌Tomcat源码&#xff1a; 1、调用启动类SpringbootdemoApplication中的SpringApplication.run()方法。 SpringBootApplication public class SpringbootdemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootdemoApplicat…...

企业级docker应用注意事项

现在很多企业使用容器化技术部署应用&#xff0c;绕不开的docker技术&#xff0c;在生产环境docker常用操作总结。参考&#xff1a;https://juejin.cn/post/7259275893796651069 1. 尽可能使用官方镜像 在docker hub 官方 使用后面带有 DOCKER OFFICIAL IMAGE 标签的镜像&…...

腾讯云高性能计算集群CPU服务器处理器说明

腾讯云高性能计算集群以裸金属云服务器为节点&#xff0c;通过RDMA互联&#xff0c;提供了高带宽和极低延迟的网络服务&#xff0c;能满足大规模高性能计算、人工智能、大数据推荐等应用的并行计算需求&#xff0c;腾讯云服务器网分享腾讯云服务器高性能计算集群CPU处理器说明&…...

tinkerCAD案例:23.Tinkercad 中的自定义字体

tinkerCAD案例&#xff1a;23.Tinkercad 中的自定义字体 原文 Tinkercad Projects Tinkercad has a fun shape in the Shape Generators section that allows you to upload your own font in SVG format and use it in your designs. I’ve used it for a variety of desi…...

Box-Cox 变换

Box-cox 变化公式如下&#xff1a; y ( λ ) { y λ − 1 λ λ ≠ 0 l n ( y ) λ 0 y^{(\lambda)}\left\{ \begin{aligned} \frac{y^{\lambda} - 1}{\lambda} && \lambda \ne 0 \\ ln(y) && \lambda 0 \end{aligned} \right. y(λ)⎩ ⎨ ⎧​λyλ−1​ln…...

Linux wc命令用于统计文件的行数,字符数,字节数

Linux wc命令用于计算字数。 利用wc指令我们可以计算文件的Byte数、字数、或是列数&#xff0c;若不指定文件名称、或是所给予的文件名为"-"&#xff0c;则wc指令会从标准输入设备读取数据。 语法 wc [-clw][–help][–version][文件…] 参数&#xff1a; -c或–b…...

Python读取多个栅格文件并提取像元的各波段时间序列数据与变化值

本文介绍基于Python语言&#xff0c;读取文件夹下大量栅格遥感影像文件&#xff0c;并基于给定的一个像元&#xff0c;提取该像元对应的全部遥感影像文件中&#xff0c;指定多个波段的数值&#xff1b;修改其中不在给定范围内的异常值&#xff0c;并计算像元数值在每一景遥感影…...

Linux 之 wget curl

wget 命令 wget是非交互式的文件下载器&#xff0c;可以在命令行内下载网络文件 语法&#xff1a; wget [-b] url 选项&#xff1a; -b &#xff0c;可选&#xff0c;background 后台下载&#xff0c;会将日志写入到 当前工作目录的wget-log文件 参数 url &#xff1a; 下载链…...

AngularJS 和 React区别

目录 1. 背景&#xff1a;2. 版本&#xff1a;3. 应用场景&#xff1a;4. 语法&#xff1a;5. 优缺点&#xff1a;6. 代码示例&#xff1a; AngularJS 和 React 是两个目前最为流行的前端框架之一。它们有一些共同点&#xff0c;例如都是基于 JavaScript 的开源框架&#xff0c…...

【Solr】Solr搜索引擎使用

文章目录 一、什么是Solr?二 、数据库本身就支持搜索啊,干嘛还要搞个什么solr?三、如果我们想要使用solr那么首先我们得安装它 一、什么是Solr? 其实我们大多数人都使用过Solr,也许你不会相信我说的这句话,但是事实却是如此啊 ! 每当你想买自己喜欢的东东时,你可能会打开某…...

一起学算法(选择排序篇)

距离上次更新已经很久了&#xff0c;以前都是非常认真的写笔记进行知识分享&#xff0c;但是带来的情况并不是很好&#xff0c;一度认为发博客是没有意义的&#xff0c;但是这几天想了很多&#xff0c;已经失去了当时写博客的初心了&#xff0c;但是我觉得应该做点有意义的事&a…...

智能体的主观和能动

摘要 智能体的主动性是提升智能机器的能力的关键。围绕智能体的主动性存在很多思想迷雾&#xff0c;本文继续我们以前的工作&#xff0c;试图清理这些概念上的问题。我们的讨论显示&#xff1a;要研究主动性&#xff0c;并不一定需要研究意识&#xff0c;仅需要研究主观和能动就…...

AB 压力测试

服务器配置 阿里云Ubuntu 64位 CPU1 核 内存2 GB 公网带宽1 Mbps ab -c100 -n1000 http://127.0.0.1:9501/ -n&#xff1a;在测试会话中所执行的请求个数。默认时&#xff0c;仅执行一个请求。 -c&#xff1a;一次产生的请求个数。默认是一次一个。 ab -c 100 -n 200 ht…...

多旋翼物流无人机节能轨迹规划(Python代码实现)

目录 &#x1f4a5;1 概述 &#x1f4da;2 运行结果 &#x1f308;3 Python代码实现 &#x1f389;4 参考文献 &#x1f4a5;1 概述 多旋翼物流无人机的节能轨迹规划是一项重要的技术&#xff0c;可以有效减少无人机的能量消耗&#xff0c;延长飞行时间&#xff0c;提高物流效率…...

Vue通过指令 命令将打包好的dist静态文件上传到腾讯云存储桶 (保存原有存储目录结构)

1、在项目根目录创建uploadToCOS.js文件 &#xff08;建议起简单的名字 方便以后上传输入命令方便&#xff09; 2、uploadToCOS.js文件代码编写 const path require(path); const fs require(fs); const COS require(cos-nodejs-sdk-v5);// 配置腾讯云COS参数 const cos n…...

Linux 新硬盘分区,挂载

在Linux系统中&#xff0c;当你插入新的硬盘时&#xff0c;你需要进行一些步骤来使系统识别并使用它。以下是一些常见的步骤&#xff1a; 确保硬盘已正确连接到计算机。检查硬盘的电源和数据线是否牢固连接。 打开终端或命令行界面。 运行以下命令来扫描新硬盘&#xff1a; s…...

Stable Diffusion 开源模型 SDXL 1.0 发布

关于 SDXL 模型&#xff0c;之前写过两篇&#xff1a; Stable Diffusion即将发布全新版本Stable Diffusion XL 带来哪些新东西&#xff1f; 一晃四个月的时间过去了&#xff0c;Stability AI 团队终于发布了 SDXL 1.0。当然在这中间发布过几个中间版本&#xff0c;分别是 SDXL …...

反向工程与模型迁移:打造未来商品详情API的可持续创新体系

在电商行业蓬勃发展的当下&#xff0c;商品详情API作为连接电商平台与开发者、商家及用户的关键纽带&#xff0c;其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息&#xff08;如名称、价格、库存等&#xff09;的获取与展示&#xff0c;已难以满足市场对个性化、智能…...

在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:

在 HarmonyOS 应用开发中&#xff0c;手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力&#xff0c;既支持点击、长按、拖拽等基础单一手势的精细控制&#xff0c;也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档&#xff0c…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

C++中string流知识详解和示例

一、概览与类体系 C 提供三种基于内存字符串的流&#xff0c;定义在 <sstream> 中&#xff1a; std::istringstream&#xff1a;输入流&#xff0c;从已有字符串中读取并解析。std::ostringstream&#xff1a;输出流&#xff0c;向内部缓冲区写入内容&#xff0c;最终取…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

Matlab | matlab常用命令总结

常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...

基于 TAPD 进行项目管理

起因 自己写了个小工具&#xff0c;仓库用的Github。之前在用markdown进行需求管理&#xff0c;现在随着功能的增加&#xff0c;感觉有点难以管理了&#xff0c;所以用TAPD这个工具进行需求、Bug管理。 操作流程 注册 TAPD&#xff0c;需要提供一个企业名新建一个项目&#…...

JVM 内存结构 详解

内存结构 运行时数据区&#xff1a; Java虚拟机在运行Java程序过程中管理的内存区域。 程序计数器&#xff1a; ​ 线程私有&#xff0c;程序控制流的指示器&#xff0c;分支、循环、跳转、异常处理、线程恢复等基础功能都依赖这个计数器完成。 ​ 每个线程都有一个程序计数…...

JavaScript基础-API 和 Web API

在学习JavaScript的过程中&#xff0c;理解API&#xff08;应用程序接口&#xff09;和Web API的概念及其应用是非常重要的。这些工具极大地扩展了JavaScript的功能&#xff0c;使得开发者能够创建出功能丰富、交互性强的Web应用程序。本文将深入探讨JavaScript中的API与Web AP…...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...