-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallPython.cpp
More file actions
141 lines (135 loc) · 4.26 KB
/
CallPython.cpp
File metadata and controls
141 lines (135 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// CallPython.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Python.h"
#include <iostream>
#include <string>
using namespace std;
int callPythonFunc(const char* scriptName, const char* funcName) {
Py_Initialize();
int callState = NULL;
if (Py_IsInitialized()) {
PyObject* pModule = NULL;
PyObject* PFunc = NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('../PyrthonScripts/')");
//初始化python系统文件路径,保证可以访问到 .py文件
pModule = PyImport_ImportModule(scriptName);
if (pModule) {
cout << "Import module " << scriptName << " success" << endl;
PFunc = PyObject_GetAttrString(pModule, funcName);
if (PFunc || PyCallable_Check(PFunc)) {
cout << "Import attribute " << funcName << " success" << endl;
//创建一个元组,包含1个元素
PyObject* pArgs = PyTuple_New(1);
//s表示创建string型变量,赋值给第0个参数
PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", "cpp is calling python function!"));
PyObject* pReturn = PyEval_CallObject(PFunc, pArgs);
PyArg_Parse(pReturn, "i", &callState);
Py_DecRef(pArgs);
Py_DecRef(pReturn);
Py_DecRef(PFunc);
Py_DecRef(pModule);
//PyMem_Free(pArgs);
//销毁创建的PyObject对象引用
//当函数的返回值是New reference时,需要对PyObject* 变量使用Py_DECREF()
//返回值是 Borrowed reference时,无需使用Py_DECREF()
}
else {
callState = 0;
cout << "Import attribute " << funcName << " faile" << endl;
}
}
else
{
callState = -1;
cout << "Import module " << scriptName << " faile" << endl;
}
}
Py_Finalize();
return callState;
}
int callPythonAdd(const char* scriptName, const char* funcName)
{
Py_Initialize();
int callState = NULL;
if (Py_IsInitialized()) {
PyObject* pModule = NULL;
PyObject* PFunc = NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('../PyrthonScripts/')");
//初始化python系统文件路径,保证可以访问到 .py文件
pModule = PyImport_ImportModule(scriptName);
if (pModule) {
cout << "Import module " << scriptName << " success" << endl;
PFunc = PyObject_GetAttrString(pModule, funcName);
if (PFunc || PyCallable_Check(PFunc)) {
cout << "Import attribute " << funcName << " success" << endl;
PyObject* pArgs = PyTuple_New(2);
//i表示创建int型变量,赋值给第0个参数
PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 10));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 50));
PyObject* pReturn = PyEval_CallObject(PFunc, pArgs);
//解析返回参数
PyArg_Parse(pReturn, "i", &callState);
cout << "the result is " << callState << endl;
Py_DecRef(pArgs);
Py_DecRef(pReturn);
Py_DecRef(PFunc);
Py_DecRef(pModule);
//PyMem_Free(pArgs);会出现内存非法访问
//销毁创建的PyObject对象引用
//当函数的返回值是New reference时,需要对PyObject* 变量使用Py_DECREF()
//返回值是 Borrowed reference时,无需使用Py_DECREF()
}
else {
callState = 0;
cout << "Import attribute " << funcName << " faile" << endl;
}
}
else
{
callState = -1;
cout << "Import module " << scriptName << " faile" << endl;
}
}
Py_Finalize();
return callState;
}
int callPythonFile(const char* scriptName, const char* scriptPath, int argc, wchar_t** argv) {
Py_Initialize();
int callState = -1;
if (Py_IsInitialized())
{
cout << " python interpreter initialize success" << endl;
//Py_SetPath(scriptPath);
//设置默认的模块搜索路径
//切换路径函数 GetCurrentDirectory 和 SetCurrentDirectory
/*wprintf(L"%s\n", scriptPath);
wprintf(L"%s\n", argv[0]);
wprintf(L"%s\n", argv[1]);
*/
PySys_SetArgv(argc, argv);
//导入执行脚本时的命令行参数
FILE *fp;
//errno_t err = fopen_s(&fp, scriptFile, "r");
if (!(fopen_s(&fp, scriptPath, "r")))
{
//获取python文件句柄
cout << "open python script success" << endl;
if (!(PyRun_AnyFile(fp, scriptName)))
{
callState = 1;
cout << "call python script success" << endl;
}
//执行调用脚本文件命令, 注意文件的路径
}
else
{
callState = -2;
cout << "open python script fail" << endl;
}
fclose(fp);
}
Py_Finalize();
return callState;
}