1、首先要配好vs开发工程
注意版本;我这使用32位的python那么我vs工程这边也选择32位的编译环境去配置
注意点;需要将python安装目录的一些文件拷过来作为vs工程使用。
2、C++调用Python结果
py代码
这里引用了cdll库也需要放置到运行目录,py文件也是需要放置到运行目录(也就是exe生成所在目录)
import os
import time
from ctypes import *
def testDLL():
pDll = CDLL("./pythonTestCDll.dll")
pstr = create_string_buffer(1024, '\0') # 创建字符串缓冲区
# 对输入输出参数进行声明
GetAndSetString = pDll.GetAndSetString
GetAndSetString.restype = c_char_p
GetAndSetString.argtypes = [c_char_p]
pchar = GetAndSetString(pstr)
szbuffer = c_char_p(pchar) # 强制转换为c_char_p类型,取其value值
print(pstr.value)
print(szbuffer.value)
def Start():
testDLL()
C++代码
#include <iostream>
#include "Python.h"
using namespace std;
void Hello();
void Add();
void Start();
void Hello1()
{
cout << "\n调用Test001.py中的Add函数..." << endl;
}
int main(int argc, char* argv[])
{
/*cout << "调用Test001.py中的Hello函数..." << endl;
Hello();
cout << "\n调用Test001.py中的Add函数..." << endl;
Add();*/
cout << "调用testMultiprocessingDll.py中的Start函数..." << endl;
Start();
getchar();
return 0;
}
void Start()
{
Py_Initialize();//调用Py_Initialize()进行初始化
if (!Py_IsInitialized()) {
printf("Python envirment initialized fale!");
return;
}
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('D:/code/pythonTestCDll/CdoPython/Release/DLLs')");
//PyRun_SimpleString("print(\"sdasd\")");
pModule = PyImport_ImportModule("testMultiprocessingDll");//调用的Python文件名 py文件放置exe同级
if (pModule == NULL)
{
PyErr_Print();
cout << "PyImport_ImportModule Fail!" << endl;
return;
}
pFunc = PyObject_GetAttrString(pModule, "__main__");//调用的函数名
PyEval_CallObject(pFunc, NULL);//调用函数,NULL表示参数为空
Py_Finalize();//调用Py_Finalize,和Py_Initialize相对应的.
}
void Hello()
{
Py_Initialize();//调用Py_Initialize()进行初始化
if (!Py_IsInitialized()) {
printf("Python envirment initialized fale!");
return ;
}
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
PyRun_SimpleString("print(\"sdasd\")" );
pModule = PyImport_ImportModule("Test001");//调用的Python文件名 py文件放置exe同级
if (pModule == NULL)
{
PyErr_Print();
cout << "PyImport_ImportModule Fail!" << endl;
return;
}
pFunc = PyObject_GetAttrString(pModule, "Hello");//调用的函数名
PyEval_CallObject(pFunc, NULL);//调用函数,NULL表示参数为空
Py_Finalize();//调用Py_Finalize,和Py_Initialize相对应的.
}
//调用Add函数,传两个int型参数
void Add()
{
Py_Initialize();
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule = PyImport_ImportModule("Test001");//Test001:Python文件名
pFunc = PyObject_GetAttrString(pModule, "Add");//Add:Python文件中的函数名
//创建参数:
PyObject *pArgs = PyTuple_New(2);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数
PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 6));//0--序号,i表示创建int型变量
PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 8));//1--序号
//返回值
PyObject *pReturn = NULL;
pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数
//将返回值转换为int类型
int result;
PyArg_Parse(pReturn, "i", &result);//i表示转换成int型变量
cout << "6 + 8 = " << result << endl;
Py_Finalize();
}
3、报错:ValueError: source code string cannot contain null bytes
Python中import模块时报SyntaxError: (unicode error)utf-8 codec can not decode 错误的解决办法
混合编程之——C++调用python2.7&python3.5
4、C++调用python文件中import时报错
C++通过内嵌解释器调用Python及间接调用Python三方库
5、C++多线程调用Python多进程multiprocessing时发现不支持
C++多线程调用Python多进程
6、C++调用python文件中的线程以及字符串在C++和python中间的传递
python代码
import os
import threading
import time
g_flage = 0
def thread_test():
while(True):
if g_flage == False:
print('循环代码段区域')
else:
break
def Hello(str):
print("%s" % str)
return "PythonGoC++"
def Add(a, b):
return a+b
def Begin_voice():
print('开启线程!')
global g_flage
g_flage = False
p = threading.Thread(target=thread_test)
p.start()
def End_voice():
print('关闭线程!')
global g_flage
g_flage = True
C++代码
#include <iostream>
#include "Python.h"
#include <thread>
using namespace std;
void Hello();
void Add();
void Hello1()
{
cout << "\n调用Test001.py中的Add函数..." << endl;
}
std::thread *m_pVideoThread = NULL;
PyObject * pModule = NULL;
void Start()
{
PyObject * pFunc = NULL;
PyObject * pFunc2 = NULL;
//Py_SetPythonHome(L"D:\\code\\pythonTestCDll\\CdoPython\\Release");
Py_Initialize();//调用Py_Initialize()进行初始化
if (!Py_IsInitialized()) {
printf("Python envirment initialized fale!");
return;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('D:/code/pythonTestCDll/CdoPython/Release/DLLs')");
//PyRun_SimpleString("sys.path.append('D:/code/pythonTestCDll/CdoPython/Release/Lib')");
pModule = PyImport_ImportModule("Test001");//调用的Python文件名 py文件放置exe同级
if (pModule == NULL)
{
PyErr_Print();
cout << "PyImport_ImportModule Fail!" << endl;
return;
}
pFunc = PyObject_GetAttrString(pModule, "Begin_voice");//调用的函数名
PyEval_CallObject(pFunc, NULL);//调用函数,NULL表示参数为空
Py_Finalize();//调用Py_Finalize,和Py_Initialize相对应的.
}
int main(int argc, char* argv[])
{
cout << "调用Test001.py中的Hello函数..." << endl;
Hello();
cout << "\n调用Test001.py中的Add函数..." << endl;
Add();
cout << "调用testMultiprocessingDll.py中的Start函数..." << endl;
m_pVideoThread = new std::thread(Start);
//Start();
_sleep(1000);
PyObject * pFunc2 = NULL;
pFunc2 = PyObject_GetAttrString(pModule, "End_voice");//调用的函数名
PyEval_CallObject(pFunc2, NULL);//调用函数,NULL表示参数为空
getchar();
return 0;
}
void Hello()
{
Py_Initialize();//调用Py_Initialize()进行初始化
if (!Py_IsInitialized()) {
printf("Python envirment initialized fale!");
return ;
}
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
PyRun_SimpleString("print(\"sdasd\")" );
pModule = PyImport_ImportModule("Test001");//调用的Python文件名 py文件放置exe同级
if (pModule == NULL)
{
PyErr_Print();
cout << "PyImport_ImportModule Fail!" << endl;
return;
}
pFunc = PyObject_GetAttrString(pModule, "Hello");//调用的函数名
PyObject *pArgs = PyTuple_New(1);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数
PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", "C++DoPython"));//0--序号,i表示创建int型变量
PyObject *pReturn = NULL;
//PyEval_CallObject(pFunc, NULL);//调用函数,NULL表示参数为空
pReturn = PyEval_CallObject(pFunc, pArgs);
string result;
PyArg_Parse(pReturn, "s", &result);//i表示转换成int型变量
printf("%s", result);
Py_Finalize();//调用Py_Finalize,和Py_Initialize相对应的.
}
//调用Add函数,传两个int型参数
void Add()
{
Py_Initialize();
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule = PyImport_ImportModule("Test001");//Test001:Python文件名
pFunc = PyObject_GetAttrString(pModule, "Add");//Add:Python文件中的函数名
//创建参数:
PyObject *pArgs = PyTuple_New(2);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数
PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 6));//0--序号,i表示创建int型变量
PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 8));//1--序号
//返回值
PyObject *pReturn = NULL;
pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数
//将返回值转换为int类型
int result;
PyArg_Parse(pReturn, "i", &result);//i表示转换成int型变量
cout << "6 + 8 = " << result << endl;
Py_Finalize();
}